結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
61,980 KB
testcase_01 AC 126 ms
55,928 KB
testcase_02 AC 127 ms
55,804 KB
testcase_03 AC 125 ms
55,660 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 127 ms
55,868 KB
testcase_07 AC 127 ms
55,732 KB
testcase_08 AC 128 ms
55,884 KB
testcase_09 AC 130 ms
55,712 KB
testcase_10 AC 127 ms
55,812 KB
testcase_11 AC 127 ms
55,924 KB
testcase_12 AC 126 ms
55,616 KB
testcase_13 AC 126 ms
55,868 KB
testcase_14 AC 127 ms
55,536 KB
testcase_15 AC 130 ms
55,652 KB
testcase_16 AC 129 ms
55,904 KB
testcase_17 AC 129 ms
55,616 KB
testcase_18 AC 144 ms
62,176 KB
testcase_19 AC 145 ms
62,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Kenkampa {
    //メインメソッド
    public static void main(String[] args){
        int N = InputN();
        long[] Count = Calculation(N);
        //配列は0から始まるのでN-1番目が求めたい組み合わせ数になる
        System.out.println(Count[N - 1]);
    }
    
    //入力値を読み取り数字であることと範囲内の数字であることを確認するメソッド
    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);
        //1~3番目は漸化式で解けないので値を直接入力する
        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