結果

問題 No.1731 Product of Subsequence
ユーザー とりゐとりゐ
提出日時 2023-05-23 21:28:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 522 ms / 2,000 ms
コード長 1,581 bytes
コンパイル時間 1,629 ms
コンパイル使用メモリ 87,140 KB
実行使用メモリ 80,532 KB
最終ジャッジ日時 2023-08-24 21:09:32
合計ジャッジ時間 9,498 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 224 ms
79,148 KB
testcase_01 AC 99 ms
77,288 KB
testcase_02 AC 98 ms
77,452 KB
testcase_03 AC 99 ms
77,416 KB
testcase_04 AC 99 ms
77,268 KB
testcase_05 AC 100 ms
77,440 KB
testcase_06 AC 100 ms
77,204 KB
testcase_07 AC 97 ms
77,284 KB
testcase_08 AC 160 ms
79,004 KB
testcase_09 AC 102 ms
77,768 KB
testcase_10 AC 308 ms
80,016 KB
testcase_11 AC 251 ms
79,296 KB
testcase_12 AC 105 ms
78,028 KB
testcase_13 AC 116 ms
78,424 KB
testcase_14 AC 254 ms
80,496 KB
testcase_15 AC 100 ms
77,456 KB
testcase_16 AC 98 ms
77,024 KB
testcase_17 AC 97 ms
77,256 KB
testcase_18 AC 522 ms
80,532 KB
testcase_19 AC 116 ms
78,236 KB
testcase_20 AC 430 ms
80,060 KB
testcase_21 AC 299 ms
79,088 KB
testcase_22 AC 105 ms
78,036 KB
testcase_23 AC 102 ms
77,720 KB
testcase_24 AC 106 ms
78,008 KB
testcase_25 AC 104 ms
78,084 KB
testcase_26 AC 150 ms
79,300 KB
testcase_27 AC 169 ms
79,384 KB
testcase_28 AC 187 ms
79,380 KB
testcase_29 AC 155 ms
79,552 KB
testcase_30 AC 237 ms
79,272 KB
testcase_31 AC 104 ms
77,940 KB
testcase_32 AC 101 ms
77,696 KB
testcase_33 AC 104 ms
78,132 KB
testcase_34 AC 130 ms
79,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod=10**9+7
table_size=10**4

fac=[1]*(table_size+1)
finv=[1]*(table_size+1)

for i in range(2,table_size+1):
  fac[i]=fac[i-1]*i%mod
finv[table_size]=pow(fac[table_size],mod-2,mod)
for i in range(table_size-1,-1,-1):
  finv[i]=finv[i+1]*(i+1)%mod

def rebuild(n):
  global table_size,fac,finv
  fac+=[0]*(n-table_size)
  fac+=[0]*(n-table_size)
  finv+=[0]*(n-table_size)
  for i in range(table_size+1,n+1):
    fac[i]=fac[i-1]*i%mod
  finv[n]=inv(fac[n])
  for i in range(n-1,table_size,-1):
    finv[i]=finv[i+1]*(i+1)%mod
  table_size=n

def binom(n,k):
  if n<0 or k<0:
    return 0
  if k>n:
    return 0
  if n>table_size:
    rebuild(n+10**4)
  return (fac[n]*finv[k]%mod)*finv[n-k]%mod

def fpow(x,k):
  res=1
  while k:
    if k&1:
      res=res*x%mod
    x=x*x%mod
    k>>=1
  return res

def inv(a):
  if a<table_size:
    return fac[a-1]*finv[a]%mod
  return fpow(a,mod-2)


from math import gcd
from collections import defaultdict
n,k=map(int,input().split())
a=list(map(int,input().split()))
pow2=[1]*(n+1)
for i in range(1,n+1):
  pow2[i]=pow2[i-1]*2%mod

if k==1:
  print(pow(2,n,mod)-1)
  exit()

cnt=defaultdict(int)
for i in a:
  cnt[gcd(k,i)]+=1

dp=defaultdict(int)
dp[1]=1

for i in cnt:
  ndp=defaultdict(int)
  c=cnt[i]
  for frm in dp:
    rem=pow2[c]
    tmp=frm
    for j in range(c+1):
      nxt=gcd(k,tmp*i)
      if tmp==nxt:
        ndp[tmp]+=dp[frm]*rem%mod
        ndp[tmp]%=mod
        break
      else:
        ndp[tmp]+=dp[frm]*binom(c,j)%mod
        ndp[tmp]%=mod
        rem-=binom(c,j)
        rem%=mod
        tmp=nxt
  dp=ndp

print(dp[k])
0