Cod sursa(job #3239979)

Utilizator filipinezulBrain Crash filipinezul Data 10 august 2024 17:03:56
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.3 kb
#include <bits/stdc++.h>
using namespace std;

#define MOD 666013
#define DIM 3 // dimensiunea matricei cu coeficienti data de cate cazuri de baza sunt

struct Matrix {
    int64_t mat[DIM][DIM];

    Matrix operator*(const Matrix& other) const {
        Matrix result = {{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}};

        for (int i = 0; i < DIM; ++i) {
            for (int j = 0; j < DIM; ++j) {
                for (int k = 0; k < DIM; ++k) {
                    result.mat[i][j] = (result.mat[i][j] + mat[i][k] * other.mat[k][j] % MOD) % MOD;
                }
            }
        }
        return result;
    }
};

class Task { // O(log n)
 public:
    void solve() {
        read_input();
        get_result();
        print_output();
    }

 private:
    int t;
    vector<int> x, y, z, a, b, c, n;
    vector<int64_t> results;

    void read_input() {
        ifstream fin("iepuri.in");
        fin.tie(0);
        
        fin >> t;
        x.resize(t);
        
        y.resize(t);
        z.resize(t);

        a.resize(t);
        b.resize(t);

        c.resize(t);
        n.resize(t);

        for (int i = 0; i < t; i++) {
            fin >> x[i] >> y[i] >> z[i] >> a[i] >> b[i] >> c[i] >> n[i];
        }
        
        fin.close();
    }

    Matrix matrix_pow(Matrix base, int64_t exp) {
        Matrix result = {{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}};

        while (exp > 0) {
            if (exp % 2 == 1) {
                result = result * base;
            }
            base = base * base;
            exp /= 2;
        }

        return result;
    }

    void get_result() {
        results.reserve(t);

        for (int i = 0; i < t; i ++) {
            Matrix C = {{{0, 1, 0}, {0, 0, 1}, {c[i], b[i], a[i]}}};

            Matrix C_n = matrix_pow(C, n[i] - 2);

            int64_t result = C_n.mat[2][0] * x[i] + C_n.mat[2][1] * y[i] + C_n.mat[2][2] * z[i];

            results.push_back(result % MOD);
        }
    }

    void print_output() {
        ofstream fout("iepuri.out");
        
        for (int i = 0; i < t; i++) {
            fout << results[i] << "\n";
        }

        fout.close();
    }
};

int main() {
    ios::sync_with_stdio(false);
    auto* task = new (nothrow) Task();

    if (!task) {
        cerr << "new failed\n";
        return -1;
    }

    task->solve();
    delete task;

    return 0;
}