Cod sursa(job #1784840)

Utilizator Mircea_DonciuDonciu Mircea Mircea_Donciu Data 20 octombrie 2016 15:59:07
Problema Iepuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 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]=s.w[i][0]*t.w[0][j]+s.w[i][1]*t.w[1][j]+s.w[i][2]*t.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;
        n-=2;
        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[2][0]*x+p.w[2][1]*y+p.w[2][2]*z)%666013<<'\n';
    }
    f.close(); g.close();
    return 0;
}