結果
問題 |
No.526 フィボナッチ数列の第N項をMで割った余りを求める
|
ユーザー |
|
提出日時 | 2017-07-06 14:40:11 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 20 ms / 2,000 ms |
コード長 | 653 bytes |
コンパイル時間 | 160 ms |
コンパイル使用メモリ | 28,288 KB |
最終ジャッジ日時 | 2025-01-05 01:14:04 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:28:20: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 28 | int n, m; scanf("%d%d", &n, &m); | ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <cassert> #include <cstdio> using ll = long long; int fib(ll n, int mod) { assert (n >= 0); if (n <= 1) return n; int a = 0; int b = 1; ll i = 1ll << (63 - __builtin_clzll(n) - 1); for (; i; i >>= 1) { int na = (a *(ll) a + b *(ll) b) % mod; ll nb = 2ll * a + b; if (nb >= mod) nb -= mod; nb = nb * b % mod; a = na; b = nb; if (n & i) { ll c = a +(ll) b; if (c >= mod) c -= mod; a = b; b = c; } } return b; } int main() { int n, m; scanf("%d%d", &n, &m); printf("%d\n", fib(n - 1, m)); return 0; }