Cod sursa(job #617525)

Utilizator cristianalex81Cristian Alexandru cristianalex81 Data 14 octombrie 2011 23:32:06
Problema Iepuri Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <stdio.h>
#include <stdlib.h>
#define m 666013

unsigned int t,x,y,z,a,b,c,iep;
unsigned long long n;
int mat[3][3],rez[3][3],v[3];

void mat_mult(int m1[3][3], int m2[3][3])
{
    int temp [3][3];
    int i,j,k;
    for (i=0;i<3;i++)
        for (j=0;j<3;j++)
        {
            temp[i][j]=0;
            for (k=0;k<3;k++)
                temp[i][j]=(temp[i][j]+(m1[i][k]*m2[k][j]) % m) % m;
        }
    for (i=0;i<3;i++)
        for (j=0;j<3;j++)
            rez[i][j]=temp[i][j];
}

void mat_ini()
{
        mat[0][0]=a; mat[0][1]=b; mat[0][2]=c;
        mat[1][0]=1; mat[1][1]=0; mat[1][2]=0;
        mat[2][0]=0; mat[2][1]=1; mat[2][2]=0;
}

void mat_pow(int p)
{
    int i,j;
    if (p==1)
    {
        for (i=0;i<3;i++)
            for (j=0;j<3;j++)
                rez[i][j]=mat[i][j];
    }
    else
    {
        mat_pow(p/2);
        mat_mult(rez,rez);
        if (p%2) // daca putere e impara mai adauga la rez mat init;
            mat_mult(rez,mat);
    }
}

int calc_iep()
{
    int j,temp[3]={a,b,c};
    if (n>3)
    {
        mat_pow(n-3);
        a = 0;  b = 0;  c = 0;
        for (j=0;j<3;j++)
            a = (a + (temp[j]*rez[j][0]) % m) % m;
        for (j=0;j<3;j++)
            b = (b + (temp[j]*rez[j][1]) % m) % m;
        for (j=0;j<3;j++)
            c = (c + (temp[j]*rez[j][2]) % m) % m;

    }
    return ((a*z) % m + (b*y) % m + (c*x) % m) % m;
}

int main()
{
    freopen("iepuri.in","r",stdin);
    freopen("iepuri.out","w",stdout);
    scanf("%d",&t);
    int i;
    for(i=0;i<t;i++)
    {
        scanf("%d %d %d %d %d %d %lld",&x,&y,&z,&a,&b,&c,&n);
        mat_ini();
        printf("%d\n",calc_iep());
    }

    return 0;
}