結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー tenten
提出日時 2020-10-09 13:12:31
言語 Java
(openjdk 23)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 591 bytes
コンパイル時間 2,316 ms
コンパイル使用メモリ 73,868 KB
実行使用メモリ 41,380 KB
最終ジャッジ日時 2024-07-20 06:45:24
合計ジャッジ時間 5,502 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long m = sc.nextInt();
        if (n == 1) {
            System.out.println(0);
            return;
        } else if (n == 2) {
            System.out.println(1);
            return;
        }
        long prepre = 0;
        long pre = 1;
        for (int i = 3; i <= n; i++) {
            long cur = (prepre + pre) % m;
            prepre = pre;
            pre = cur;
        }
        System.out.println(pre);
    }
}
0