Pagini recente » Istoria paginii runda/concursceva2 | Cod sursa (job #2783159) | Istoria paginii runda/jc2021-runda1/clasament | Cod sursa (job #2796564) | Cod sursa (job #2783152)
#include <bits/stdc++.h>
#define NMAX 7005
using namespace std;
/**********/
// INPUT / OUTPUT
ifstream f("rsir.in");
ofstream g("rsir.out");
/**********/
/// GLOBAL DECLARATIONS
int T0, T1, A, B, X, Y, Z, M, N;
int firstSum[NMAX], secondSum[NMAX];
int ans, cycleLength, length;
pair <int, int> hare, tortoise;
pair <int, int> temp, start;
/**********/
/// FUNCTIONS
void ReadInput();
void Solution();
void Output();
/**********/
///---------------------------------
inline void ReadInput()
{
f >> T0 >> T1 >> A >> B >> X >> Y >> Z >> M >> N;
}
///---------------------------------
///---------------------------------
pair <int, int> Move(pair <int, int> p)
{
temp.first = p.second;
temp.second = firstSum[p.first] + secondSum[p.second];
if (temp.second >= M)
temp.second -= M;
return temp;
}
///---------------------------------
void PrintPair(pair <int, int> p)
{
g << p.first << " " << p.second << "\n";
}
///---------------------------------
inline void Init()
{
T0 %= M, T1 %= M, A %= M, B %= M, X %= M, Z %= M;
start = {T0, T1};
long long sum = 0;
for (int i = 0 ; i < M ; ++ i)
{
sum = 1LL * A * i * i + 1LL * X * i + Z;
firstSum[i] = sum;
sum = 1LL * B * i * i + 1LL * Y * i;
secondSum[i] = sum;
}
}
///---------------------------------
inline void FindCycleLength()
{
hare = Move(start);
tortoise = start;
while (tortoise != hare)
{
tortoise = Move(tortoise);
hare = Move(Move(hare));
}
do
{
tortoise = Move(tortoise);
cycleLength ++;
} while (tortoise != hare);
}
///---------------------------------
inline void FindLength()
{
pair <int, int> tortoise1, tortoise2;
tortoise1 = start;
tortoise2 = start;
for (int i = 1 ; i <= cycleLength ; ++ i)
tortoise2 = Move(tortoise2);
while (tortoise1 != tortoise2)
{
length ++;
tortoise1 = Move(tortoise1);
tortoise2 = Move(tortoise2);
}
}
///---------------------------------
inline void GetAns()
{
tortoise = start;
if (N < length)
{
for (int i = 2 ; i <= N ; ++ i)
tortoise = Move(tortoise);
}
else
{
for (int i = 2 ; i <= length ; ++ i)
{
tortoise = Move(tortoise);
}
for (int i = 2 ; i <= (N - length) % cycleLength ; ++ i)
{
tortoise = Move(tortoise);
}
}
ans = tortoise.second;
}
///---------------------------------
inline void Solution()
{
Init();
FindCycleLength();
FindLength();
GetAns();
}
///---------------------------------
inline void Output()
{
g << ans;
}
///---------------------------------
int main()
{
ReadInput();
Solution();
Output();
return 0;
}