Cod sursa(job #2789240)

Utilizator Asgari_ArminArmin Asgari Asgari_Armin Data 27 octombrie 2021 11:01:59
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <bits/stdc++.h>
#define int long long

using namespace std;

ifstream fin( "iepuri.in" );
ofstream fout( "iepuri.out" );

const int MMAX = 5;
const int MOD = 666013;

struct matrix{
  int mat[MMAX + 2][MMAX + 2];
  int l, c;
};

matrix Multiply( matrix A, matrix B ){
  int i, j, k;
  matrix M;

  for( i = 1; i <= A.l; ++i )
    for( j = 1; j <= B.c; ++j )
      M.mat[i][j] = 0;
  M.l = A.l; M.c = B.c;

  for( i = 1; i <= A.l; ++i )
    for( j = 1; j <= B.c; ++j )
      for( k = 1; k <= A.c; ++k )
        M.mat[i][j] = (M.mat[i][j] + (A.mat[i][k] * B.mat[k][j]) % MOD) % MOD;
  return M;
}

matrix LgPut( matrix A, int b ){
  matrix P, P1, A1;
  P.l = P.c = 3;
  P.mat[1][1] = 1; P.mat[1][2] = 0; P.mat[1][3] = 0;
  P.mat[2][1] = 0; P.mat[2][2] = 1; P.mat[2][3] = 0;
  P.mat[3][1] = 0; P.mat[3][2] = 0; P.mat[3][3] = 1;

  while( b > 0 ){
    if( b % 2 == 1 ) {
      P1 = Multiply(P, A);
      P = P1;
    }

    A1 = Multiply(A, A);
    A = A1;
    b /= 2;
  }
  return P;
}

signed main() {
  int x, y, z, a, b, c, n, t;

  fin >> t;
  while( t-- ) {
    fin >> x >> y >> z >> a >> b >> c >> n;

    matrix A, B, B1;
    A.l = 1; A.c = 3;
    A.mat[1][1] = x; A.mat[1][2] = y; A.mat[1][3] = z;

    B.l = B.c = 3;
    B.mat[1][1] = 0; B.mat[1][2] = 0; B.mat[1][3] = c;
    B.mat[2][1] = 1; B.mat[2][2] = 0; B.mat[2][3] = b;
    B.mat[3][1] = 0; B.mat[3][2] = 1; B.mat[3][3] = a;

    B1 = LgPut(B, n - 2); B = B1;
    B1 = Multiply(A, B);

    fout << B1.mat[1][3] << "\n";
  }
  return 0;
}