結果

問題 No.220 世界のなんとか2
ユーザー むらためむらため
提出日時 2019-02-02 18:33:47
言語 Nim
(2.0.2)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 991 bytes
コンパイル時間 1,958 ms
コンパイル使用メモリ 66,104 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 03:21:20
合計ジャッジ時間 2,903 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils,math

proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': return
    result = 10 * result + k.ord - '0'.ord

proc solve() : seq[int] =
  # 総和 mod 3 と 3がつくかでわける
  var dp = newSeqWith(20, newSeqWith(3,newSeqWith(2,0)))
  for i in 0..<10:
    let x = i mod 3
    let y = if i == 3 : 1 else: 0
    dp[0][x][y] += 1

  for i in 0..<18:
    for x in 0..<3:
      for y in 0..<3:
        # 既に3がつくものに 1,4,7 | 2 5 8 | 0 3 6 9
        dp[i+1][(x+y) mod 3][1] += [4,3,3][y] * dp[i][x][1]
        # つかないものに 1,4,7 | 2,5,8 | 0,6,9
        dp[i+1][(x+y) mod 3][0] += 3 * dp[i][x][0]
    # つかないものに 3
    dp[i+1][0][1] += dp[i][0][0]
    dp[i+1][1][1] += dp[i][1][0]
    dp[i+1][2][1] += dp[i][2][0]

  return dp.mapIt(it[0][0] + it.mapIt(it[1]).sum() - 1)

const answers = solve()
let n = scan()
echo answers[n - 1]
0