結果
| 問題 |
No.1731 Product of Subsequence
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-03-25 00:42:52 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,966 bytes |
| コンパイル時間 | 384 ms |
| コンパイル使用メモリ | 82,096 KB |
| 実行使用メモリ | 111,892 KB |
| 最終ジャッジ日時 | 2025-03-25 00:42:59 |
| 合計ジャッジ時間 | 7,043 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 4 |
| other | TLE * 1 -- * 30 |
ソースコード
## https://yukicoder.me/problems/no/1731
import math
MOD = 10 ** 9 + 7
def main():
N, K = map(int, input().split())
A = list(map(int, input().split()))
if K == 1:
cnt = 0
for a in A:
if a == 1:
cnt += 1
ans = pow(2, cnt, MOD) - 1
print(ans)
return
# Kの素因数分解
sqrt_k = int(math.sqrt(K))
prime_map = {}
for p in range(2, sqrt_k + 1):
if K % p == 0:
c_p = 0
while K % p == 0:
c_p += 1
K //= p
prime_map[p] = c_p
if K > 1:
prime_map[K] = 1
# 注目すべき素数の洗い出し
prime_array = [(p, v) for p, v in prime_map.items()]
primes = [p[0] for p in prime_array]
max_states = tuple([p[1] for p in prime_array])
# Aについてprimesで割った時のベクトルを計算
state_array = []
for a in A:
array = []
for p, ms in zip(primes, max_states):
a_cnt = 0
while a % p== 0:
a //= p
a_cnt += 1
if a_cnt == ms:
break
array.append(a_cnt)
state_array.append(tuple(array))
# dpによって計算していく
init_state = tuple([0] * len(primes))
dp = {init_state: 1}
for state in state_array:
new_dp = dp.copy()
for key, value in dp.items():
# A[i]を部分列に加える
new_key = [0] * len(primes)
for i in range(len(primes)):
new_key[i] = min(key[i] + state[i], max_states[i])
new_key = tuple(new_key)
if new_key not in new_dp:
new_dp[new_key] = 0
new_dp[new_key] += value
new_dp[new_key] %= MOD
dp = new_dp
if max_states in dp:
print(dp[max_states])
else:
print(0)
if __name__ == "__main__":
main()