Pagini recente » Cod sursa (job #213213) | Cod sursa (job #1017609)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int mod = 666013;
typedef long long Matrix[4][4];
int T, N, A, B, C, X, Y, Z;
inline void mult( Matrix A, Matrix B )
{
Matrix aux;
memset( aux, 0, sizeof ( aux ) );
for ( int k = 1; k <= 3; ++k )
for ( int i = 1; i <= 3; ++i )
for ( int j = 1; j <= 3; ++j )
aux[i][j] = ( aux[i][j] + A[i][k] * B[k][j] * 1LL ) % mod;
memcpy( A, aux, sizeof ( aux ) );
}
int power()
{
N -= 2;
Matrix SOL, I;
memset ( SOL, 0, sizeof ( SOL ) );
memset ( I, 0, sizeof ( I ) );
SOL[1][2] = SOL[2][3] = 1;
SOL[3][1] = C; SOL[3][2] = B; SOL[3][3] = A;
I[1][1] = 1;
I[2][2] = 1;
I[3][3] = 1;
for ( int i = 0; ( 1LL << i ) <= N; ++i )
{
if ( N & ( 1LL << i ) )
{
mult( I, SOL );
}
mult( SOL, SOL );
}
return ( I[3][1] * X + I[3][2] * Y + I[3][3] * Z ) % mod;
}
inline void scan( int &n )
{
n = 0;
int ch = getchar();
int sign = 1;
while ( ch < '0' || ch > '9' )
{
if ( ch == '-')
sign = -1;
ch = getchar();
}
while ( ch >= '0' && ch <= '9' )
{
n = ( n << 3 ) + ( n << 1) + ch - '0';
ch = getchar();
}
n = n * sign;
}
int main()
{
freopen("iepuri.in", "r", stdin);
freopen("iepuri.out", "w", stdout);
for ( scan( T ); T; T-- )
{
scan( X );
scan( Y );
scan( Z );
scan( A );
scan( B );
scan( C );
scan( N );
printf("%d\n", power());
}
return 0;
}