Cod sursa(job #1040766)

Utilizator AnonymouslegionAnonymous Anonymouslegion Data 24 noiembrie 2013 21:48:33
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

class matrix{
public:
  matrix(){
    _kmod = 666013;
  }

  matrix(int n, int m){
    _kmod = 666013;
    for(int i = 1; i <= n; ++i){
      vector<long long> aux;
      _mat.push_back(aux);
      for(int j = 1; j <= m; ++j)
        _mat[i - 1].push_back(0);
    }
  }

  void add(int x, int y, long long v){
    _mat[x][y] = (_mat[x][y] + v) % _kmod;
  }

  int col(){
    return _mat[0].size();
  }

  int lin(){
    return _mat.size();
  }

  long long getv(int x, int y){
    return _mat[x][y];
  }

  matrix operator * (matrix B){
    matrix C(lin(), B.col());

    for(int i = 0; i < C.lin(); ++i)
      for(int j = 0; j < C.col(); ++j)
        for(int k = 0; k < col(); ++k)
          C.add(i, j, getv(i, k) * B.getv(k, j));

    return C;
  }
private:
  int _kmod;
  vector<vector<long long> > _mat;
};

matrix pow(matrix base, long long exp){
  if(exp == 1)
    return base;
  matrix aux = pow(base, exp / 2);
  aux = aux * aux;
  if(exp % 2)
    aux = aux * base;
  return aux;
}

int main(){
  ifstream in("iepuri.in");
  ofstream out("iepuri.out");

  int t;
  in >> t;
  while(t--){
    int a, b, c, x, y, z, n;

    in >> x >> y >> z >> a >> b >> c >> n;

    matrix gv(1, 3);
    gv.add(0, 0, x);
    gv.add(0, 1, y);
    gv.add(0, 2, z);

    matrix chg(3, 3);
    chg.add(1, 0, 1);
    chg.add(2, 1, 1);
    chg.add(0, 2, c);
    chg.add(1, 2, b);
    chg.add(2, 2, a);

    matrix tomul = pow(chg, n);
    matrix ans = gv * tomul;

    out << ans.getv(0, 0) << "\n";
  }

  return 0;
}