Pagini recente » Cod sursa (job #1130368) | Cod sursa (job #3126057) | Cod sursa (job #1298161) | Cod sursa (job #1891184) | Cod sursa (job #1157055)
#include <fstream>
#include <algorithm>
#include <unordered_map>
using namespace std;
int T0, T1, a, b, x, y, z, M;
long long N;
int main()
{
ifstream fin("rsir.in");
ofstream fout("rsir.out");
fin >> T0 >> T1 >> a >> b >> x >> y >> z >> M >> N;
if (N == 0)
{
fout << T0 << '\n';
fin.close();
fout.close();
return 0;
}
if (N == 1)
{
fout << T1 << '\n';
fin.close();
fout.close();
return 0;
}
int limit = min(N, 1LL * M * M + 2);
for (int i = 2; i <= limit; ++i)
{
int aux = (1LL * a * T0 * T0 + b * T1 * T1 + x * T0 + y * T1 + z) % M;
T0 = T1;
T1 = aux;
}
if (N <= M * M + 2)
{
fout << T1 << '\n';
fin.close();
fout.close();
return 0;
}
int iT0 = T0, iT1 = T1, cycle = 0;
while (true)
{
int aux = (1LL * a * T0 * T0 + b * T1 * T1 + x * T0 + y * T1 + z) % M;
T0 = T1;
T1 = aux;
++cycle;
if (T0 == iT0 && T1 == iT1) break;
}
N -= M * M + 2;
N = (N % cycle == 0 ? cycle : N % cycle);
T0 = iT0;
T1 = iT1;
for (int i = 1; i <= N; ++i)
{
int aux = (1LL * a * T0 * T0 + b * T1 * T1 + x * T0 + y * T1 + z) % M;
T0 = T1;
T1 = aux;
}
fout << T1 << '\n';
fin.close();
fout.close();
}