Cod sursa(job #2485583)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 1 noiembrie 2019 19:41:26
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <bits/stdc++.h>

using namespace std;

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

const int mod = 666013;

struct Matrix
{
	int n, m;
	vector <vector <int> > v;
	
	Matrix(int _n, int _m)
	{
		n = _n;
		m = _m;
		
		v.resize(n, vector <int> (m, 0));
	}
	
	Matrix multiply(Matrix aux)
	{
		Matrix res(n, aux.m);
		
		for(int i = 0; i < n; i++)
			for(int j = 0; j < aux.m; j++)
				for(int t = 0; t < m; t++)
					res.v[i][j] = (res.v[i][j] + (v[i][t] * 1LL * aux.v[t][j]) % mod) % mod;
		
		return res;
	}
	
	void Fill(int A, int B, int C)
	{
		v[0][2] = C;
		v[1][0] = 1;
		v[1][2] = B;
		v[2][1] = 1;
		v[2][2] = A;
	}
};

Matrix logpow(Matrix x, int n)
{
	Matrix res(3, 3);
	
	res.v[0][0] = 1;
	res.v[1][1] = 1;
	res.v[2][2] = 1;
	
	while(n)
	{
		if(n & 1)
		{
			res = res.multiply(x);
		}
		
		n >>= 1;
		
		x = x.multiply(x);
	}
	
	return res;
}

main()
{
	int t;
	fin >> t;
	
	while(t--)
	{
		int x, y, z, a, b, c, n;
		fin >> x >> y >> z >> a >> b >> c >> n;
		
		Matrix magic(3, 3);
		magic.Fill(a, b, c);
		
		Matrix initial(1, 3);
		
		initial.v[0][0] = x;
		initial.v[0][1] = y;
		initial.v[0][2] = z;
		
		magic = logpow(magic, n - 2);
		
		Matrix res = initial.multiply(magic);
		
		fout << res.v[0][2] << '\n';
	}
}