結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
65,328 KB
testcase_01 AC 70 ms
65,328 KB
testcase_02 AC 162 ms
78,396 KB
testcase_03 AC 125 ms
78,244 KB
testcase_04 AC 188 ms
78,376 KB
testcase_05 AC 119 ms
78,344 KB
testcase_06 AC 134 ms
78,396 KB
testcase_07 AC 211 ms
78,396 KB
testcase_08 AC 120 ms
78,372 KB
testcase_09 AC 180 ms
78,392 KB
testcase_10 AC 259 ms
78,520 KB
testcase_11 AC 131 ms
78,344 KB
testcase_12 AC 123 ms
78,384 KB
testcase_13 AC 126 ms
78,356 KB
testcase_14 AC 144 ms
78,368 KB
testcase_15 AC 119 ms
78,372 KB
testcase_16 AC 271 ms
78,376 KB
testcase_17 AC 185 ms
78,520 KB
testcase_18 AC 122 ms
78,496 KB
testcase_19 AC 154 ms
78,384 KB
testcase_20 AC 191 ms
78,372 KB
testcase_21 AC 210 ms
78,380 KB
testcase_22 AC 124 ms
78,140 KB
testcase_23 AC 139 ms
78,012 KB
testcase_24 AC 110 ms
77,888 KB
testcase_25 AC 117 ms
77,888 KB
testcase_26 AC 117 ms
78,152 KB
testcase_27 AC 303 ms
78,524 KB
testcase_28 AC 547 ms
78,676 KB
testcase_29 AC 488 ms
78,672 KB
testcase_30 AC 630 ms
78,796 KB
testcase_31 AC 299 ms
78,652 KB
testcase_32 AC 262 ms
127,000 KB
testcase_33 AC 284 ms
107,520 KB
testcase_34 AC 371 ms
121,808 KB
testcase_35 AC 161 ms
93,692 KB
testcase_36 AC 305 ms
124,312 KB
testcase_37 AC 232 ms
78,508 KB
testcase_38 AC 249 ms
79,404 KB
testcase_39 AC 268 ms
83,484 KB
testcase_40 AC 240 ms
79,544 KB
testcase_41 AC 237 ms
78,628 KB
testcase_42 AC 246 ms
78,644 KB
testcase_43 AC 304 ms
78,916 KB
testcase_44 AC 221 ms
78,384 KB
testcase_45 AC 257 ms
78,516 KB
testcase_46 AC 233 ms
80,612 KB
testcase_47 AC 1,306 ms
79,684 KB
testcase_48 AC 454 ms
158,768 KB
testcase_49 AC 2,892 ms
110,336 KB
testcase_50 AC 2,772 ms
113,732 KB
testcase_51 AC 246 ms
79,004 KB
testcase_52 AC 541 ms
79,048 KB
testcase_53 AC 143 ms
78,336 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