結果

問題 No.2578 Jewelry Store
ユーザー chineristACchineristAC
提出日時 2023-12-06 00:53:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,499 ms / 3,500 ms
コード長 4,324 bytes
コンパイル時間 1,139 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 158,764 KB
最終ジャッジ日時 2023-12-06 00:53:58
合計ジャッジ時間 23,908 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
65,456 KB
testcase_01 AC 62 ms
65,456 KB
testcase_02 AC 126 ms
78,392 KB
testcase_03 AC 115 ms
78,244 KB
testcase_04 AC 182 ms
78,392 KB
testcase_05 AC 109 ms
78,344 KB
testcase_06 AC 126 ms
78,392 KB
testcase_07 AC 201 ms
78,260 KB
testcase_08 AC 117 ms
78,372 KB
testcase_09 AC 182 ms
78,524 KB
testcase_10 AC 259 ms
78,396 KB
testcase_11 AC 110 ms
78,344 KB
testcase_12 AC 115 ms
78,372 KB
testcase_13 AC 116 ms
78,372 KB
testcase_14 AC 138 ms
78,360 KB
testcase_15 AC 113 ms
78,376 KB
testcase_16 AC 260 ms
78,384 KB
testcase_17 AC 180 ms
78,528 KB
testcase_18 AC 111 ms
78,248 KB
testcase_19 AC 146 ms
78,524 KB
testcase_20 AC 182 ms
78,392 KB
testcase_21 AC 208 ms
78,396 KB
testcase_22 AC 114 ms
78,260 KB
testcase_23 AC 124 ms
78,016 KB
testcase_24 AC 99 ms
78,028 KB
testcase_25 AC 112 ms
78,036 KB
testcase_26 AC 112 ms
78,144 KB
testcase_27 AC 288 ms
78,652 KB
testcase_28 AC 548 ms
78,812 KB
testcase_29 AC 487 ms
78,548 KB
testcase_30 AC 672 ms
79,056 KB
testcase_31 AC 285 ms
78,396 KB
testcase_32 AC 256 ms
127,004 KB
testcase_33 AC 277 ms
107,636 KB
testcase_34 AC 372 ms
121,840 KB
testcase_35 AC 154 ms
93,692 KB
testcase_36 AC 294 ms
124,408 KB
testcase_37 AC 222 ms
78,508 KB
testcase_38 AC 238 ms
79,532 KB
testcase_39 AC 257 ms
83,740 KB
testcase_40 AC 232 ms
79,668 KB
testcase_41 AC 227 ms
78,756 KB
testcase_42 AC 223 ms
78,516 KB
testcase_43 AC 290 ms
78,788 KB
testcase_44 AC 214 ms
78,512 KB
testcase_45 AC 241 ms
78,644 KB
testcase_46 AC 215 ms
81,040 KB
testcase_47 AC 1,391 ms
79,564 KB
testcase_48 AC 439 ms
158,764 KB
testcase_49 AC 3,320 ms
110,044 KB
testcase_50 AC 3,499 ms
114,000 KB
testcase_51 AC 225 ms
79,132 KB
testcase_52 AC 541 ms
79,184 KB
testcase_53 AC 133 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>>i & 1 == 0:
                dp[S^t] *= dp[S]
                dp[S^t] %= 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