Cod sursa(job #2310615)

Utilizator Data 1 ianuarie 2019 18:07:45 Rsir 50 cpp-64 done Arhiva de probleme 1.78 kb
``````#include <iostream>
#include <fstream>
#include <unordered_map>

using namespace std;

ifstream in ("rsir.in");
ofstream out ("rsir.out");

#define ll long long
#define MIN(a , b) (((a) < (b)) ? (a) : (b))
#define MAX(a , b) (((a) < (b)) ? (b) : (a))

int const nmax = 7000;

unordered_map<int , int> frec;

int firstel[5 + nmax];
int secel[5 + nmax];

int main()
{
int t0 , t1 , a , b , x , y , z , modulo ;
ll n;
in >> t0 >> t1 >> a >> b >> x >> y >> z >> modulo >> n;
t0 %= modulo;
t1 %= modulo;
for(int i = 0 ; i <= modulo ; i++)
firstel[i] = (a * i % modulo * i % modulo + x * i % modulo) % modulo;
for(int i = 0 ; i <= modulo ; i++)
secel[i] = (b * i % modulo * i % modulo + y * i % modulo) % modulo;

int initt0 = t0, initt1 = t1;

int lim = 1 , val = 0;
while(true){
//cout << t0 << " " << t1 << '\n';

if(frec[t0 * modulo + t1] == 1) {
val = t0 * modulo + t1;
break;
}
frec[t0 * modulo + t1] = 1;

t0 = firstel[t0] + secel[t1] + z;
if(modulo <= t0)
t0 -= modulo;
if(modulo <= t0)
t0 -= modulo;

swap(t0 , t1);
lim++;
}

t0 = initt0;
t1 = initt1;

int start = 1;

while(true){
if(t0 * modulo + t1 == val)
break;

t0 = firstel[t0] + secel[t1] + z;
if(modulo <= t0)
t0 -= modulo;
if(modulo <= t0)
t0 -= modulo;

swap(t0 , t1);
start++;
}

//cout << start << " " << lim << '\n';

if(start <= n)
n = (n - start) % (lim - start) + start;

t0 = initt0;
t1 = initt1;
for(int i = 2 ; i <= n ; i++){
t0 = firstel[t0] + secel[t1] + z;
if(modulo <= t0)
t0 -= modulo;
if(modulo <= t0)
t0 -= modulo;

swap(t0 , t1);
}
out << t1;

return 0;
}
``````