Cod sursa(job #3229841)

Utilizator denisa0230Zarioiu Denisa denisa0230 Data 17 mai 2024 18:48:07
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <bits/stdc++.h>
using namespace std;

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

void multiply_matrix(int a[3][3], int b[3][3], int c[3][3]) {
    int tmp[3][3];
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            unsigned long long sum = 0;
            for (int k = 0; k < 3; ++k) {
                sum += 1LL * a[i][k] * b[k][j];
            }
            tmp[i][j] = sum;
        }
    }
    memcpy(c, tmp, sizeof(tmp));
}

void exp_matrix(int x[3][3], int exp, int res[3][3]) {
    int tmp[3][3];
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            tmp[i][j] = (i == j) ? 1 : 0;
        }
    }
    while (exp != 1) {
        if (exp % 2 == 0) {
            multiply_matrix(x, x, x);
            exp /= 2;
        } else {
            multiply_matrix(tmp, x, tmp);
            exp--;
        }
    }
    multiply_matrix(tmp, x, res);
}

int main() {
    int x, y, z, a, b, c, n, t;
    fin >> t;
    int mat[3][3], res[3][3];
    for (int i = 1; i <= t; i++) {
        fin >> x >> y >> z >> a >> b >> c >> n;
        for (int j = 0; j < 3; ++j) {
            for (int k = 0; k < 3; ++k) {
                mat[j][k] = 0;
                res[j][k] = 0;
            }
        }
        mat[1][0] = 1;
        mat[2][1] = 1;
        mat[0][2] = c;
        mat[1][2] = b;
        mat[2][2] = a;
        exp_matrix(mat, n-2, res);
        fout << x * res[0][2] + y * res[1][2] + z * res[2][2] << '\n';
    }
    return 0;
}