Cod sursa(job #2177676)

Utilizator MihaelaCismaruMihaela Cismaru MihaelaCismaru Data 18 martie 2018 19:08:20
Problema Rsir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.39 kb
#include<fstream>
#include<map>
using namespace std;
ifstream in ("rsir.in");
ofstream out ("rsir.out");
long long a,b,c,d,A,B,X,Y,Z,n,mod,aux,alfa,beta,ciclu,coada,s;
map<pair<long long,int>,bool> hz;
int main (void) {
    in >> a >> b >> A >> B >> X >> Y >> Z >> mod >> n;
    n ++;
    if (n == 1) {
        out << a;
        return 0;

    }
    if (n == 2) {
        out << b;
        return 0;
    }
    c = a; d = b;
    hz [{c,d}] = 1;
    while (true) {
        ciclu++;
        hz[{c,d}] = 1;
        aux = (A * c*c + B * d*d + X * c + Y * d + Z) % mod;
        if (hz[{d,aux}] == 1) {
            alfa = d;
            beta = aux;
            break;
        }
        c = d;
        d = aux;
    }
    c = a;
    d = b;
    coada = 2;
    while (true) {
        coada++;
        aux = (A * c*c + B * d*d + X * c + Y * d + Z) % mod;
        if (n == coada) {
            out << aux;
            return 0;
        }
        if (alfa == d && beta == aux) {
            break;
        }
        c = d;
        d = aux;
    }
    coada -= 2;

    ciclu -= coada;
    n -= coada;
    n %= ciclu;
    c = alfa;
    b = beta;
    while (true) {
        s++;
        aux = (A * c*c + B * d*d + X * c + Y * d + Z) % mod;
        if (s == n) {
            out << aux;
            return 0;
        }
        c = d;
        d = aux;
    }
    return 0;
}