結果

問題 No.314 ケンケンパ
ユーザー SagTokiSagToki
提出日時 2018-05-25 10:42:06
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 1,927 bytes
コンパイル時間 3,709 ms
コンパイル使用メモリ 74,708 KB
実行使用メモリ 62,220 KB
最終ジャッジ日時 2023-09-11 03:22:18
合計ジャッジ時間 7,083 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 148 ms
61,788 KB
testcase_01 AC 125 ms
55,696 KB
testcase_02 AC 124 ms
55,800 KB
testcase_03 AC 124 ms
55,748 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 128 ms
55,864 KB
testcase_07 AC 128 ms
55,632 KB
testcase_08 AC 127 ms
56,012 KB
testcase_09 AC 125 ms
56,048 KB
testcase_10 AC 126 ms
55,856 KB
testcase_11 AC 125 ms
55,476 KB
testcase_12 AC 127 ms
55,720 KB
testcase_13 AC 125 ms
55,996 KB
testcase_14 AC 125 ms
55,764 KB
testcase_15 AC 126 ms
55,836 KB
testcase_16 AC 126 ms
55,880 KB
testcase_17 AC 131 ms
55,980 KB
testcase_18 AC 147 ms
62,220 KB
testcase_19 AC 150 ms
62,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Kenkenpa {
    //メインメソッド
    public static void main(String[] args){
        int N = InputN();
        long[] Count = Calculation(N);
        //1~3番目は漸化式で解けないので値を直接入力する
        switch (N) {
            case 1:
                System.out.println("1");
                break;
            case 2:
            case 3:
                System.out.println("2");
                break;
            default:
                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