結果

問題 No.147 試験監督(2)
ユーザー mayoko_mayoko_
提出日時 2015-04-04 16:35:32
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 3,406 bytes
コンパイル時間 1,384 ms
コンパイル使用メモリ 155,560 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-17 04:25:25
合計ジャッジ時間 10,180 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;

const int MOD = 1e9+7;
const int MM = MOD-1;

template<int R, int C, ll mod>
class Mat {
public:
    Mat() : mat(R, vector<ll>(C)) {}
    Mat<R, C, mod>& operator=(const Mat<R, C, mod>& rhs) {
        for (int i = 0; i < R; i++) for (int j = 0; j < C; j++) {
            mat[i][j] = rhs[i][j];
        }
        return *this;
    }

    vector<ll>& operator[](int r) {return mat[r];}
    const vector<ll> operator[](int r) const {return mat[r];}

    Mat<R, C, mod>& operator+=(const Mat<R, C, mod> rhs) {
        for (int i = 0; i < R; i++) for (int j = 0; j < C; j++) {
            mat[i][j] += rhs.mat[i][j];
            if (mat[i][j] >= mod) mat[i][j] -= mod;
        }
        return (*this);
    }
    Mat<R, C, mod>& operator-=(const Mat<R, C, mod> rhs) {
        for (int i = 0; i < R; i++) for (int j = 0; j < C; j++) {
            mat[i][j] -= rhs.mat[i][j];
            mat[i][j] %= mod;
            if (mat[i][j] < 0) mat[i][j] += mod;
        }
        return (*this);
    }
    const Mat<R, C, mod> operator+(const Mat<R, C, mod> rhs) const {
        Mat<R, C, mod> ret = (*this);
        ret += rhs;
        return ret;
    }
    const Mat<R, C, mod> operator-(const Mat<R, C, mod> rhs) const {
        Mat<R, C, mod> ret = (*this);
        ret -= rhs;
        return ret;
    }
    const Mat<R, R, mod> operator*(const Mat<C, R, mod> rhs) const {
        Mat<R, R, mod> ret;
        for (int i = 0; i < R; i++) {
            for (int k = 0; k < C; k++) {
                for (int j = 0; j < R; j++) {
                    ret[i][j] += ((*this)[i][k] * (ll)rhs[k][j]) % mod;
                    if (ret[i][j] >= mod) ret[i][j] %= mod;
                }
            }
        }
        return ret;
    }

    void print() const {
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                cout << mat[i][j] << "\t";
            }
            cout << endl;
        }
    }
private:
    vector<vector<ll> > mat;
};

template<int R, ll mod>
Mat<R, R, mod> matrixPow(Mat<R, R, mod> a, ll N) {
    Mat<R, R, mod> ret;
    for (int i = 0; i < R; i++) ret[i][i] = 1;
    for (int i = 0; N>0; i++) {
        if ((N>>i)&1) {
            ret = ret*a;
            N -= (1ll<<i);
        }
        a = a*a;
    }
    return ret;
}

typedef Mat<2, 2, MOD> Mat2x2;

ll getMod(const string& s) {
    int n = s.size();
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        ans *= 10;
        ans += (s[i]-'0');
        ans %= MM;
    }
    return ans;
}
// x^p
ll powmod(ll x, ll p, ll m = MOD) {
    if (x == 0) return 0;
    if (p == 0) return 1;
    if (p == 1) return x;
    if (p % 2 == 0) {
        ll tmp = powmod(x, p/2, m);
        return (tmp*tmp)%m;
    } else {
        ll tmp = powmod(x, p-1, m);
        return (tmp*x)%m;
    }
}

int main() {
    int N;
    cin >> N;
    ll ans = 1;
    Mat2x2 a;
    a[0][1] = a[1][0] = a[1][1] = 1;
    while (N--) {
        ll C;
        string D;
        cin >> C >> D;
        ll d = getMod(D);
        Mat2x2 b = matrixPow(a, C-1);
        ll tmp = b[1][0] + b[1][1] * 2;
        tmp %= MOD;
        ans *= powmod(tmp, d);
        ans %= MOD;
        cout << ans << endl;
    }
    cout << ans << endl;
    return 0;
}
0