結果

問題 No.2437 Fragile Multiples of 11
ユーザー tassei903tassei903
提出日時 2024-06-20 14:40:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 3,365 bytes
コンパイル時間 472 ms
コンパイル使用メモリ 82,096 KB
実行使用メモリ 75,152 KB
最終ジャッジ日時 2024-06-20 14:40:30
合計ジャッジ時間 4,426 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
54,504 KB
testcase_01 AC 40 ms
53,624 KB
testcase_02 AC 44 ms
60,680 KB
testcase_03 AC 75 ms
74,516 KB
testcase_04 AC 69 ms
73,148 KB
testcase_05 AC 73 ms
73,996 KB
testcase_06 AC 74 ms
73,516 KB
testcase_07 AC 73 ms
73,652 KB
testcase_08 AC 77 ms
74,060 KB
testcase_09 AC 72 ms
73,948 KB
testcase_10 AC 72 ms
73,584 KB
testcase_11 AC 73 ms
74,292 KB
testcase_12 AC 74 ms
74,496 KB
testcase_13 AC 74 ms
74,580 KB
testcase_14 AC 71 ms
75,152 KB
testcase_15 AC 38 ms
54,004 KB
testcase_16 AC 38 ms
53,856 KB
testcase_17 AC 39 ms
53,168 KB
testcase_18 AC 39 ms
53,336 KB
testcase_19 AC 39 ms
53,688 KB
testcase_20 AC 38 ms
53,500 KB
testcase_21 AC 41 ms
54,628 KB
testcase_22 AC 39 ms
53,080 KB
testcase_23 AC 39 ms
53,672 KB
testcase_24 AC 52 ms
62,868 KB
testcase_25 AC 43 ms
59,608 KB
testcase_26 AC 44 ms
61,084 KB
testcase_27 AC 51 ms
64,104 KB
testcase_28 AC 71 ms
73,148 KB
testcase_29 AC 70 ms
72,372 KB
testcase_30 AC 73 ms
73,804 KB
testcase_31 AC 64 ms
70,516 KB
testcase_32 AC 63 ms
68,476 KB
testcase_33 AC 51 ms
64,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda :sys.stdin.readline()[:-1]
ni = lambda :int(input())
na = lambda :list(map(int,input().split()))
yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES")
no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO")
#######################################################################

n = ni()
X = [int(x) for x in input()]

m = 11
mod = 998244353

def solve(n, X):
    dp = [[0 for i in range(4)] for i in range(m)]
    dp[0][0] = 1

    for i in range(n):
        ndp = [[0 for i in range(4)] for i in range(m)]
        ndp, dp = dp, ndp
        for j in range(m):
            for k in range(4):
                p, q = k//2, k%2
                if ndp[j][k] == 0:continue
                for x in range(10):
                    np = p
                    nq = q
                    xj = x if (n-i-1) % 2 else (-x)%m
                    nj = (j + xj)%m
                    if (xj+j * 2)%m == 0 and p:continue
                    if x != 0:
                        np = 1
                    if q == 0 and x > X[i]:
                        continue
                    if x < X[i]:
                        nq = 1
                    nk = np*2 + nq
                    dp[nj][nk] += ndp[j][k]
                    dp[nj][nk] %= mod

    return (dp[0][2]+dp[0][3])%mod

e0 = set()
e1 = {0}
def f(s, x):
    return set([y * 10 + x for y in s])
def merge(s1, s2):
    return s1 | s2

def solve_greedy(n, X):
    dp = [[e0 for i in range(4)] for i in range(m)]
    dp[0][0] = e1

    for i in range(n):
        ndp = [[set() for i in range(4)] for i in range(m)]
        ndp, dp = dp, ndp
        for j in range(m):
            for k in range(4):
                p, q = k//2, k%2
                if len(ndp[j][k]) == 0:continue
                for x in range(10):
                    np = p
                    nq = q
                    xj = x if (n-i-1) % 2 else (-x)%m
                    nj = (j + xj)%m
                    if (xj+j * 2)%m == 0 and p:continue
                    if x != 0:
                        np = 1
                    if q == 0 and x > X[i]:
                        continue
                    if x < X[i]:
                        nq = 1
                    nk = np*2 + nq
                    dp[nj][nk] = merge(dp[nj][nk], f(ndp[j][k], x))
        #print(dp)
    return dp[0]

def solve2(n, X):
    dp = [[0 for i in range(4)] for i in range(m)]
    dp[0][0] = 1

    for i in range(n):
        ndp = [[0 for i in range(4)] for i in range(m)]
        ndp, dp = dp, ndp
        #print(ndp)
        for j in range(m):
            for k in range(4):
                p, q = k//2, k%2
                if ndp[j][k] == 0:continue
                for x in range(10):
                    np = p
                    nq = q
                    xj = x if (n-i) % 2 else (-x)%m
                    nj = (j + xj)%m
                    if x != 0:
                        np = 1
                    if q == 0 and x > X[i]:
                        continue
                    if x < X[i]:
                        nq = 1
                    nk = np*2 + nq
                    #print((j,p,q),(nj, np,nq))
                    dp[nj][nk] += ndp[j][k]
                    dp[nj][nk] %= mod

    return (dp[0][2]+dp[0][3])%mod

print(solve(n, X))
#print(solve_greedy(n, X))
#print(solve2(n, X))
0