Cod sursa(job #3330208)

Utilizator n6v26rDedu Razvan Matei n6v26r Data 18 decembrie 2025 02:01:52
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
// NAME: Iepuri
// SOURCE: infoarena.ro
// URL: https://www.infoarena.ro/problema/iepuri
#include <cstring>
#include <stdio.h>

#define MOD 666013

#define LIN 3
#define COL 3

typedef long long ll;

struct Mat {
  int mat[LIN * COL];
  int lin = LIN;
  int col = COL;

  int *operator[](int idx) const { return ((int *)mat) + col * idx; }

  Mat() { memset(mat, 0, sizeof(mat)); }

  Mat(int *v, int l = LIN, int c = COL) {
    memcpy(this->mat, v, sizeof(int) * l * c);
    this->lin = l;
    this->col = c;
  }

  void operator=(const Mat &other) {
    memcpy(this->mat, other.mat, sizeof(int) * LIN * COL);
    this->lin = other.lin;
    this->col = other.col;
  }

  friend Mat operator*(const Mat &a, const Mat &b) {
    Mat res;
    res.lin = a.lin;
    res.col = b.col;
    for (int i = 0; i < a.lin; i++) {
      for (int j = 0; j < b.col; j++) {
        for (int k = 0; k < a.col; k++) {
          res[i][j] = (res[i][j] + ((ll)a[i][k] * b[k][j])) % MOD;
        }
      }
    }
    return res;
  }
};

Mat fastexp(Mat b, int e) {
  Mat p = b;
  while (e) {
    if (e % 2)
      p = p * b;
    b = b * b;
    e /= 2;
  }
  return p;
}

int t;

int main() {
  freopen("iepuri.in", "r", stdin);
  freopen("iepuri.out", "w", stdout);

  scanf("%d", &t);
  while (t) {
    int x, y, z, a, b, c, n;
    scanf("%d%d%d%d%d%d%d", &x, &y, &z, &a, &b, &c, &n);

    int matrix[] = {0, 0, c, 1, 0, b, 0, 1, a};
    int start[] = {x, y, z};

    printf("%d\n", ((Mat){start, 1, 3}*fastexp({matrix}, n-3)).mat[2]);
    t--;
  }
  return 0;
}