Cod sursa(job #2061037)

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

int m;
long long n;
int a[7006], b[7006];
struct sir{
    int t0, t1, a, b, x, y, z;
};
sir t, t2, e, aux;
inline void next(sir &tr){
    int z = a[tr.t0] + b[tr.t1] + tr.z;
    while(z >= m) z = z - m;
    tr.t0 = tr.t1; tr.t1 = z;
}
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, &t.a, &t.b, &t.x, &t.y, &t.z, &m, &n);
    t.z %= m;
    for(int i = 0; i <= m ; ++i) a[i] = ((1LL * i * i) % m * t.a + 1LL * t.x * i) % m;
    for(int i = 0; i <= m ; ++i) b[i] = ((1LL * i * i) % m * t.b + 1LL * t.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;
}