Cod sursa(job #1330831)

Utilizator smaraldaSmaranda Dinu smaralda Data 31 ianuarie 2015 00:11:38
Problema Rsir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include<stdio.h>
#include<algorithm>
using namespace std;

pair <int, int> turtle, hare;
int a, b, x, y, z, t0, t1, m;

pair <int, int> next (pair <int, int> t) {
    return make_pair(t.second, (a * t.first * t.first + b * t.second * t.second + x * t.first + y * t.second + z) % m);
}

int main() {
    freopen("rsir.in", "r", stdin);
    freopen("rsir.out", "w", stdout);
    int i, len, nSteps, n;

    scanf("%d%d%d%d%d%d%d%d%d", &t0, &t1, &a, &b, &x, &y, &z, &m, &n);

    turtle = make_pair(t0, t1);
    hare = next(turtle);
    nSteps = 0;
    while(turtle != hare) {
        ++ nSteps;
        if(nSteps == n) {
            printf("%d\n", turtle.second);
            return 0;
        }

        turtle = next(turtle);
        hare = next(next(hare));
    }


    len = 0;
    while(turtle != hare) {
        ++ len;

        turtle = next(turtle);
    }

    n = (n - nSteps) % len;
    if(n == 0)
        n = len;

    for(i = 2; i <= n; ++ i)
        turtle = next(turtle);

    printf("%d\n", turtle.second);
    return 0;
}