結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー tentententen
提出日時 2020-10-09 13:12:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 591 bytes
コンパイル時間 1,987 ms
コンパイル使用メモリ 71,484 KB
実行使用メモリ 56,052 KB
最終ジャッジ日時 2023-09-27 12:37:31
合計ジャッジ時間 4,477 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
55,948 KB
testcase_01 AC 106 ms
55,720 KB
testcase_02 AC 104 ms
55,668 KB
testcase_03 AC 102 ms
55,928 KB
testcase_04 AC 108 ms
55,820 KB
testcase_05 AC 101 ms
55,528 KB
testcase_06 AC 104 ms
55,880 KB
testcase_07 AC 106 ms
55,860 KB
testcase_08 AC 108 ms
55,568 KB
testcase_09 AC 109 ms
55,796 KB
testcase_10 AC 127 ms
56,052 KB
testcase_11 AC 181 ms
53,956 KB
testcase_12 AC 182 ms
55,948 KB
testcase_13 AC 183 ms
55,940 KB
testcase_14 AC 189 ms
55,532 KB
権限があれば一括ダウンロードができます

ソースコード

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