結果

問題 No.554 recurrence formula
ユーザー takeya_okinotakeya_okino
提出日時 2017-08-11 23:11:50
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 890 bytes
コンパイル時間 2,315 ms
コンパイル使用メモリ 77,360 KB
実行使用メモリ 54,408 KB
最終ジャッジ日時 2024-04-20 23:27:46
合計ジャッジ時間 5,826 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 135 ms
41,208 KB
testcase_02 AC 133 ms
41,544 KB
testcase_03 AC 132 ms
41,684 KB
testcase_04 AC 133 ms
41,152 KB
testcase_05 AC 117 ms
40,300 KB
testcase_06 AC 134 ms
41,264 KB
testcase_07 AC 132 ms
41,164 KB
testcase_08 AC 119 ms
40,292 KB
testcase_09 AC 118 ms
39,752 KB
testcase_10 AC 131 ms
41,584 KB
testcase_11 AC 134 ms
41,536 KB
testcase_12 AC 132 ms
41,260 KB
testcase_13 AC 119 ms
40,420 KB
testcase_14 AC 131 ms
41,636 KB
testcase_15 AC 134 ms
41,700 KB
testcase_16 AC 131 ms
41,644 KB
testcase_17 AC 134 ms
41,316 KB
testcase_18 AC 132 ms
41,124 KB
testcase_19 AC 142 ms
43,608 KB
testcase_20 AC 143 ms
43,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    long MOD = (long)Math.pow(10, 9) + 7;
    long[] dp = new long[(int)n + 1];
    long[] oddsum = new long[(int)n + 1];
    long[] evensum = new long[(int)n + 1];
    dp[1] = 1;
    oddsum[1] = 1;
    evensum[1] = 0;
    dp[2] = 2;
    oddsum[2] = 1;
    evensum[2] = 2;
    for(long i = 3; i < n + 1; i++) {
      if(i % 2 == 0) {
        dp[(int)i] = (i * oddsum[(int)i - 1]) % MOD;
        oddsum[(int)i] = oddsum[(int)i - 1];
        evensum[(int)i] = (evensum[(int)i - 2] + dp[(int)i]) % MOD;
      } else {
        dp[(int)i] = (i * evensum[(int)i - 1]) % MOD;
        oddsum[(int)i] = (oddsum[(int)i - 2] + dp[(int)i]) % MOD;
        evensum[(int)i] = evensum[(int)i - 1];
      }
    }
    System.out.println(dp[(int)n]);
  }
}
0