Cod sursa(job #1141691)

Utilizator toranagahVlad Badelita toranagah Data 13 martie 2014 01:42:08
Problema Iepuri Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <cstring>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

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

const int MOD = 666013;

typedef int Matrix[3][3];

void log_pow(Matrix base, int P);
void multiply(Matrix A, Matrix B);
int mod(long long x, int mod);
void print_matrix(const Matrix &M);

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

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

    Matrix M;
    memset(M, 0, sizeof(M));
    M[0][1] = M[1][2] = 1;
    M[2][0] = c;
    M[2][1] = b;
    M[2][2] = a;

    log_pow(M, n - 2);

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

  return 0;
}

void log_pow(Matrix M, int P) {
  Matrix b;
  memset(b, 0, sizeof(b));
  b[0][0] = b[1][1] = b[2][2] = 1;

  for (int p = 1; p <= P; p *= 2) {
    if (p & P)
      multiply(b, M);
    multiply(M, M);
  }
  memcpy(M, b, sizeof(b));
}

void multiply(Matrix A, Matrix B) {
  Matrix result;
  memset(result, 0, sizeof(result));
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      for (int k = 0; k < 3; ++k) {
        result[i][j] += mod((1LL * A[i][k] * B[k][j]), MOD);
      }
    }
  }
  memcpy(A, result, sizeof(result));
}

inline int mod(long long x, int m) {
  if (x < m) return x;
  if (x < 2 * m) return x - m;
  return x % m;
}