結果

問題 No.314 ケンケンパ
ユーザー SagTokiSagToki
提出日時 2018-05-25 10:43:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 144 ms / 1,000 ms
コード長 1,935 bytes
コンパイル時間 3,425 ms
コンパイル使用メモリ 74,984 KB
実行使用メモリ 62,108 KB
最終ジャッジ日時 2023-09-11 03:22:31
合計ジャッジ時間 7,098 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 144 ms
61,672 KB
testcase_01 AC 125 ms
55,564 KB
testcase_02 AC 125 ms
55,920 KB
testcase_03 AC 124 ms
55,756 KB
testcase_04 AC 125 ms
55,760 KB
testcase_05 AC 125 ms
55,808 KB
testcase_06 AC 124 ms
56,220 KB
testcase_07 AC 123 ms
55,892 KB
testcase_08 AC 125 ms
55,892 KB
testcase_09 AC 126 ms
55,968 KB
testcase_10 AC 125 ms
53,488 KB
testcase_11 AC 124 ms
55,820 KB
testcase_12 AC 125 ms
55,876 KB
testcase_13 AC 123 ms
55,988 KB
testcase_14 AC 124 ms
55,748 KB
testcase_15 AC 124 ms
55,564 KB
testcase_16 AC 126 ms
55,912 KB
testcase_17 AC 127 ms
55,824 KB
testcase_18 AC 141 ms
62,108 KB
testcase_19 AC 143 ms
61,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.InputMismatchException ;

public class Kenkenpa {
    //メインメソッド
    public static void main(String[] args){
        int N = InputN();
        //1~3番目は漸化式で解けないので値を直接入力する
        switch (N) {
            case 1:
                System.out.println("1");
                break;
            case 2:
            case 3:
                System.out.println("2");
                break;
            default:
                long[] Count = Calculation(N);
                System.out.println(Count[N - 1]);
                break;
        }
    }
    
    //入力値を読み取り数字であることと範囲内の数字であることを確認するメソッド
    public static int InputN(){
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        try{
            if(N < 1 || N > Math.pow(10, 6)){
                System.out.println("Nは1以上10の6乗以下で入力してください");
                System.exit(0);
            }
        }catch(InputMismatchException e){
            System.out.println("数字を入力してください");
            System.exit(0);
        }catch(Exception E){
            System.out.println("予期せぬエラーです");
            System.exit(0);
        }
        return N;
    }
    
    //入力値Nにおいて考えうる組み合わせを計算するメソッド
    public static long[] Calculation(int N){
        //Nに応じた組み合わせ数を格納する配列を定義する
        long Count[] = new long[N];
        int Division = (int) (Math.pow(10,9) + 7);
        Count[0] = 1;
        Count[1] = 2;
        Count[2] = 2;
        //漸化式 {a[n]=a[n-2]+a[n-3] (n≧4)} が成り立つ
        for(int i = 3 ; i < N ; i++){
            Count[i] = (Count[i - 2] + Count[i - 3]) % Division;
        }
        return Count;
    }
}
0