結果

問題 No.2578 Jewelry Store
ユーザー chineristACchineristAC
提出日時 2023-12-06 00:59:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,767 ms / 3,500 ms
コード長 4,314 bytes
コンパイル時間 490 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 159,512 KB
最終ジャッジ日時 2024-09-27 00:50:11
合計ジャッジ時間 21,382 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
64,896 KB
testcase_01 AC 67 ms
65,152 KB
testcase_02 AC 141 ms
78,720 KB
testcase_03 AC 122 ms
78,720 KB
testcase_04 AC 186 ms
78,892 KB
testcase_05 AC 117 ms
78,720 KB
testcase_06 AC 134 ms
79,000 KB
testcase_07 AC 185 ms
78,720 KB
testcase_08 AC 120 ms
78,592 KB
testcase_09 AC 183 ms
79,004 KB
testcase_10 AC 247 ms
79,048 KB
testcase_11 AC 130 ms
78,744 KB
testcase_12 AC 124 ms
78,976 KB
testcase_13 AC 133 ms
78,592 KB
testcase_14 AC 118 ms
78,592 KB
testcase_15 AC 119 ms
78,848 KB
testcase_16 AC 248 ms
78,976 KB
testcase_17 AC 182 ms
79,224 KB
testcase_18 AC 119 ms
79,028 KB
testcase_19 AC 151 ms
78,948 KB
testcase_20 AC 185 ms
78,976 KB
testcase_21 AC 186 ms
78,720 KB
testcase_22 AC 120 ms
78,464 KB
testcase_23 AC 131 ms
78,464 KB
testcase_24 AC 103 ms
78,464 KB
testcase_25 AC 121 ms
78,444 KB
testcase_26 AC 118 ms
78,700 KB
testcase_27 AC 301 ms
79,324 KB
testcase_28 AC 503 ms
79,332 KB
testcase_29 AC 487 ms
78,976 KB
testcase_30 AC 613 ms
78,848 KB
testcase_31 AC 297 ms
78,720 KB
testcase_32 AC 269 ms
127,488 KB
testcase_33 AC 291 ms
108,048 KB
testcase_34 AC 348 ms
122,232 KB
testcase_35 AC 160 ms
93,824 KB
testcase_36 AC 307 ms
124,704 KB
testcase_37 AC 233 ms
79,160 KB
testcase_38 AC 245 ms
80,000 KB
testcase_39 AC 244 ms
84,076 KB
testcase_40 AC 241 ms
80,188 KB
testcase_41 AC 239 ms
79,360 KB
testcase_42 AC 252 ms
78,848 KB
testcase_43 AC 298 ms
79,104 KB
testcase_44 AC 193 ms
79,016 KB
testcase_45 AC 253 ms
78,976 KB
testcase_46 AC 225 ms
81,280 KB
testcase_47 AC 1,275 ms
80,060 KB
testcase_48 AC 462 ms
159,512 KB
testcase_49 AC 2,767 ms
111,020 KB
testcase_50 AC 2,708 ms
114,028 KB
testcase_51 AC 233 ms
79,360 KB
testcase_52 AC 516 ms
79,104 KB
testcase_53 AC 141 ms
78,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def isPrimeMR(n):
    if n==1:
        return 0
    d = n - 1
    d = d // (d & -d)
    L = [2, 3, 5, 7, 11, 13, 17]
    if n in L:
        return 1
    for a in L:
        t = d
        y = pow(a, t, n)
        if y == 1: continue
        while y != n - 1:
            y = (y * y) % n
            if y == 1 or t == n - 1: return 0
            t <<= 1
    return 1
def findFactorRho(n):
    from math import gcd
    m = 1 << n.bit_length() // 8
    for c in range(1, 99):
        f = lambda x: (x * x + c) % n
        y, r, q, g = 2, 1, 1, 1
        while g == 1:
            x = y
            for i in range(r):
                y = f(y)
            k = 0
            while k < r and g == 1:
                ys = y
                for i in range(min(m, r - k)):
                    y = f(y)
                    q = q * abs(x - y) % n
                g = gcd(q, n)
                k += m
            r <<= 1
        if g == n:
            g = 1
            while g == 1:
                ys = f(ys)
                g = gcd(abs(x - ys), n)
        if g < n:
            if isPrimeMR(g): return g
            elif isPrimeMR(n // g): return n // g
            return findFactorRho(g)
def primeFactor(n):
    i = 2
    ret = {}
    rhoFlg = 0
    while i*i <= n:
        k = 0
        while n % i == 0:
            n //= i
            k += 1
        if k: ret[i] = k
        i += 1 + i % 2
        if i == 101 and n >= 2 ** 20:
            while n > 1:
                if isPrimeMR(n):
                    ret[n], n = 1, 1
                else:
                    rhoFlg = 1
                    j = findFactorRho(n)
                    k = 0
                    while n % j == 0:
                        n //= j
                        k += 1
                    ret[j] = k

    if n > 1: ret[n] = 1
    if rhoFlg: ret = {x: ret[x] for x in sorted(ret)}
    return ret

import sys
from itertools import permutations
from heapq import heappop,heappush
from collections import deque
import random
import bisect

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

K = 17
popcount = [0] * (1<<K)
for i in range(K):
    for S in range(1<<K):
        if S >> i & 1:
            popcount[S] ^= 1

mod = 998244353

T,M = mi()

pf = primeFactor(M)
"""
M_div = [1]
for p in pf:
    e = pf[p]
    nxt_M_div = []
    for a in M_div:
        t = 1
        for i in range(e+1):
            nxt_M_div.append(a * t)
            t *= p
    M_div = nxt_M_div

M_div.sort()
"""


M_prime = [pow(p,pf[p]) for p in pf]
M_n = len(M_prime)

def solve1(N,B,C,D,A):
    dic = {}
    w = B
    for a in A:
        if M % a:
            w = (C * w + D) % mod
            continue
        if a not in dic:
            dic[a] = 1
        dic[a] *= w + 1
        dic[a] %= mod

        w = (C * w + D) % mod
    
    for p in pf:
        for d in M_div:
            if d not in dic:
                continue
            if M % (d*p):
                continue
            if d*p not in dic:
                dic[d*p] = 1
            dic[d*p] *= dic[d]
            dic[d*p] %= mod
        
    
    for d in dic:
        dic[d] = (dic[d] - 1 ) % mod
    
    for p in pf:
        for d in M_div[::-1]:
            if d % p:
                continue
            if d // p not in dic:
                continue
            dic[d] -= dic[d//p]
            dic[d] %= mod
    
    
    if M not in dic:
        return 0
    return dic[M]


def solve2(N,B,C,D,A):
    dp = [1] * (1<<M_n)
    w = B
    for a in A:
        if M % a:
            w = (C * w + D) % mod
            continue

        S = 0
        for i in range(M_n):
            if a % M_prime[i] == 0:
                S ^= 1<<i
        dp[S] *= 1 + w
        dp[S] %= mod

        w = (C * w + D) % mod
    
    for i in range(M_n):
        dp[2**i] *= -1
        dp[2**i] %= mod
    
    
    
    for i in range(M_n):
        t = 1<<i
        for S in range(1<<M_n):
            if S & t:
                dp[S] *= dp[S^t]
                dp[S] %= mod
    
    
    res = sum(dp) % mod
    if M_n & 1:
        res = (mod-res) % mod
    return res

    

for _ in range(T):
    N,B,C,D = mi()
    A = li()
    if M!=1:
        print(solve2(N,B,C,D,A))
    else:
        print((solve2(N,B,C,D,A)-1) % mod)
    #print(solve1(N,B,C,D,A))

0