Pagini recente » Cod sursa (job #373444) | Cod sursa (job #1921484) | Cod sursa (job #1961587) | Cod sursa (job #2050754) | Cod sursa (job #2967980)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("rsir.in");
ofstream fout("rsir.out");
long long n;
int mod, t0, t1, a, b, x, y, z, start, loop = 1;
int v1[7005], v2[7005];
struct ura {
int curr, urm;
bool operator != (const ura &other) const {
return curr != other.curr || urm != other.urm;
}
}aux0, aux1;
ura nxt(ura x)
{
return {x.urm, (v1[x.urm] + v2[x.curr]) % mod};
}
int main()
{
fin >> t0 >> t1 >> a >> b >> x >> y >> z >> mod >> n;
for(int i = 0; i < mod; i++)
{
v1[i] = ((((i * i) % mod) * b) % mod + (y * i) % mod + z) % mod;
v2[i] = ((((i * i) % mod) * a) % mod + (x * i) % mod) % mod;
}
t0 = t0 % mod;
t1 = t1 % mod;
aux0 = nxt({t0, t1});
aux1 = nxt(nxt({t0, t1}));
while(aux0 != aux1)
{
aux0 = nxt(aux0);
aux1 = nxt(nxt(aux1));
}
aux0 = {t0, t1};
while(aux0 != aux1)
{
aux0 = nxt(aux0);
aux1 = nxt(aux1);
start++;
}
aux1 = nxt(aux0);
while(aux0 != aux1)
{
aux1 = nxt(aux1);
loop++;
}
aux0 = {t0, t1};
if(n > start)
n = start + (n - start) % loop;
for(int i = 1; i <= n; i++)
aux0 = nxt(aux0);
fout << aux0.curr;
return 0;
}