QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#779034 | #6300. Best Carry Player 2 | wangshengzhe | RE | 0ms | 0kb | Python3 | 1.5kb | 2024-11-24 17:09:35 | 2024-11-24 17:09:36 |
answer
import sys
import math
def read():
return int(input().strip())
def print_ans(n):
if n < 0:
print('-', end='')
n = -n
print(n)
def solve(x, m):
dp = [[[float('inf')] * 2 for _ in range(20)] for _ in range(40)]
a = [0] * 20
cnt = 0
while x:
cnt += 1
a[cnt] = x % 10
x //= 10
num = [1] * 40
for i in range(1, 37):
num[i] = num[i - 1] * 10
dp[0][0][0] = 0
p = max(cnt, m)
if m == 0:
for i in range(1, cnt + 2):
if a[i] != 9:
print_ans(num[i - 1])
return
for i in range(1, 37):
for k in range(0, m + 1):
if a[i] + 1 < 10:
dp[i][k][0] = min(dp[i - 1][k][1], dp[i - 1][k][0])
else:
dp[i][k][0] = dp[i - 1][k][0]
if k != 0:
if a[i] != 0:
dp[i][k][1] = min((10 - a[i] - 1) * num[i - 1] + dp[i - 1][k - 1][1],
(10 - a[i]) * num[i - 1] + dp[i - 1][k - 1][0])
else:
dp[i][k][1] = (10 - a[i] - 1) * num[i - 1] + dp[i - 1][k - 1][1]
ans = float('inf')
for i in range(cnt, 37):
ans = min(ans, min(dp[i][m][1], dp[i][m][0]))
print_ans(ans)
return
def main():
t = int(input())
for _ in range(t):
(x, m) = map(int, input().split())
solve(x, m)
if __name__ == "__main__":
main()
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
4 12345678 0 12345678 5 12345678 18 990099 5
output:
1