QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#765633#5460. Sum of NumbersO_start#TL 10ms10712kbPython31.8kb2024-11-20 14:52:232024-11-20 14:52:33

Judging History

你现在查看的是最新测评结果

  • [2024-11-20 14:52:33]
  • 评测
  • 测评结果:TL
  • 用时:10ms
  • 内存:10712kb
  • [2024-11-20 14:52:23]
  • 提交

answer

import sys
sys.set_int_max_str_digits(0)
t = int(input())
n, k = 0, 0
s = ''

d = 10


def solve():
    len0 = n // (k + 1)
    pos0 = [len0 * i + len0 - 1 for i in range(k)]
    dp = [[-1 for i in range(d * 2)] for _ in range(k)]
    for i in range(k):
        if i == 0:
            for j in range(d * 2):
                p1 = pos0[i] + (j - d)
                if p1 < 0 or p1 >= n:
                    continue
                dp[i][j] = int(s[0: p1 + 1])
            continue
        for j in range(d * 2):
            for pre_j in range(d * 2):
                p1 = pos0[i] + (j - d)
                pre_p1 = pos0[i - 1] + (pre_j - d)
                if p1 < 0 or p1 >= n:
                    continue
                if pre_p1 < 0 or pre_p1 >= n:
                    continue
                if not pre_p1 < p1:
                    continue
                if dp[i - 1][pre_j] == -1:
                    continue
                temp = dp[i - 1][pre_j] + int(s[pre_p1 + 1: p1 + 1])
                # dp[i][j] = temp if dp[i][j] == -1 or temp < dp[i][j] else -1
                if dp[i][j] == -1:
                    dp[i][j] = temp
                else:
                    dp[i][j] = min(dp[i][j], temp)
    ans = -1
    for pre_j in range(d * 2):
        pre_p1 = pos0[len(pos0) - 1] + (pre_j - d)
        if pre_p1 < 0 or pre_p1 >= n - 1:
            continue
        if dp[k - 1][pre_j] == -1:
            continue
        temp = dp[k - 1][pre_j] + int(s[pre_p1 + 1:])
        if ans == -1:
            ans = temp
        else:
            ans = min(ans, temp)

    # for i in range(k):
    #     print(dp[i])
    print(ans)


for _ in range(t):
    ins = input().split(' ')
    n, k = int(ins[0]), int(ins[1])
    s = input()
    # for _ in range(n):
    #     s += '5'
    solve()


'''
1
8 1
45455151
'''

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 10ms
memory: 10712kb

input:

2
8 1
45455151
2 1
42

output:

9696
6

result:

ok 2 lines

Test #2:

score: -100
Time Limit Exceeded

input:

10
1301 6
56328399613959594774559774218276494124991536454496431869449134772679831477279356599352619469813771742358572734317965823527349354276551857226632977613336815474383422853946661428822284645652423563864641261338984158269966469425994769486371736593879954275146732544891889693921182364554588732946...

output:

2861837555106640794797067737879913860686764066159587941287350938727749577629356630565034353414526438507603808735990935008225192080065174423508575377930722196909797866802717925250679901255
1330897896655974774035586406544907434842835048336411271110427836483063457950873824562288934364096546537492367401...

result: