結果
| 問題 |
No.1634 Sorting Integers (Multiple of K) Hard
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-12-20 23:34:31 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 833 ms / 3,000 ms |
| コード長 | 1,978 bytes |
| コンパイル時間 | 189 ms |
| コンパイル使用メモリ | 82,096 KB |
| 実行使用メモリ | 99,564 KB |
| 最終ジャッジ日時 | 2024-09-27 10:15:56 |
| 合計ジャッジ時間 | 18,271 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 28 |
ソースコード
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)