結果
| 問題 |
No.891 隣接3項間の漸化式
|
| コンテスト | |
| ユーザー |
merom686
|
| 提出日時 | 2019-09-20 21:51:40 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 3,498 bytes |
| コンパイル時間 | 864 ms |
| コンパイル使用メモリ | 75,404 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-14 17:16:39 |
| 合計ジャッジ時間 | 2,005 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 39 |
ソースコード
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;
struct ModInt {
ModInt() {}
ModInt(ll k) : i(k %Mod) {}
ModInt operator+(ModInt m) const {
ModInt r;
r.i = i + m.i;
if (r.i >= Mod) r.i -= Mod;
return r;
}
ModInt operator-(ModInt m) const {
ModInt r;
r.i = i - m.i;
if (r.i < 0) r.i += Mod;
return r;
}
ModInt operator*(ModInt m) const {
ModInt r;
r.i = (ll)i * m.i % Mod;
return r;
}
ModInt &operator+=(ModInt m) {
i += m.i;
if (i >= Mod) i -= Mod;
return *this;
}
ModInt &operator-=(ModInt m) {
i -= m.i;
if (i < 0) i += Mod;
return *this;
}
ModInt &operator*=(ModInt m) {
i = (ll)i * m.i % Mod;
return *this;
}
ModInt operator-() const {
ModInt r;
r.i = i == 0 ? 0 : Mod - i;
return r;
}
ModInt pow(ll k) const {
ModInt r = 1, t = *this;
for (; k != 0; k /= 2) {
if (k & 1) r *= t;
t *= t;
}
return r;
}
////Modが素数のときのみ
//ModInt inv() const {
// return pow(Mod - 2);
//}
//ModInt operator/(ModInt m) const {
// return *this * m.inv();
//}
//ModInt &operator/=(ModInt m) {
// return *this *= m.inv();
//}
static int Mod;
int i;
};
int ModInt::Mod = 1000000007;
template <class T, int N>
struct ColumnVector {
struct SquareMatrix {
static SquareMatrix E() {
SquareMatrix r;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
r.a[i][j] = i == j;
}
}
return r;
}
ColumnVector operator*(const ColumnVector &v) const {
ColumnVector r;
for (int i = 0; i < N; i++) {
T t = 0;
for (int k = 0; k < N; k++) {
t += a[i][k] * v.a[k];
}
r.a[i] = t;
}
return r;
}
SquareMatrix operator*(const SquareMatrix &x) const {
SquareMatrix r;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
T t = 0;
for (int k = 0; k < N; k++) {
t += a[i][k] * x.a[k][j];
}
r.a[i][j] = t;
}
}
return r;
}
SquareMatrix pow(ll k) const {
SquareMatrix r = E(), t = *this;
for (; k != 0; k /= 2) {
if (k & 1) r = r * t;
t = t * t;
}
return r;
}
void print() const {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << a[i][j].i << " \n"[j == N - 1];
}
}
}
T a[N][N];
};
void print() const {
for (int i = 0; i < N; i++) {
cout << a[i].i << " \n"[i == N - 1];
}
}
T a[N];
};
int main() {
int a, b, n;
cin >> a >> b >> n;
ColumnVector<ModInt, 2> v = { 0, 1 };
decltype(v)::SquareMatrix x = { 0, 1, b, a };
v = x.pow(n) * v;
cout << v.a[0].i << endl;
return 0;
}
merom686