結果
| 問題 |
No.891 隣接3項間の漸化式
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2020-10-31 12:01:52 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,791 bytes |
| コンパイル時間 | 1,822 ms |
| コンパイル使用メモリ | 172,976 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-22 04:52:22 |
| 合計ジャッジ時間 | 3,315 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 39 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)
template<ULL M>
struct static_modint {
ULL x;
static_modint(ULL val = 0) : x(val) {}
static_modint& operator+=(static_modint r) { x += r.x; if (x >= M) x -= M; return *this; }
static_modint operator+(static_modint r) const { static_modint res = x; return res += r; }
static_modint& operator-=(static_modint r) { x += M - r.x; if (x >= M) x -= M; return *this; }
static_modint operator-(static_modint r) const { static_modint res = x; return res -= r; }
static_modint& operator*=(static_modint r) { x = x * r.x % M; return *this; }
static_modint operator*(static_modint r) const { return static_modint(x * r.x % M); }
static_modint pow(ULL r) const {
if (r == 0) return static_modint(1);
static_modint res = pow(r / 2);
res *= res;
if (r % 2) res *= *this;
return res;
}
static_modint& operator/=(static_modint r) { *this *= r.pow(M - 2); return *this; }
static_modint operator/(static_modint r) const { return *this * (r.pow(M - 2)); }
ULL& operator*() { return x; }
const ULL& operator*() const { return x; }
bool operator==(static_modint r) const { return x == r; }
bool operator!=(static_modint r) const { return x != r; }
};
template<class Elem, size_t matrix_sz>
struct static_matrix {
Elem X[matrix_sz][matrix_sz] = {};
static static_matrix id() { static_matrix res; rep(i, matrix_sz) res[i][i] = 1; return res; }
Elem* operator[](int x) { return X[x]; }
const Elem* operator[](int x) const { return X[x]; }
static_matrix operator+(const static_matrix& r) const {
static_matrix res;
rep(i, matrix_sz) rep(j, matrix_sz)
res[i][j] = X[i][j] + r[i][j];
return res;
}
static_matrix operator-(const static_matrix& r) const {
static_matrix res;
rep(i, matrix_sz) rep(j, matrix_sz)
res[i][j] = X[i][j] - r[i][j];
return res;
}
static_matrix operator*(const static_matrix& r) const {
static_matrix res;
rep(i, matrix_sz) rep(j, matrix_sz) rep(k, matrix_sz)
res[i][j] += X[i][k] * r[k][j];
return res;
}
static_matrix pow(ULL N) const {
if (N == 0) return id();
static_matrix res = pow(N / 2);
res = res * res;
if (N % 2 == 1) res = res * *this;
return res;
}
};
const ULL M = 1000000007;
using MLL = static_modint<M>;
using Mat = static_matrix<MLL, 2>;
Mat G;
int main() {
MLL a, b; ULL N; cin >> *a >> *b >> N;
G[0][0] = a;
G[1][0] = b;
G[0][1] = 1;
G[1][1] = 0;
G = G.pow(N);
cout << *G[0][1] << endl;
return 0;
}
Nachia