結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー 👑 rin204rin204
提出日時 2023-12-20 23:34:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 816 ms / 3,000 ms
コード長 1,978 bytes
コンパイル時間 191 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 99,284 KB
最終ジャッジ日時 2023-12-20 23:34:50
合計ジャッジ時間 17,858 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 36 ms
53,460 KB
testcase_02 AC 36 ms
53,460 KB
testcase_03 AC 100 ms
76,360 KB
testcase_04 AC 61 ms
73,068 KB
testcase_05 AC 566 ms
77,500 KB
testcase_06 AC 508 ms
77,172 KB
testcase_07 AC 519 ms
77,536 KB
testcase_08 AC 532 ms
88,304 KB
testcase_09 AC 35 ms
53,460 KB
testcase_10 AC 36 ms
53,460 KB
testcase_11 AC 791 ms
97,128 KB
testcase_12 AC 746 ms
94,804 KB
testcase_13 AC 807 ms
97,484 KB
testcase_14 AC 767 ms
97,852 KB
testcase_15 AC 738 ms
97,520 KB
testcase_16 AC 798 ms
96,092 KB
testcase_17 AC 253 ms
79,228 KB
testcase_18 AC 315 ms
81,056 KB
testcase_19 AC 425 ms
84,112 KB
testcase_20 AC 302 ms
81,004 KB
testcase_21 AC 179 ms
77,512 KB
testcase_22 AC 572 ms
82,648 KB
testcase_23 AC 689 ms
86,836 KB
testcase_24 AC 780 ms
94,920 KB
testcase_25 AC 755 ms
95,296 KB
testcase_26 AC 775 ms
96,688 KB
testcase_27 AC 762 ms
95,884 KB
testcase_28 AC 758 ms
99,284 KB
testcase_29 AC 768 ms
95,572 KB
testcase_30 AC 770 ms
96,852 KB
testcase_31 AC 816 ms
96,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def next_permutation(P):
    n = len(P)
    for i in range(n - 2, -1, -1):
        if P[i] < P[i + 1]:
            l = i + 1
            r = n - 1
            while r > l:
                P[l], P[r] = P[r], P[l]
                l += 1
                r -= 1
            for j in range(i + 1, n):
                if P[i] < P[j]:
                    P[i], P[j] = P[j], P[i]
                    return True
    return False


def all_permutations(P):
    # 全列挙したい場合はソートしてある状態で渡す
    yield P
    while next_permutation(P):
        yield P


def prev_permutation(P):
    n = len(P)
    for i in range(n - 2, -1, -1):
        if P[i] > P[i + 1]:
            l = i + 1
            r = n - 1
            while r > l:
                P[l], P[r] = P[r], P[l]
                l += 1
                r -= 1
            for j in range(i + 1, n):
                if P[i] > P[j]:
                    P[i], P[j] = P[j], P[i]
                    return True
    return False


def rev_all_permutations(P):
    # 全列挙したい場合は逆順ソートしてある状態で渡す
    yield P
    while prev_permutation(P):
        yield P


n, k = map(int, input().split())
C = list(map(int, input().split()))
A = []
for i, c in enumerate(C, 1):
    A += [i] * c


def f(A):
    cnt = {}
    for P in all_permutations(A):
        tot = 0
        for p in P:
            tot = 10 * tot + p
        tot %= k
        cnt[tot] = cnt.get(tot, 0) + 1
    return cnt


se = set()
x = n // 2
times = pow(10, x, k)
ans = 0
for bit in range(1 << n):
    L = []
    R = []
    pc = 0
    for i in range(n):
        if bit >> i & 1:
            L.append(A[i])
            pc += 1
        else:
            R.append(A[i])

    if len(L) != x:
        continue
    tup = tuple(L)
    if tup in se:
        continue
    se.add(tup)

    cl = f(L)
    cr = f(R)
    for kr, vr in cr.items():
        kl = -kr * times % k
        ans += cl.get(kl, 0) * vr

print(ans)
0