結果
問題 |
No.438 Cwwプログラミング入門
|
ユーザー |
![]() |
提出日時 | 2020-03-07 02:04:12 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 35 ms / 2,000 ms |
コード長 | 1,488 bytes |
コンパイル時間 | 122 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 11,008 KB |
最終ジャッジ日時 | 2024-10-14 11:04:13 |
合計ジャッジ時間 | 6,217 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 98 |
ソースコード
#!/usr/bin/env python3 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # %% x, y, z = map(int, read().split()) # %% def simulate(code): stack = [] for ch in code: if ch == 'c': stack.append(x) elif ch == 'w': stack.append(y) elif ch == 'C': a = stack.pop() b = stack.pop() stack.append(a + b) elif ch == 'W': a = stack.pop() b = stack.pop() stack.append(a - b) else: raise ValueError return stack[-1] def to_code(A, B): if A == B == 0: return 'ccW' if A == 0 and B > 0: return 'w' * B + 'C' * (B - 1) if A > 0 and B == 0: return 'c' * A + 'C' * (A - 1) if A > 0 and B > 0: return 'c' * A + 'w' * B + 'C' * (A + B - 1) if A > 0 and B < 0: return 'w' * (-B) + 'C' * (-B - 1) + 'c' * A + 'C' * (A - 1) + 'W' if A < 0 and B > 0: return 'c' * (-A) + 'C' * (-A - 1) + 'w' * B + 'C' * (B - 1) + 'W' print(A, B) def solve(x, y, z): for A in range(5100, -5100, -1): if y == 0: B = 0 else: B = (z - A * x) // y if A * x + B * y == z: if 2 * (abs(A) + abs(B)) - 1 <= 10000: return to_code(A, B) return 'NO' code = solve(x, y, z) if code != 'NO': assert simulate(code) == z print(code) # %%