結果

問題 No.1810 RGB Biscuits
ユーザー se1ka2se1ka2
提出日時 2022-01-14 21:55:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 178 ms / 2,000 ms
コード長 2,987 bytes
コンパイル時間 1,089 ms
コンパイル使用メモリ 84,560 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-30 14:25:41
合計ジャッジ時間 3,112 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 4 ms
6,940 KB
testcase_05 AC 167 ms
6,940 KB
testcase_06 AC 178 ms
6,944 KB
testcase_07 AC 174 ms
6,944 KB
testcase_08 AC 173 ms
6,940 KB
testcase_09 AC 176 ms
6,940 KB
testcase_10 AC 108 ms
6,944 KB
testcase_11 AC 4 ms
6,940 KB
testcase_12 AC 4 ms
6,944 KB
testcase_13 AC 4 ms
6,944 KB
testcase_14 AC 29 ms
6,940 KB
testcase_15 AC 6 ms
6,940 KB
testcase_16 AC 10 ms
6,944 KB
testcase_17 AC 84 ms
6,948 KB
testcase_18 AC 87 ms
6,940 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 19 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

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