結果

問題 No.147 試験監督(2)
ユーザー mamekinmamekin
提出日時 2015-02-13 22:15:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 490 ms / 2,000 ms
コード長 2,305 bytes
コンパイル時間 797 ms
コンパイル使用メモリ 97,364 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 00:46:13
合計ジャッジ時間 3,637 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 487 ms
4,380 KB
testcase_01 AC 490 ms
4,380 KB
testcase_02 AC 488 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

const int MOD = 1000000007;

// 行列の積
template <class T>
vector<vector<T> > matrixProduct(const vector<vector<T> >& x, const vector<vector<T> >& y)
{
    int a = x.size();
    int b = x[0].size();
    int c = y[0].size();
    vector<vector<T> > z(a, vector<T>(c, 0));
    for(int i=0; i<a; ++i){
        for(int j=0; j<c; ++j){
            for(int k=0; k<b; ++k){
                z[i][j] += x[i][k] * y[k][j];
                z[i][j] %= MOD;
            }
        }
    }
    return z;
}

// 行列の累乗
template <class T>
vector<vector<T> > matrixPower(const vector<vector<T> >& x, long long k)
{
    int n = x.size();
    vector<vector<T> > y(n, vector<T>(n, 0));
    for(int i=0; i<n; ++i)
        y[i][i] = 1; // 積の単位元

    vector<vector<T> > z = x;
    while(k > 0){
        if(k & 1)
            y = matrixProduct(y, z);
        z = matrixProduct(z, z);
        k >>= 1;
    }
    return y;
}

// 累乗、べき乗
long long power(long long a, long long b)
{
    long long ret = 1;
    long long tmp = a;
    while(b > 0){
        if(b & 1){
            ret *= tmp;
            ret %= MOD;
        }
        tmp *= tmp;
        tmp %= MOD;
        b >>= 1;
    }
    return ret;
}

long long modular(const string& s, long long mod)
{
    long long ret = 0;
    for(unsigned i=0; i<s.size(); ++i){
        ret *= 10;
        ret += s[i] - '0';
        ret %= mod;
    }
    return ret;
}

int main()
{
    int n;
    cin >> n;

    long long ret = 1;
    while(--n >= 0){
        long long c;
        string d;
        cin >> c >> d;

        vector<vector<long long> > mat ={{1, 1}, {1, 0}};
        mat = matrixPower(mat, c);
        long long x = (mat[0][0] + mat[0][1]) % MOD;
        if(x == 0){
            ret = 0;
            continue;
        }

        long long y = modular(d, MOD - 1);
        ret *= power(x, y);
        ret %= MOD;
    }
    cout << ret << endl;

    return 0;
}
0