Cod sursa(job #1784855)

Utilizator Mircea_DonciuDonciu Mircea Mircea_Donciu Data 20 octombrie 2016 16:19:01
Problema Iepuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <fstream>

using namespace std;
struct mat
{
    int w[3][3];
};
mat p,q;
long t,n,x,y,z,a,b,c,i,j,k,su;
mat inm (mat s, mat t)
{
    mat rez;
    for(i=0; i<3; i++)
    for(j=0; j<3; j++)
    {
        rez.w[i][j]=t.w[i][0]*s.w[0][j]+t.w[i][1]*s.w[1][j]+t.w[i][2]*s.w[2][j];
    }
    return rez;
}
mat rest(mat s)
{
    for(i=0; i<3; i++)
    for(j=0; j<3; j++)
    s.w[i][j]%=666013;
    return s;
}
int main()
{
    ifstream f("iepuri.in");
    ofstream g("iepuri.out");
    f>>t;
    while(t)
    {
        t--;
        f>>x>>y>>z>>a>>b>>c>>n;
        for(i=0; i<3; i++)
        for(j=0; j<3; j++)
        {
            p.w[i][j]=0;
            q.w[i][j]=0;
        }
        q.w[0][1]=1;
        q.w[1][2]=1;
        q.w[2][0]=c;
        q.w[2][1]=b;
        q.w[2][2]=a;
        while(n%2==0)
        {
            q=inm(q,q);
            n/=2;
            q=rest(q);
        }
        p=q;
        q=inm(q,q);
        n/=2;
        while(n)
        {
            if(n%2) p=inm(p,q);
            q=inm(q,q);
            p=rest(p);
            q=rest(q);
            n/=2;
        }
        g<<(p.w[0][0]*x+p.w[0][1]*y+p.w[0][2]*z)%666013<<'\n';
    }
    f.close(); g.close();
    return 0;
}