結果
問題 | No.891 隣接3項間の漸化式 |
ユーザー |
![]() |
提出日時 | 2019-09-21 22:08:18 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,545 bytes |
コンパイル時間 | 622 ms |
コンパイル使用メモリ | 83,920 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-19 03:10:11 |
合計ジャッジ時間 | 1,914 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 39 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:72:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 72 | scanf("%d%d%d", &a, &b, &n); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~
ソースコード
/* -*- coding: utf-8 -*- * * 891.cc: No.891 隣接3項間の漸化式 - yukicoder */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int MOD = 1000000007; /* typedef */ typedef long long ll; typedef int mat[2][2]; /* global variables */ mat ma, mc; /* subroutines */ void copymat(const mat a, mat b) { memcpy(b, a, sizeof(mat)); } void initmat(mat a) { a[0][0] = a[1][1] = 1, a[0][1] = a[1][0] = 0; } void mulmat(const mat a, const mat b, mat c) { // a * b => c c[0][0] = ((ll)a[0][0] * b[0][0] + (ll)a[0][1] * b[1][0]) % MOD; c[0][1] = ((ll)a[0][0] * b[0][1] + (ll)a[0][1] * b[1][1]) % MOD; c[1][0] = ((ll)a[1][0] * b[0][0] + (ll)a[1][1] * b[1][0]) % MOD; c[1][1] = ((ll)a[1][0] * b[0][1] + (ll)a[1][1] * b[1][1]) % MOD; } void powmat(const mat a, int b, mat c) { // a^b => c mat s, t; initmat(c); copymat(a, s); while (b > 0) { if (b & 1) { mulmat(c, s, t); copymat(t, c); } mulmat(s, s, t); copymat(t, s); b >>= 1; } } /* main */ int main() { int a, b, n; scanf("%d%d%d", &a, &b, &n); if (n <= 1) printf("%d\n", n); else { ma[0][0] = 0; ma[0][1] = 1; ma[1][0] = b; ma[1][1] = a; powmat(ma, n, mc); printf("%d\n", mc[0][1]); } return 0; }