結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 134 ms
54,060 KB
testcase_02 AC 135 ms
54,220 KB
testcase_03 AC 140 ms
54,024 KB
testcase_04 AC 140 ms
54,480 KB
testcase_05 AC 134 ms
54,068 KB
testcase_06 AC 133 ms
54,220 KB
testcase_07 AC 135 ms
53,976 KB
testcase_08 AC 133 ms
54,276 KB
testcase_09 AC 135 ms
54,376 KB
testcase_10 AC 136 ms
54,244 KB
testcase_11 AC 134 ms
54,256 KB
testcase_12 AC 134 ms
54,000 KB
testcase_13 AC 136 ms
54,212 KB
testcase_14 AC 135 ms
53,976 KB
testcase_15 AC 134 ms
56,276 KB
testcase_16 AC 133 ms
54,180 KB
testcase_17 AC 134 ms
54,288 KB
testcase_18 AC 134 ms
54,288 KB
testcase_19 AC 143 ms
56,044 KB
testcase_20 AC 146 ms
56,292 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 - 2];
      }
    }
    System.out.println(dp[(int)n]);
  }
}
0