Cod sursa(job #1387739)

Utilizator tudor00Stoiean Tudor tudor00 Data 14 martie 2015 17:19:46
Problema Iepuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.12 kb
#include <iostream>
#include <fstream>

using namespace std;

int mat[4][4];

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

inline void ExpLog(int n)
{
    /// mat = mat^n
    int put[4][4], aux[4][4];
    long long nr;
    int i, j, k;
    for (i=1; i<=3; i++)
        for (j=1; j<=3; j++)
            put[i][j] = 0;
    put[1][1] = put[2][2] = put[3][3] = 1;
    while (n>0)
    {
        if (n&1)
        {
            for (i=1; i<=3; i++) // liniile din prima
            {
                for (j=1; j<=3; j++) // coloanele din a 2a
                {
                    nr = 0LL;
                    for (k=1; k<=3; k++)
                        nr += 1LL * put[i][k] * mat[k][j];
                    aux[i][j] = nr%666013;
                }
            }
            for (i=1; i<=3; i++)
                for (j=1; j<=3; j++)
                    put[i][j] = aux[i][j];
            //put = put*mat;
            n--;
        }
        for (i=1; i<=3; i++) // liniile din prima
        {
            for (j=1; j<=3; j++) // coloanele din a 2a
            {
                nr = 0LL;
                for (k=1; k<=3; k++)
                    nr += 1LL * mat[i][k] * mat[k][j];
                aux[i][j] = nr%666013;
            }
        }
        for (i=1; i<=3; i++)
            for (j=1; j<=3; j++)
                mat[i][j] = aux[i][j];
        //mat = mat*mat;
        n>>=1;
    }
    for (i=1; i<=3; i++)
        for (j=1; j<=3; j++)
            mat[i][j] = put[i][j];
}

inline long long Solve(int x, int y, int z, int a, int b, int c, int n)
{
    int i, j;
    for (i=1; i<=3; i++)
        for (j=1; j<=3; j++)
            mat[i][j] = 0;
    mat[1][2] = mat[2][3] = 1;
    mat[3][1] = c, mat[3][2] = b, mat[3][3] = a;
    ExpLog(n);
    long long ans;
    ans = ((1LL*mat[1][1]*x) + (1LL*mat[1][2]*y) + (1LL*mat[1][3]*z))%666013;
    return ans;
}

int main()
{
    int t, x, y, z, a, b, c, n;
    in>>t;
    for(i=1; i<=t; i++)
    {
        in>>x>>y>>z>>a>>b>>c>>n;
        out<<Solve(x, y, z, a, b, c, n)<<"\n";
    }

    f.close();
    g.close();
    return 0;
}