Cod sursa(job #1740215)

Utilizator cella.florescuCella Florescu cella.florescu Data 11 august 2016 11:00:27
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <cstdio>
#define DIM 3
#define MOD 666013

int mat[DIM][DIM], sol[DIM][DIM];

inline void mymult(int n, int a[][DIM], int b[][DIM], int c[][DIM]){
  int i, j, k;
  for(i=0; i<n; i++)
    for(j=0; j<n; j++){
      c[i][j]=0;
      for(k=0; k<n; k++)
        c[i][j]=(c[i][j]+1LL*a[i][k]*b[k][j])%MOD;
    }
}

inline void mycopy(int n, int d[][DIM], int s[][DIM]){
  int i, j;
  for(i=0; i<n; i++)
    for(j=0; j<n; j++)
      d[i][j]=s[i][j];
}

inline void inv(int n, int in[][DIM]){
  int i, j;
  for(i=0; i<n; i++){
    for(j=0; j<n; j++)
      in[i][j]=0;
    in[i][i]=1;
  }
}

inline void mypow(int n, int expo, int mat[][DIM], int rez[][DIM]){
  int aux[DIM][DIM];
  inv(DIM, rez);
  while(expo){
    if(expo&1){
      mymult(DIM, mat, rez, aux);
      mycopy(DIM, rez, aux);
    }
    mymult(DIM, mat, mat, aux);
    mycopy(DIM, mat, aux);
    expo>>=1;
  }
}

int main()
{
    FILE *fin, *fout;
    int t, i, x, y, z, a, b, c, n;
    fin=fopen("iepuri.in", "r");
    fscanf(fin, "%d", &t);
    fout=fopen("iepuri.out", "w");
    for(i=0; i<t; i++){
      fscanf(fin, "%d%d%d%d%d%d%d", &x, &y, &z, &a, &b, &c, &n);
      mat[0][0]=a; mat[0][1]=b; mat[0][2]=c; mat[1][0]=mat[2][1]=1; mat[1][1]=mat[1][2]=mat[2][0]=mat[2][2]=0;
      mypow(DIM, n-2, mat, sol);
      fprintf(fout, "%lld\n", (1LL*z*sol[0][0]+1LL*y*sol[0][1]+1LL*x*sol[0][2])%MOD);
    }
    fclose(fin);
    fclose(fout);
    return 0;
}