Cod sursa(job #2582929)

Utilizator UrsuDanUrsu Dan UrsuDan Data 17 martie 2020 15:25:31
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.67 kb
#include <bits/stdc++.h>
using namespace std;

const int kMod = 666013;

class Task {
 public:
    void solve() {
        ifstream fin("iepuri.in");
        ofstream fout("iepuri.out");
        int matrix[3][3];

        int nr_tasks;
        fin >> nr_tasks;

        for (int i = 0; i < nr_tasks; ++i) {
            int x, y, z, a, b, c, n;
            fin >> x >> y >> z >> a >> b >> c >> n;
            init_matrix(a, b, c, matrix);

            if (n == 0) {
                fout << x << endl;
            } else if (n == 1) {
                fout << y << endl;
            } else if (n == 2) {
                fout << z << endl;
            } else {
                power_matrix(matrix, n - 2);
                fout << get_result(x, y, z, matrix) << endl;
            }
        }

        fin.close();
        fout.close();
    }
 private:
    void init_matrix(int a, int b, int c, int matrix[][3]) {
        matrix[0][0] = a;
        matrix[0][1] = b;
        matrix[0][2] = c;

        matrix[1][0] = 1;
        matrix[1][1] = 0;
        matrix[1][2] = 0;

        matrix[2][0] = 0;
        matrix[2][1] = 1;
        matrix[2][2] = 0;
    }

    void power_matrix(int matrix[][3], int n) {
        if (n == 1 || n == 0) {
            return;
        }

        if (n % 2 == 0) {
            mul_matrix(matrix, matrix);
            power_matrix(matrix, n / 2);
        } else {
            int aux_matrix[3][3];
            copy_matrix(matrix, aux_matrix);

            mul_matrix(matrix, matrix);
            power_matrix(matrix, n / 2);
            mul_matrix(matrix, aux_matrix);
        }
    }

    // the result will be in matrix1
    void mul_matrix(int matrix1[][3], int matrix2[][3]) {
        int aux_matrix[3][3];

        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                aux_matrix[i][j] = 0;
                for (int k = 0; k < 3; ++k) {
                    aux_matrix[i][j] = (aux_matrix[i][j] + 1LL * matrix1[i][k] * matrix2[k][j]) % kMod;
                }
            }
        }

        copy_matrix(aux_matrix, matrix1);
    }

    void copy_matrix(int source[][3], int destination[][3]) {
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                destination[i][j] = source[i][j];
            }
        }
    }

    int get_result(int x, int y, int z, int matrix[][3]) {
        return ((1LL * z * matrix[0][0]) % kMod + (1LL * y * matrix[0][1]) % kMod + (1LL * x * matrix[0][2])) % kMod;
    }
};

int main() {
    // Allocate a Task object on heap in order to be able to
    // declare huge static-allocated data structures inside the class.
    Task *task = new Task();
    task->solve();
    delete task;
    return 0;
}