結果
| 問題 |
No.1731 Product of Subsequence
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-03-25 01:01:19 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,537 bytes |
| コンパイル時間 | 272 ms |
| コンパイル使用メモリ | 82,976 KB |
| 実行使用メモリ | 83,780 KB |
| 最終ジャッジ日時 | 2025-03-25 01:01:28 |
| 合計ジャッジ時間 | 7,650 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 29 WA * 2 |
ソースコード
## https://yukicoder.me/problems/no/1731
import math
MOD = 10 ** 9 + 7
def calc_gcd(A, B):
"""
正の整数A, Bの最大公約数を計算する
"""
a = max(A, B)
b = min(A, B)
while a % b > 0:
c = a % b
a = b
b = c
return b
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))
divisors = []
for p in range(1, sqrt_k + 1):
if K % p == 0:
q = K // p
divisors.append(p)
if q != p:
divisors.append(q)
divisors.sort()
# 注目すべき素数の洗い出し
# Aについてprimesで割った時のベクトルを計算
state_array = []
for a in A:
z = calc_gcd(a, K)
state_array.append(z)
# dpによって計算していく
dp = {1: 1}
for state in state_array:
new_dp = dp.copy()
for key, value in dp.items():
# A[i]を部分列に加える
new_key = calc_gcd(key * state, K)
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 K in dp:
print(dp[K])
else:
print(0)
if __name__ == "__main__":
main()