Cod sursa(job #733556)

Utilizator mihai995mihai995 mihai995 Data 12 aprilie 2012 15:23:21
Problema Rsir Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.42 kb
#include <fstream>
#include <vector>
using namespace std;

const int N=7000,key=66013;
int a[N],b[N],n,mod;
struct Term
{
    int val,poz;
};
vector<Term> hash[key];

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

inline int eval(int x,int y)
{
    return a[x]+b[y]-mod*(a[x]+b[y]>=mod);
}

inline int calc(long long x,int a,int b,int c)
{
    return (a*x*x+b*x+c)%mod;
}

int use(int A,int B)
{
    int x=A+B*mod;
    vector<Term>& v=hash[x%key];
    for (vector<Term>::iterator i=v.begin();i!=v.end();i++)
        if ((*i).val==x)
            return (*i).poz;
    return 0;
}

void add(int A,int B,int i)
{
    int x=A+B*mod;
    hash[x%key].push_back((Term){x,i});
}

int get(int val)
{
    for (int x=0;x<key;x++)
        for (vector<Term>::iterator i=hash[x].begin();i!=hash[x].end();i++)
            if ((*i).poz==val)
                return eval((*i).val % mod,(*i).val / mod);
    return 0;
}

int main()
{
    int i,t0,t1,A,B,C,x,y,z;
    in>>t0>>t1>>A>>B>>x>>y>>z>>mod>>n;
    for (int i=0;i<mod;i++)
    {
        a[i]=calc(i,A,x,z);
        b[i]=calc(i,B,y,0);
    }
    A=t0%mod;
    B=t1%mod;
    for (i=1;i<n && !use(A,B);i++)
    {
        add(A,B,i);
        C=eval(A,B);
        A=B;
        B=C;
    }
    if (i==n)
    {
        out<<B<<"\n";
        return 0;
    }
    x=use(A,B);
    y=i;
    n=(n-x)%(y-x);
    out<<get(n)<<"\n";
    return 0;

}