Cod sursa(job #1836598)

Utilizator mdiannnaMarusic Diana mdiannna Data 28 decembrie 2016 15:16:59
Problema Iepuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.97 kb
#include <iostream>
#include <cstdint>
#include <cmath>
#include <stdio.h>


using namespace std;
typedef long long ll;
void rezultat(ll X, ll Y, ll Z, ll** z){
    for(int i=1; i<4; i++){

    }
   cout << (z[1][1] * Z + z[1][2] * Y + z[1][3]*X) % 666013 << "\n";
}

void afisare_matrice(ll **z){
    for(int i=1; i<4;i++){
        for(int j=1; j<4;j++)
            cout << z[i][j] << " ";
        cout << "\n";
    }
}


ll ** inmultire_matrici(ll **z, ll **x){
    ll** A = (ll**)malloc(3*sizeof(ll*));

    for(int i=1; i<4; i++){
        A[i] = (ll*)malloc(4*sizeof(ll));

        for(int j=1; j<4; j++){
            A[i][j] = 0;
            for(int k=1; k<4; k++)
               A[i][j] += z[i][k] * x[k][j];
        }
    }

    return A;
}


ll ** exponentiere(ll **z, ll P){
    ll** A = (ll**)malloc(3*sizeof(ll*));
    A[1] = (ll*)malloc(3*sizeof(ll));
    A[2] = (ll*)malloc(3*sizeof(ll));

//    afisare_matrice(z);
//    cout << "-----" << endl;

    if(P == 1){
        return z;
    }

    if(P%2 == 0){
        A = inmultire_matrici(z, z);
        return exponentiere(A, P/2);
    }
    else
    {
        A = inmultire_matrici(z, z);
        return inmultire_matrici(exponentiere(A, (P-1)/2), z);
    }
}


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

    ll** z = (ll**)malloc(4*sizeof(ll*));

    z[1] = (ll*)malloc(4*sizeof(ll));
    z[2] = (ll*)malloc(4*sizeof(ll));
    z[3] = (ll*)malloc(4*sizeof(ll));


    z[2][1] = 1;
    z[2][2] = 0;
    z[2][3] = 0;
    z[3][1] = 0;
    z[3][2] = 1;
    z[3][3] = 0;


    ll** R = (ll**)malloc(4*sizeof(ll*));

    R[1] = (ll*)malloc(4*sizeof(ll));
    R[2] = (ll*)malloc(4*sizeof(ll));
    R[3] = (ll*)malloc(4*sizeof(ll));

    ll T, X, Y, Z, A, B, C, N;
    cin >> T;

    for(int i=0; i<T; i++){
        cin >> X >> Y >> Z >> A >> B >> C >> N;

        z[1][1] = A;
        z[1][2] = B;
        z[1][3] = C;


        R = exponentiere(z, N-2);
        rezultat(X, Y, Z, R);

    }

    return 0;
}