結果

問題 No.147 試験監督(2)
ユーザー HachimoriHachimori
提出日時 2015-02-09 01:01:24
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,999 bytes
コンパイル時間 505 ms
コンパイル使用メモリ 55,748 KB
実行使用メモリ 23,384 KB
最終ジャッジ日時 2023-09-05 15:10:33
合計ジャッジ時間 7,096 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cstring>
using namespace std;
const int NUM_DATA = 30005;
const int MOD = 1000000007;

class Matrix {
public:
    int v[2][2];

    Matrix(){
        memset(v, 0, sizeof(v));
    }
};


int N;
long long C[NUM_DATA];
string D[NUM_DATA];

void read() {
    cin >> N;
    for (int i = 0; i < N; ++i) {
        cin >> C[i] >> D[i];
        for (int j = 0; j < D[i].size(); ++j)
            D[i][j] -= '0';
    }
}


Matrix matmul(const Matrix &a, const Matrix &b) {
    Matrix c;
    for (int i = 0; i < 2; ++i)
        for (int j = 0; j < 2; ++j) {
            int toPush = 0;
            for (int k = 0; k < 2; ++k)
                toPush = (toPush + 1LL * a.v[i][k] * b.v[k][j]) % MOD;
            c.v[i][j] = toPush;
        }
    return c;
}


Matrix matpow(Matrix &p, long long n) {
    if (n == 0) {
        Matrix I;
        I.v[0][0] = 1; I.v[0][1] = 0;
        I.v[1][0] = 0; I.v[1][1] = 1;
        return I;
    }
    
    Matrix t = matpow(p, n / 2);
    return n & 1 ? matmul(matmul(t, t), p) : matmul(t, t);
}


int mypow(int p, string s) {
    int bgn = 0;
    int ret = 1;
    
    while (1) {
        if (s[s.size() - 1] % 2 == 1) {
            ret = 1LL * ret * p % MOD;
        }
        p = 1LL * p * p % MOD;
        
        
        while (bgn < s.size() && s[bgn] == 0) ++bgn;
        if (bgn == s.size()) break;
        
        // s /= 2
        int carry = 0;
        for (int i = bgn; i < s.size(); ++i) {
            int nextCarry = (s[i] + carry * 10) % 2;
            s[i] = (s[i] + carry * 10) / 2;
            carry = nextCarry;
        }
    }
    
    return ret;
}


void work() {
    // 1, 1
    // 1, 0
    Matrix mat;
    mat.v[0][0] = 1; mat.v[0][1] = 1;
    mat.v[1][0] = 1; mat.v[1][1] = 0;
    
    int ans = 1;
    for (int i = 0; i < N; ++i) {
        Matrix t = matpow(mat, C[i] + 1);
        ans = 1LL * ans * mypow(t.v[0][0], D[i]) % MOD;
    }
    cout << ans << endl;
}


int main() {
    read();
    work();
    return 0;
}
0