結果
問題 | No.891 隣接3項間の漸化式 |
ユーザー |
![]() |
提出日時 | 2019-09-20 21:47:01 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,909 bytes |
コンパイル時間 | 1,551 ms |
コンパイル使用メモリ | 168,844 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-14 17:09:04 |
合計ジャッジ時間 | 2,691 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 39 |
ソースコード
#include <bits/stdc++.h>using namespace std;#define FOR(i,a,b) for(int i=(a);i<(b);i++)#define REP(i,n) FOR(i,0,n)#define ALL(v) (v).begin(),(v).end()#define fi first#define se secondtemplate<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }typedef long long ll;typedef pair<int, int> pii;typedef pair<ll, ll> pll;const ll INF = 1ll<<30;const ll longINF = 1ll<<60;const ll MOD = 1000000007;const double EPS = 1e-9;const bool debug = 0;//---------------------------------//typedef vector<ll> vl;typedef vector<vl> vvl;vvl multi(vvl &a, vvl &b, ll mod) {vvl res(a.size(), vl(b[0].size()));REP(i, a.size()) {REP(j, b[0].size()) {REP(k, b.size()) {res[i][j] += a[i][k] * b[k][j] % mod;res[i][j] %= mod;}}}return res;}vvl pow(vvl x, ll n, ll mod) {vvl res(x.size(), vl(x.size()));REP(i, x.size()) res[i][i] = 1;while (n > 0) {if (n & 1) res = multi(res, x, mod);x = multi(x, x, mod);n >>= 1;}return res;}vvl init(ll a, ll b) {vvl res(2, vl(2));res[0][0] = a;res[0][1] = b;res[1][0] = 1;res[1][1] = 0;return res;}ll solve(ll x, ll mod, ll a, ll b) {vvl cur = init(a, b);vvl se(2, vl(1));se[0][0] = 1;se[1][0] = 0;cur = pow(cur, x - 1, mod);cur = multi(cur, se, mod);return cur[0][0];}ll mod_pow(ll x, ll n, ll mod) {if (n == 0) return 1;ll res = mod_pow(x * x % mod, n / 2, mod);if (n & 1) res = res * x % mod;return res;}int main() {ll a, b, n;cin >> a >> b >> n;ll ans;if (a == 0 && b == 0) ans = n == 1;else if (a == 0) ans = n % 2 == 1 ? mod_pow(b, n / 2, MOD) : 0;else if (b == 0) ans = n == 0 ? 0 : mod_pow(a, n - 1, MOD);else ans = n == 0 ? 0 : solve(n , MOD, a, b);cout << ans << endl;return 0;}