結果
| 問題 | No.220 世界のなんとか2 |
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-20 20:39:39 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 56 ms / 1,000 ms |
| コード長 | 1,536 bytes |
| 記録 | |
| コンパイル時間 | 225 ms |
| コンパイル使用メモリ | 96,268 KB |
| 実行使用メモリ | 79,344 KB |
| 最終ジャッジ日時 | 2026-07-07 11:47:13 |
| 合計ジャッジ時間 | 2,878 ms |
|
ジャッジサーバーID (参考情報) |
judge1_0 / judge3_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
ソースコード
P = int(input())
# Convert 10^P to its digit representation as a string
max_num = 10 ** P
digits = list(str(max_num))
n = len(digits)
# Initialize DP table: dp[pos][tight][leading_zero][mod3]
dp = [[[[0] * 3 for _ in range(2)] for __ in range(2)] for ___ in range(n + 1)]
dp[0][1][1][0] = 1 # Starting state: position 0, tight=1, leading_zero=1, mod3=0
for pos in range(n):
for tight in [0, 1]:
for leading_zero in [0, 1]:
for mod3 in range(3):
current = dp[pos][tight][leading_zero][mod3]
if current == 0:
continue
# Determine the upper bound of the current digit
upper = int(digits[pos]) if tight else 9
for d in range(0, upper + 1):
if d == 3:
continue # Skip digit 3
new_tight = 1 if (tight == 1 and d == upper) else 0
new_leading_zero = 1 if (leading_zero == 1 and d == 0) else 0
new_mod = (mod3 * 10 + d) % 3
dp[pos + 1][new_tight][new_leading_zero][new_mod] += current
# Calculate the count of invalid numbers (not containing 3 and not divisible by 3)
invalid_count = 0
for tight in [0, 1]:
for leading_zero in [0, 1]:
for mod3 in range(3):
if leading_zero == 0 and mod3 != 0:
invalid_count += dp[n][tight][leading_zero][mod3]
# The total numbers from 1 to 10^P (inclusive)
total = 10 ** P
result = total - invalid_count
print(result)
lam6er