Cod sursa(job #2783139)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 13 octombrie 2021 20:50:24
Problema Rsir Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.75 kb
#include <bits/stdc++.h>

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 ans, cycleLength, length;

pair <int, int> hare, tortoise;
pair <int, int> temp;
/**********/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/**********/
///---------------------------------
inline void ReadInput()
{
    f >> T0 >> T1 >> A >> B >> X >> Y >> Z >> M >> N;
}
///---------------------------------
bool Different(pair <int, int> p1, pair <int, int> p2)
{
    if (p1.first == p2.first)
        return p1.second != p2.second;
    return true;
}
///---------------------------------
int Calc(pair <int, int> p)
{
    return ((A * p.first * p.first) % M + (B * p.second * p.second) % M + (X * p.first) % M + (Y * p.second) % M + Z) % M;
}
///---------------------------------
pair <int, int> Move(pair <int, int> p)
{
    temp = {p.second, Calc(p)};

    return temp;
}
///---------------------------------
void PrintPair(pair <int, int> p)
{
    g << p.first << " " << p.second << "\n";
}
///---------------------------------
inline void Init()
{
    hare = make_pair(T0 % M, T1 % M);
    tortoise = make_pair(T0 % M, T1 % M);
}
///---------------------------------
inline void FindCycleLength()
{
    while (Different(tortoise, hare))
    {
        tortoise = Move(tortoise);
        hare = Move(Move(hare));
    }

    do
    {
        tortoise = Move(tortoise);
        cycleLength ++;    
    } while (Different(tortoise, hare));
}
///---------------------------------
inline void FindLength()
{
    pair <int, int> tortoise1, tortoise2;

    tortoise1 = {T0 % M, T1 % M};
    tortoise2 = {T0 % M, T1 % M};

    for (int i = 1 ; i <= cycleLength ; ++ i)
        tortoise2 = Move(tortoise2);
    
    while (Different(tortoise1, tortoise2))
    {
        length ++;
        tortoise1 = Move(tortoise1);
        tortoise2 = Move(tortoise2);
    }
}
///---------------------------------
inline void GetAns()
{
    tortoise = {T0 % M, T1 % M};
     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;
}