結果
問題 | No.220 世界のなんとか2 |
ユーザー |
![]() |
提出日時 | 2025-03-20 20:39:39 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 40 ms / 1,000 ms |
コード長 | 1,536 bytes |
コンパイル時間 | 141 ms |
コンパイル使用メモリ | 82,172 KB |
実行使用メモリ | 54,284 KB |
最終ジャッジ日時 | 2025-03-20 20:39:53 |
合計ジャッジ時間 | 1,613 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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)