結果
| 問題 |
No.147 試験監督(2)
|
| コンテスト | |
| ユーザー |
mayoko_
|
| 提出日時 | 2015-04-04 12:47:21 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 1,510 ms / 2,000 ms |
| コード長 | 3,336 bytes |
| コンパイル時間 | 1,306 ms |
| コンパイル使用メモリ | 165,360 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-04 01:47:10 |
| 合計ジャッジ時間 | 8,579 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 |
ソースコード
#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 ll MOD = 1e9+7;
const ll MM = MOD-1;
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;
}
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) {return rhs;}
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 j = 0; j < R; j++) {
for (int k = 0; k < C; k++) {
ret[i][j] += (*this)[i][k] * rhs[k][j];
if (ret[i][j] >= mod) ret[i][j] %= mod;
}
}
return ret;
}
const Mat<R, R, mod> pow(ll N) const {
if (N == 0) {
Mat<R, R, mod> E;
for (int i = 0; i < R; i++) E[i][i] = 1;
return E;
}
if (N == 1) {
return (*this);
}
if (N % 2 == 1) {
Mat<R, R, mod> tmp = pow(N-1);
return tmp * (*this);
} else {
Mat<R, R, mod> tmp = pow(N/2);
return tmp*tmp;
}
}
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;
};
typedef Mat<2, 2, MOD> Mat2x2;
// 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;
while (N--) {
ll C;
string D;
cin >> C >> D;
ll d = getMod(D);
Mat2x2 a;
a[0][1] = a[1][0] = a[1][1] = 1;
Mat2x2 b = a.pow(C-1);
ll tmp = (b[1][0]+b[1][1]*2)%MOD;
tmp = powmod(tmp, d);
ans *= tmp;
ans %= MOD;
}
cout << ans << endl;
return 0;
}
mayoko_