Cod sursa(job #1141701)

Utilizator toranagahVlad Badelita toranagah Data 13 martie 2014 01:53:37
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.1 kb
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

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

const int MOD = 666013;

typedef vector<vector<int>> Matrix;

Matrix log_pow(Matrix base, int P);
Matrix operator*(const Matrix &a, const Matrix &b);

int main() {
  int t;
  fin >> t;

  int x, y, z, a, b, c, n;
  Matrix M, result;
  for (int i = 1; i <= t; ++i) {
    fin >> x >> y >> z >> a >> b >> c >> n;
    M = {{0, 1, 0},
         {0, 0, 1},
         {c, b, a}};

    M = log_pow(M, n - 3);

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

  return 0;
}

Matrix log_pow(Matrix base, int P) {
  Matrix result = base;
  for (; P; P >>= 1) {
    if (P & 1)
      result = result * base;
    base = base * base;
  }
  return result;
}

Matrix operator*(const Matrix &a, const Matrix &b) {
  Matrix result(3, vector<int>(3));
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 3; ++k) {
        result[i][j] += (1LL * a[i][k] * b[k][j]) % MOD;
      }
    }
  }
  return result;
}