結果

問題 No.2578 Jewelry Store
ユーザー suisensuisen
提出日時 2023-01-11 19:32:01
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,566 bytes
コンパイル時間 416 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 163,672 KB
最終ジャッジ日時 2023-12-05 23:32:02
合計ジャッジ時間 21,583 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
68,228 KB
testcase_01 AC 60 ms
68,228 KB
testcase_02 AC 130 ms
78,440 KB
testcase_03 AC 120 ms
78,164 KB
testcase_04 AC 165 ms
78,424 KB
testcase_05 WA -
testcase_06 AC 127 ms
78,312 KB
testcase_07 AC 171 ms
78,424 KB
testcase_08 AC 121 ms
78,164 KB
testcase_09 AC 172 ms
78,424 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 115 ms
78,292 KB
testcase_13 AC 117 ms
78,268 KB
testcase_14 AC 118 ms
78,152 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 206 ms
78,424 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 208 ms
78,424 KB
testcase_21 AC 205 ms
78,424 KB
testcase_22 AC 146 ms
78,184 KB
testcase_23 WA -
testcase_24 AC 126 ms
78,168 KB
testcase_25 AC 138 ms
78,184 KB
testcase_26 AC 147 ms
78,312 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 571 ms
79,320 KB
testcase_31 WA -
testcase_32 AC 269 ms
126,756 KB
testcase_33 WA -
testcase_34 AC 370 ms
134,016 KB
testcase_35 AC 166 ms
101,072 KB
testcase_36 AC 304 ms
136,268 KB
testcase_37 AC 227 ms
78,796 KB
testcase_38 AC 253 ms
79,328 KB
testcase_39 WA -
testcase_40 AC 250 ms
80,216 KB
testcase_41 WA -
testcase_42 AC 239 ms
78,688 KB
testcase_43 AC 282 ms
78,792 KB
testcase_44 WA -
testcase_45 AC 255 ms
78,804 KB
testcase_46 WA -
testcase_47 AC 1,045 ms
84,680 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 226 ms
78,924 KB
testcase_52 AC 511 ms
79,564 KB
testcase_53 AC 139 ms
78,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from typing import List

input = sys.stdin.readline

#BEGIN: https://qiita.com/Kiri8128/items/eca965fe86ea5f4cbb98

def gcd(a: int, b: int) -> int:
    while b: a, b = b, a % b
    return a

def isPrimeMR(n: int):
    d = n - 1
    d = d // (d & -d)
    L = [2]
    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: int):
    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
        x = ys = y
        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

#END: https://qiita.com/Kiri8128/items/eca965fe86ea5f4cbb98

def divisors(factorized):
    res = [1]
    for p, c in factorized:
        siz = len(res)
        for i in range(siz):
            d = res[i]
            for _ in range(c):
                d *= p
                res.append(d)
    res.sort()
    return res

P = 998244353

t, m = map(int, input().split())
pf = primeFactor(m).keys()
k = len(pf)

m_div_p = [m // p for p in pf]

def popcount_parity_16(x: int):
    x = x - ((x >> 1) & 0x5555)
    x = (x & 0x3333) + ((x >> 2) & 0x3333)
    x = (x + (x >> 4)) & 0x0f0f
    x = x + (x >> 8)
    return x & 1

def subset_zeta_product(f: List[int]):
    block = 1
    while block < 1 << k:
        offset = 0
        while offset < 1 << k:
            for i in range(offset, offset + block):
                f[i + block] = f[i + block] * f[i] % P
            offset += 2 * block
        block <<= 1

def solve():
    n, x0, c, d = map(int, input().split())

    w = [x0]
    for i in range(n - 1):
        w.append((c * w[i] + d) % P)

    a = list(map(int, input().split()))

    zeta = [1] * (1 << k)

    for ai, wi in zip(a, w):
        if m % ai:
            continue

        t = 0
        for j, mp in enumerate(m_div_p):
            t |= (mp % ai != 0) << j

        zeta[t] = zeta[t] * (1 + wi) % P
    
    subset_zeta_product(zeta)

    ans = 0
    for s in range(1 << k):
        if popcount_parity_16(s):
            ans -= zeta[s]
        else:
            ans += zeta[s]
    if m == 1:
        ans -= 1

    print(ans % P)

for _ in range(t):
    solve()
0