Cod sursa(job #1639364)

Utilizator dariusdariusMarian Darius dariusdarius Data 8 martie 2016 11:59:10
Problema Rsir Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <algorithm>
#include <fstream>
#include <iostream>
using namespace std;

int a, b, x, y, z, t0, t1, MOD;

int mult_a[7005];
int mult_b[7005];

inline int f(int p, int q) {
    int answer = mult_a[p] + mult_b[q] + z;
    while (answer >= MOD) {
        answer -= MOD;
    }
    return answer;
}

int detect(const pair<int, int> &x0) {
    int cycle = 1;
    int last = x0.second, next = f(x0.first, x0.second);
    while (x0.first != last || x0.second != next) {
        int _next = f(last, next);
        last = next;
        next = _next;
        cycle += 1;
    }
    return cycle;
}

int main() {
    ifstream fin("rsir.in");
    ofstream fout("rsir.out");
    long long n;
    fin >> t0 >> t1 >> a >> b >> x >> y >> z >> MOD >> n;
    for (int i = 0; i < MOD; ++ i) {
        mult_a[i] = (1LL * i * i * a + 1LL * i * x) % MOD;
        mult_b[i] = (1LL * i * i * b + 1LL * i * y) % MOD;
    }
    t0 = t0 % MOD; t1 = t1 % MOD;
    int cycle = detect(make_pair(t0, t1));
    n = n % cycle;
    int t2;
    for (int i = 1; i < n; ++ i) {
        t2 = f(t0, t1);
        t0 = t1; t1 = t2;
    }
    fout << t2 << "\n";
    return 0;
}