Cod sursa(job #1141679)

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

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

const int MOD = 666013;

struct Matrix {
 int M[3][3];
 int n, m;
 Matrix(int _n=0, int _m=0)
 : n(_n), m(_m) {
  for (int i = 0; i < n; i += 1) {
    for (int j = 0; j < m; j += 1) {
      M[i][j] = 0;
    }
  }
}
};

Matrix log_pow(Matrix &base, int exponent);
Matrix operator*(const Matrix &A, const 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(3, 3);
    M.M[0][1] = M.M[1][2] = 1;
    M.M[2][0] = c;
    M.M[2][1] = b;
    M.M[2][2] = a;

    M = log_pow(M, n - 3);

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

  return 0;
}

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

Matrix operator*(const Matrix &A, const Matrix &B) {
  Matrix result(A.n, B.m);
  for (int i = 0; i < A.n; ++i) {
    for (int j = 0; j < B.m; ++j) {
      for (int k = 0; k < B.n; ++k) {
        result.M[i][j] += mod((1LL * A.M[i][k] * B.M[k][j]), MOD);
      }
    }
  }
  return result;
}

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