Cod sursa(job #386776)

Utilizator remusmpRemus MP remusmp Data 25 ianuarie 2010 22:15:39
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.6 kb
#include <stdio.h>

long long int M[9];
long long int R[9];

long long int REST[9];
long long int RAUX[9];

void afis(long long int* A)
{
     printf("------------\n");
     for (int i = 0; i < 9; i++)
     {
         if (i > 0 && i%3 == 0)
            printf("\n");
         printf("%d ", A[i]);         
     }
}

void matmul(long long int* M1, long long int* M2, long long int* R)
{
    R[0] = (1LL*M1[0]*M2[0]) % 666013 + (1LL*M1[1]*M2[3]) % 666013 + (1LL*M1[2]*M2[6]) % 666013;
    R[1] = (1LL*M1[0]*M2[1]) % 666013 + (1LL*M1[1]*M2[4]) % 666013 + (1LL*M1[2]*M2[7]) % 666013;
    R[2] = (1LL*M1[0]*M2[2]) % 666013 + (1LL*M1[1]*M2[5]) % 666013 + (1LL*M1[2]*M2[8]) % 666013;
    
    R[3] = (1LL*M1[3]*M2[0]) % 666013 + (1LL*M1[4]*M2[3]) % 666013 + (1LL*M1[5]*M2[6]) % 666013;
    R[4] = (1LL*M1[3]*M2[1]) % 666013 + (1LL*M1[4]*M2[4]) % 666013 + (1LL*M1[5]*M2[7]) % 666013;
    R[5] = (1LL*M1[3]*M2[2]) % 666013 + (1LL*M1[4]*M2[5]) % 666013 + (1LL*M1[5]*M2[8]) % 666013;
    
    R[6] = (1LL*M1[6]*M2[0]) % 666013 + (1LL*M1[7]*M2[3]) % 666013 + (1LL*M1[8]*M2[6]) % 666013;
    R[7] = (1LL*M1[6]*M2[1]) % 666013 + (1LL*M1[7]*M2[4]) % 666013 + (1LL*M1[8]*M2[7]) % 666013;
    R[8] = (1LL*M1[6]*M2[2]) % 666013 + (1LL*M1[7]*M2[5]) % 666013 + (1LL*M1[8]*M2[8]) % 666013;
    
    R[0] %= 666013; R[1] %= 666013; R[2] %= 666013;
    R[3] %= 666013; R[4] %= 666013; R[5] %= 666013;
    R[6] %= 666013; R[7] %= 666013; R[8] %= 666013;
    
    /*R[0] = (1LL*M1[0]*M2[0]) + (1LL*M1[1]*M2[3]) + (1LL*M1[2]*M2[6]);
    R[1] = (1LL*M1[0]*M2[1]) + (1LL*M1[1]*M2[4]) + (1LL*M1[2]*M2[7]);
    R[2] = (1LL*M1[0]*M2[2]) + (1LL*M1[1]*M2[5]) + (1LL*M1[2]*M2[8]);
    
    R[3] = (1LL*M1[3]*M2[0]) + (1LL*M1[4]*M2[3]) + (1LL*M1[5]*M2[6]);
    R[4] = (1LL*M1[3]*M2[1]) + (1LL*M1[4]*M2[4]) + (1LL*M1[5]*M2[7]);
    R[5] = (1LL*M1[3]*M2[2]) + (1LL*M1[4]*M2[5]) + (1LL*M1[5]*M2[8]);
    
    R[6] = (1LL*M1[6]*M2[0]) + (1LL*M1[7]*M2[3]) + (1LL*M1[8]*M2[6]);
    R[7] = (1LL*M1[6]*M2[1]) + (1LL*M1[7]*M2[4]) + (1LL*M1[8]*M2[7]);
    R[8] = (1LL*M1[6]*M2[2]) + (1LL*M1[7]*M2[5]) + (1LL*M1[8]*M2[8]);*/
    
}

long long int* compute(int A, int B, int C, int N)
{
    M[0] = 0; M[1] = 1; M[2] = 0;
    M[3] = 0; M[4] = 0; M[5] = 1;
    M[6] = C; M[7] = B; M[8] = A;
    
    for (int i = 0; i < 9; i++)
    {
        REST[i] = 0;
        RAUX[i] = 0;
    }
    REST[0] = 1; REST[4] = 1; REST[8] = 1;
    RAUX[0] = 1; RAUX[4] = 1; RAUX[8] = 1;
    
    long long int *pM = M;
    long long int *pR = R;
    long long int *aux;
    
    long long int *pMRest = REST;
    long long int *pMB = RAUX;
    
    int ct = N-2;
    while (ct > 1)
    {
          if (ct % 2)
          {
                 matmul(pM, pMRest, pMB);
                 aux = pMRest;
                 pMRest = pMB;
                 pMB = aux;
                 afis(pMRest);
          }
          matmul(pM, pM, pR);
          afis(pR);
          
          aux = pM;
          pM = pR;
          pR = aux;
          
          ct = ct / 2;
    }
    
    matmul(pM, pMRest, pR);
    
    return pR;
}

int main()
{
    FILE* fin = fopen("iepuri.in", "r");
    FILE* fout = fopen("iepuri.out", "w");
    
    int T, X, Y, Z, A, B, C, N;
    fscanf(fin, "%d", &T);
    
    for (int i = 0; i < T; i++)
    {
        fscanf(fin, "%d %d %d %d %d %d %d", &X, &Y, &Z, &A, &B, &C, &N);
        long long int* r = compute(A, B, C, N);
        
        long long int rez = r[6]*X + r[7]*Y + r[8]*Z;
        
        fprintf(fout, "%d\n", (int)(rez%666013LL));
    }
    
    fclose(fin);
    fclose(fout);
    
    return 0;
}