Cod sursa(job #1561021)

Utilizator redcrocodileIlies Andreea redcrocodile Data 3 ianuarie 2016 16:26:24
Problema Rsir Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <fstream>

using namespace std;

ifstream f("rsir.in");
ofstream g("rsir.out");

typedef pair<int,int> pere;
pere t;

int i,m,x,y,z,ct0[7001],ct1[7001];
long long n,a,b;

void pleaca (pere &i)
{
    int j=i.second;
    i.second=(ct0[i.first]+ct1[i.second]);
    if (i.second>=m) i.second-=m;
    i.first=j;
}
int main()
{
    f>>t.first>>t.second>>a>>b>>x>>y>>z>>m>>n;
    t.first%=m; t.second%=m;
    for (i=0;i<m;i++)
    {
        ct0[i]=(a*i*i+x*i+z)%m;
        ct1[i]=(b*i*i+y*i)%m;
    }
    if (n==1) {g<<t.second; return 0;}
    if (n==0) {g<<t.first; return 0;}
    pere p,q;
    p=q=t;
    int poz=1;
    pleaca(p);
    pleaca(q); pleaca(q);
    while(p!=q)
    {
      if (n==poz) {g<<p.first; return 0;}
      poz++;
      pleaca(p);
      pleaca(q); pleaca(q);
    }
    n%=poz;
    for (i=1;i<=n;i++)
        pleaca(p);
    g<<p.first;
    return 0;
}