結果

問題 No.1810 RGB Biscuits
ユーザー se1ka2se1ka2
提出日時 2022-01-14 21:55:02
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 2,987 bytes
コンパイル時間 1,077 ms
コンパイル使用メモリ 84,300 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-20 09:50:39
合計ジャッジ時間 3,262 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <functional>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

const ll MOD = 1000000007;

template <typename T>
struct Matrix
{
    using F = function<T(T, T)>;
    
    int n;
    int m;
    
    vector<vector<T>> a;
    
    const F add;
    const F mul;
    
    const T e0;
    const T e1;
    
    Matrix(
        int n,
        int m,
        const F add = [](T a, T b){return (a + b) % MOD;},
        const F mul = [](T a, T b){return a * b % MOD;},
        const T e0 = 0,
        const T e1 = 1
    ) : n(n), m(m), add(add), mul(mul), e0(e0), e1(e1){
        a.resize(n);
        for(int i = 0; i < n; i++) a[i].resize(m);
    }
    
    Matrix(
        int n,
        const F add = [](T a, T b){return (a + b) % MOD;},
        const F mul = [](T a, T b){return a * b % MOD;},
        const T e0 = 0,
        const T e1 = 1
    ) : n(n), m(n), add(add), mul(mul), e0(e0), e1(e1){
        a.resize(n);
        for(int i = 0; i < n; i++) a[i].resize(m);
    }
    
    static Matrix I(
        int n,
        const F add = [](T a, T b){return (a + b) % MOD;},
        const F mul = [](T a, T b){return a * b % MOD;},
        const T e0 = 0,
        const T e1 = 1
    ){
        Matrix res(n, add, mul, e0, e1);
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++) res.a[i][j] = e0;
        }
        for(int i = 0; i < n; i++) res.a[i][i] = e1;
        return res;
    }
    
    Matrix &operator+=(const Matrix &b){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++) a[i][j] = add(a[i][j], b.a[i][j]);
        }
        return *this;
    }
    
    Matrix &operator*=(const Matrix &b){
        vector<vector<T>> c(n, vector<T>(b.m));
        for(int i = 0; i < n; i++){
            for(int j = 0; j < b.m; j++){
                c[i][j] = e0;
                for(int k = 0; k < m; k++){
                    c[i][j] = add(c[i][j], mul(a[i][k], b.a[k][j]));
                }
            }
        }
        a.swap(c);
        return *this;
    }
    
    Matrix &operator^=(long long k){
        Matrix b = Matrix::I(n, add, mul, e0, e1);
        while(k){
            if(k % 2) b *= *this;
            *this *= *this;
            k /= 2;
        }
        a.swap(b.a);
        return *this;
    }
    
    Matrix operator+(const Matrix &a){
        return (Matrix(*this) += a);
    }
    
    Matrix operator*(const Matrix &a){
        return (Matrix(*this) *= a);
    }
    
    Matrix operator^(const long long k){
        return (Matrix(*this) ^= k);
    }
};

int main()
{
    ll a, b;
    ll n;
    cin >> a >> b >> n;
    Matrix<ll> A(6);
    A.a[0][5] = 1;
    A.a[1][3] = 1;
    A.a[3][0] = 1;
    A.a[4][1] = 1;
    A.a[5][0] = a;
    A.a[5][1] = b;
    A.a[5][2] = 1;
    while(n--){
        ll t;
        cin >> t;
        Matrix<ll> B = A ^ t;
        ll ans = 0;
        for(int i = 0; i < 6; i++) ans = (ans + B.a[i][0] + B.a[i][1]) % MOD;
        cout << ans << endl;
    }
}
0