Cod sursa(job #2061045)

Utilizator giotoPopescu Ioan gioto Data 8 noiembrie 2017 21:32:21
Problema Rsir Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <bits/stdc++.h>
using namespace std;

int m, a, b, x, y, z;
long long n;
int a1[7006], b1[7006];
struct sir{
    int t0, t1;
};
sir t, t2, e, aux;
inline void next(sir &tr){
    int x = a1[tr.t0] + b1[tr.t1] + z;
    if(x >= m) x = x - m;
    tr.t0 = tr.t1; tr.t1 = x;
}
int main(){
    freopen("rsir.in", "r", stdin);
    freopen("rsir.out", "w", stdout);
    scanf("%d%d%d%d%d%d%d%d%lld", &t.t0, &t.t1, &a, &b, &x, &y, &z, &m, &n);
    z %= m; x %= m; y %= m; a %= m; b %= m;
    for(int i = 0; i <= m ; ++i) a1[i] = (1LL * i * i * a + 1LL * x * i) % m;
    for(int i = 0; i <= m ; ++i) b1[i] = (1LL * i * i * b + 1LL * y * i) % m;
    e = t; aux = t; t2 = aux;
    next(t); next(e); next(e);
    while(!(t.t1 == e.t1 && t.t0 == e.t0)){next(t); next(e); next(e);}
    int C = 1, L = 0, cr = 0;
    next(t); next(e); next(e); next(t2);
    while(!(t.t1 == e.t1 && t.t0 == e.t0)){next(t); next(e); next(e); next(t2); ++C;}
    t = aux;
    while(!(t.t1 == t2.t1 && t.t0 == t2.t0)){next(t); next(t2); ++L;}
    t = aux;
    if(n <= L) while(n > 0) next(t), --n;
    else{
        while(L > 0) {next(t); --n; --L;}
        int w = n % C;
        while(w > 0) next(t), --w;
    }

    printf("%d", t.t0);
    return 0;
}