結果
| 問題 |
No.526 フィボナッチ数列の第N項をMで割った余りを求める
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-07-06 14:36:49 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 662 bytes |
| コンパイル時間 | 442 ms |
| コンパイル使用メモリ | 49,792 KB |
| 最終ジャッジ日時 | 2025-01-05 01:14:02 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 10 RE * 2 |
コンパイルメッセージ
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 <cmath>
#include <cstdio>
using ll = long long;
int fib(ll n, int mod) {
assert (3 * pow(mod, 2) < pow(2, 63));
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;
int nb = (2ll * a + b) * b % mod;
a = na;
b = nb;
if (n & i) {
int c = a + 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;
}