Cod sursa(job #2783191)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 13 octombrie 2021 22:34:17
Problema Rsir Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.27 kb
#include <bits/stdc++.h>
#define NMAX 7005
 
using namespace std;
 
/**********/
// INPUT / OUTPUT
 
ifstream f("rsir.in");
ofstream g("rsir.out");
/**********/
/// GLOBAL DECLARATIONS
long long N;
int T0, T1, A, B, X, Y, Z, M;
int firstSum[NMAX], secondSum[NMAX];
 
 
pair <int, int> hare, tortoise;
/**********/
/// FUNCTIONS
 
void ReadInput();
void Solution();
/**********/
///---------------------------------
inline void ReadInput()
{
    f >> T0 >> T1 >> A >> B >> X >> Y >> Z >> M >> N;
}
///---------------------------------
pair <int, int> Move(pair <int, int> p)
{
    pair <int, int> temp;
    temp.first = p.second;
    temp.second = firstSum[p.first] + secondSum[p.second];
 
    if (temp.second >= M) temp.second -= M;
 
    return temp;
}
///---------------------------------
inline void Solution()
{
    int ans, cycleLength, length = 0;
    pair <int, int> start;
    T0 %= M, T1 %= M, A %= M, B %= M, X %= M, Y %= M, Z %= M;
 
    start = {T0, T1};
    long long sum;
    for (int i = 0 ; i < M ; ++ i)
    {
        sum = 1LL * A * i * i + 1LL * X * i + 1LL * Z;
        sum %= M;
        firstSum[i] = sum;
        sum = 1LL * B * i * i + 1LL * Y * i;
        sum %= M;
        secondSum[i] = sum;
    }
 
    tortoise = start;
    hare = Move(start);
 
    while (hare != tortoise)
    {
        tortoise = Move(tortoise);
        hare = Move(Move(hare));
    }
 
    cycleLength = 1;
    tortoise = Move(tortoise);
 
    while (tortoise != hare)
    {
        tortoise = Move(tortoise);
        cycleLength ++;
    }
 
    hare = start;
    tortoise = start;
 
    for (int i = 1 ; i <= cycleLength ; ++ i)
        hare = Move(hare);
    
    while (hare != tortoise)
    {
        hare = Move(hare);
        tortoise = Move(tortoise);
        length ++;
    }
 
    if (N <= length)
    {
        if (N == 0)
        {
            g << T0;
        }
 
        tortoise = start;
        
        for (int i = 2 ; i <= N ; ++ i)
            tortoise = Move(tortoise);
    }
    else
    {
        int trip = (N - length) % cycleLength;
 
        if (trip == 0) trip = cycleLength;
 
        for (int i = 2 ; i <= trip ; ++ i)
            tortoise = Move(tortoise);
    }
    g << tortoise.second;
 
}  
///---------------------------------
int main()
{
    ReadInput();
    Solution();
    return 0;
}