Cod sursa(job #2415635)

Utilizator Stefan_RaduStefan Radu Stefan_Radu Data 26 aprilie 2019 13:05:47
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.49 kb
// By Stefan Radu

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <string>
#include <cctype>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define sz(x) (int)(x).size()

typedef pair < int, int > pii;
typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef vector < vector < int > > vvi;

ifstream cin("iepuri.in");
ofstream cout("iepuri.out");

const int MOD = 666013;

vvi mat;

void res(int a, int b, int c) {
  mat = {{0, 0, c},
       {1, 0, b},
       {0, 1, a}};
}

vvi mult(vvi a, vvi b, int n) {

  vvi ret(3, vector < int > (3));
  for (int i = 0; i < n; ++ i) {
    for (int j = 0; j < n; ++ j) {
      for (int k = 0; k < n; ++ k) {
        ret[i][j] = (1ll * ret[i][j] + 1ll * a[i][k] * b[k][j]) % MOD; 
      }
    }
  }

  return ret;
}

vvi raise(vvi k, int b) {

  vvi ans = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
  while (b) {

    if (b & 1) ans = mult(ans, k, 3);

    k = mult(k, k, 3);
    b >>= 1;
  }

  return ans;
}

int main() {

#ifdef STEF
  freopen("input", "r", stdin);
  freopen("output", "w", stdout);
#endif

  ios::sync_with_stdio(false);
  cin.tie(0);cout.tie(0);

  int t;
  cin >> t;

  while (t --) {

    ll x, y, z, a, b, c, n;
    cin >> x >> y >> z >> a >> b >> c >> n;

    res(a, b, c);

    mat = raise(mat, n);

    cout << (x * mat[0][0] + y * mat[1][0] + z * mat[2][0]) % MOD << '\n';
  }
}