結果

問題 No.1633 Sorting Integers (Multiple of 2^K)
ユーザー googol_S0googol_S0
提出日時 2021-07-30 21:41:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,540 ms / 2,000 ms
コード長 1,064 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 276,352 KB
最終ジャッジ日時 2024-09-15 23:29:46
合計ジャッジ時間 23,695 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(input())
C=list(map(int,input().split()))
A=[]
for i in range(9):
  for j in range(C[i]):
    A.append(i+1)
ANS=0
M=(N+1)>>1
L=N>>1
t=0
for u in range(5,-1,-1):
  t+=(1<<u)
  mod=1<<t
  P=[pow(10,i,mod) for i in range(N+1)]
  DP=[set() for i in range(1<<N)]
  DP[0]=set([0])
  for i in range(1<<N):
    c=bin(i).count('1')
    if c>=M:
      continue
    for j in range(N):
      if (i>>j)&1:
        continue
      if j>0 and A[j-1]==A[j] and (i>>(j-1))&1==0:
        continue
      for k in DP[i]:
        DP[i|(1<<j)].add((k*10+A[j])%mod)
  for i in range(1<<N):
    c=bin(i).count('1')
    if c>=L:
      x=((1<<N)-1)^i
      y=[0]*10
      for j in range(9):
        y[j+1]=y[j]+C[j]
      z=0
      p=0
      for k in range(N):
        while y[p]==k:
          p+=1
        if (x>>k)&1:
          z|=(1<<y[p-1])
          y[p-1]+=1
      x=z
      if len(DP[x])>0 and len(DP[i])>0:
        for j in DP[i]:
          if (-j*P[N-c])%mod in DP[x]:
            ANS=t
            break
        if ANS==t:
          break
  if ANS<t:
    t-=(1<<u)
print(ANS)
0