結果

問題 No.1810 RGB Biscuits
ユーザー nawawannawawan
提出日時 2022-01-14 22:01:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 6,218 bytes
コンパイル時間 5,002 ms
コンパイル使用メモリ 215,032 KB
実行使用メモリ 13,212 KB
最終ジャッジ日時 2023-08-12 19:37:14
合計ジャッジ時間 3,548 ms
ジャッジサーバーID
(参考情報)
judge2 / judge9
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
12,972 KB
testcase_01 AC 6 ms
12,972 KB
testcase_02 AC 6 ms
13,004 KB
testcase_03 AC 6 ms
13,000 KB
testcase_04 AC 6 ms
12,976 KB
testcase_05 AC 27 ms
12,936 KB
testcase_06 AC 30 ms
12,936 KB
testcase_07 AC 29 ms
13,212 KB
testcase_08 AC 29 ms
12,988 KB
testcase_09 AC 29 ms
12,972 KB
testcase_10 AC 20 ms
12,980 KB
testcase_11 AC 7 ms
12,976 KB
testcase_12 AC 6 ms
13,000 KB
testcase_13 AC 6 ms
13,000 KB
testcase_14 AC 10 ms
12,980 KB
testcase_15 AC 6 ms
13,028 KB
testcase_16 AC 7 ms
12,964 KB
testcase_17 AC 18 ms
13,208 KB
testcase_18 AC 18 ms
12,972 KB
testcase_19 AC 6 ms
12,940 KB
testcase_20 AC 5 ms
12,932 KB
testcase_21 AC 8 ms
12,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<const int MOD> struct modint{
    long long val;
    modint(): val(0) {}
    modint(long long x){
        if(x < 0) val = x % MOD + MOD;
        else val = x % MOD;
    }
    modint(const modint &t){
        val = t.val;
    }
    modint& operator =(const modint m){
        val = m.val;
        return *this;
    }
    modint operator -(){
        return modint(-val);
    }
    modint& operator-=(const modint &m){
        val -= m.val;
        if(val < 0) val += MOD;
        return *this;
    }
    modint& operator+=(const modint &m){
        val += m.val;
        if(val >= MOD) val -= MOD;
        return *this;
    }
    modint& operator*=(const modint &m){
        val *= m.val;
        val %= MOD;
        return *this;
    }
    modint& operator/=(modint m){
        *this *= m.inv();
        return *this;
    }
    modint inv(){
        long long x = 1, y = 0;
        long long a = val, b = MOD;
        while(b != 0){
            long long t = a / b;
            a -= t * b;
            x -= t * y;
            swap(a, b);
            swap(x, y);
        }
        x %= MOD;
        if(x < 0) x += MOD;
        return modint(x);
    }
    modint pow(long long k){
        long long res = 1;
        long long v = val;
        if(v == 0) return modint(0);
        while(k > 0){
            if(k & 1) res = res * v % MOD;
            v = v * v % MOD;
            k >>= 1;
    }
        return modint(res);
    }
    bool operator==(const modint &m){
        return val == m.val;
    }
    modint operator+(const modint &m){
        return modint(*this) += m;
    }
    modint operator-(const modint &m){
        return modint(*this) -= m;
    }
    modint operator*(const modint &m){
        return modint(*this) *= m;
    }
    modint operator/(const modint &m){
        return modint(*this) /= m;
    }
    bool operator!=(const modint &m){
        return modint(*this).val != m.val;
    }
    bool operator!=(const int &m){
        return modint(*this).val != m;
    }
};
const int MOD = 1000000007;
using mint = modint<MOD>;
const int MAX = 410000;
mint fac[MAX], finv[MAX], inv[MAX];
//MAX < MOD
void COMinit(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i;
        inv[i] = mint(MOD) - inv[MOD % i] * (MOD / i);
        finv[i] = finv[i - 1] * inv[i];
    }
}
mint COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * finv[k] * finv[n - k];
}
template<typename T> struct mat{
    vector<vector<T>> matrix;//行列
    int N, M;
    mat(){}
    mat(int n, int m): N(n), M(m){
        matrix.resize(n, vector<T>(m));
    }
    mat(vector<vector<T>> m) : N(m.size()), M(m[0].size()), matrix(m){}
    vector<T> operate(vector<T> &vec){//ベクトルにかける
        assert(vec.size() == M);
        vector<T> res(N);
        for(int i = 0; i < N; i++){
            for(int j = 0; j < M; j++) res[i] += (*this)[i][j] * vec[j];
        }
        return res;
    }
    mat I(int N){
        mat m(N, N);
        for(int i = 0; i < N; i++) m[i][i] = 1;
        return (m);
    }
    mat& pow(long long K){
        assert(N == M);//正方行列である必要あり
        mat temp(I(N));
        while(K > 0){
            if(K & 1) temp *= *this;
            *this *= *this;
            K >>= 1;
        }
        matrix.swap(temp.matrix);
        return *this;
    }
    const vector<T> &operator[](int i) const{
        return matrix.at(i);
    }
    vector<T>& operator[](int i){
        return matrix.at(i);
    }
    mat& operator*=(const mat &mat2){
        vector<vector<T>> res(N, vector<T>(M));
        for(int i = 0; i < N; i++){
            for(int j = 0; j < N; j++){
                for(int k = 0; k < M; k++){
                    res[i][j] += (*this)[i][k] * mat2[k][j];
                }
            }
        }
        matrix.swap(res);
        return *this;
    }
    mat& operator^=(const mat &mat2){
        assert(N == mat2.N && M == mat2.M);
        for(int i = 0; i < N; i++){
            for(int j = 0; j < M; j++) (*this)[i][j] ^= mat2[i][j];
        }
        return *this;
    }
    mat& operator|=(const mat &mat2){
        assert(N == mat2.N && M == mat2.M);
        for(int i = 0; i < N; i++){
            for(int j = 0; j < M; j++) (*this)[i][j] |= mat2[i][j];
        }
        return *this;
    }
    mat& operator&=(const mat &mat2){
        assert(N == mat2.N && M == mat2.M);
        for(int i = 0; i < N; i++){
            for(int j = 0; j < M; j++) (*this)[i][j] &= mat2[i][j];
        }
        return *this;
    }
    mat& operator+=(const mat &mat2){
        assert(N == mat2.N && M == mat2.M);
        for(int i = 0; i < N; i++){
            for(int j = 0; j < M; j++) (*this)[i][j] += mat2[i][j];
        }
        return *this;
    }
    mat& operator-=(const mat &mat2){
        assert(N == mat2.N && M == mat2.M);
        for(int i = 0; i < N; i++){
            for(int j = 0; j < M; j++) (*this)[i][j] -= mat2[i][j];
        }
        return *this;
    }
    mat operator+(const mat &mat1){
        return mat(*this) += mat1;
    }
    mat operator-(const mat &mat1){
        return mat(*this) -= mat1;
    }
    mat operator*(const mat &mat2){
        return mat(*this) *= mat2;
    }
    mat operator&(const mat &mat1){
        return mat(*this) &= mat1;
    }
    mat operator|(const mat &mat1){
        return mat(*this) |= mat1;
    }
    mat operator^(const mat &mat1){
        return mat(*this) ^= mat1;
    }
};
int main(){
    int A, B;
    cin >> A >> B;
    int N;
    cin >> N;
    for(int i = 0; i < N; i++){
        long long T;
        cin >> T;
        mat<mint> m(3, 3);
        m[1][1] = A;
        m[1][2]= B;
        m[2][1] = 1;
        vector<mint> temp = {0, 1, 1};
        long long t = T;
        t /= 2;
        m = m.pow(t);
        vector<mint> te = m.operate(temp);
        mint ans = 0;
        if(T % 2 == 0){
            for(int j = 0; j < 3; j++) ans += te[j];
            cout << ans.val << endl;
        }
        else{
            ans += te[1] * A + te[2] * B + te[1] + te[2];
            cout << ans.val << endl;
        }
    }
}
0