Cod sursa(job #2030098)

Utilizator danny794Dan Danaila danny794 Data 1 octombrie 2017 04:24:27
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.97 kb
#include <cstdio>

#define MODULO 666013
#define N 3
#define type long long

class Matrix {
 public:
  Matrix& operator*(const Matrix& m) {
    type result[N][N];
    for (type i = 0; i < N; i++) {
      for (type j = 0; j < N; j++) {
        result[i][j] = 0;
        for (type k = 0; k < N; k++) {
          result[i][j] += storage_[i][k] * m.storage_[k][j];
          result[i][j] %= MODULO;
        }
      }
    }
    for (type i = 0; i < N; i++) {
      for (type j = 0; j < N; j++) {
        storage_[i][j] = result[i][j];
      }
    }
    return *this;
  }

  void set(type storage[N][N]) {
    for (type i = 0; i < N; i++) {
      for (type j = 0; j < N; j++) {
        storage_[i][j] = storage[i][j];
      }
    }
  }

  int get(int x, int y, int z) {
    return (storage_[2][0] * x + storage_[2][1] * y + storage_[2][2] * z) % MODULO;
  }

 private:
  type storage_[N][N];
};

Matrix iden, base;

Matrix raiseToPower(int n, const Matrix& m) {
  fflush(stdout);
  if (n == 0) {
    return iden;
  } else if (n == 1) {
    return m;
  } else {
    Matrix half = raiseToPower(n / 2, m);
    half = half * half;
    if (n % 2 == 1) {
      return half * m;
    }
    return half;
  }
}

void setStorageToZero(type storage[N][N]) {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      storage[i][j] = 0;
    }
  }
}

int main() {
  freopen("iepuri.in", "r", stdin);
  freopen("iepuri.out", "w", stdout);
  type storage[N][N];
  setStorageToZero(storage);
  for (int i = 0; i < N; i++) {
    storage[i][i] = 1;
  }
  iden.set(storage);
  int tests, x, y, z, a, b, c, n;
  scanf("%d", &tests);
  while (tests-- > 0) {
    scanf("%d %d %d", &x, &y, &z);
    scanf("%d %d %d", &a, &b, &c);
    scanf("%d", &n);
    setStorageToZero(storage);
    storage[0][1] = 1;
    storage[1][2] = 1;
    storage[2][0] = c;
    storage[2][1] = b;
    storage[2][2] = a;
    base.set(storage);
    Matrix finalMatrix = raiseToPower(n - 2, base);
    printf("%d\n", finalMatrix.get(x, y, z));
  }
  return 0;
}