結果

問題 No.2578 Jewelry Store
ユーザー suisensuisen
提出日時 2023-01-11 20:48:52
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,506 bytes
コンパイル時間 1,652 ms
コンパイル使用メモリ 82,152 KB
実行使用メモリ 134,560 KB
最終ジャッジ日時 2024-09-27 00:35:34
合計ジャッジ時間 17,605 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
67,712 KB
testcase_01 AC 67 ms
67,328 KB
testcase_02 AC 138 ms
79,104 KB
testcase_03 AC 121 ms
79,104 KB
testcase_04 AC 181 ms
78,976 KB
testcase_05 WA -
testcase_06 AC 137 ms
78,592 KB
testcase_07 AC 174 ms
78,848 KB
testcase_08 AC 127 ms
78,848 KB
testcase_09 AC 177 ms
79,092 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 127 ms
78,592 KB
testcase_13 AC 111 ms
78,368 KB
testcase_14 AC 114 ms
78,976 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 173 ms
78,976 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 179 ms
79,044 KB
testcase_21 AC 175 ms
78,976 KB
testcase_22 AC 124 ms
78,720 KB
testcase_23 WA -
testcase_24 AC 115 ms
78,464 KB
testcase_25 AC 129 ms
78,592 KB
testcase_26 AC 129 ms
78,720 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 426 ms
79,932 KB
testcase_31 WA -
testcase_32 AC 186 ms
115,840 KB
testcase_33 WA -
testcase_34 AC 225 ms
104,172 KB
testcase_35 AC 129 ms
88,920 KB
testcase_36 AC 204 ms
114,856 KB
testcase_37 AC 179 ms
78,788 KB
testcase_38 AC 180 ms
78,464 KB
testcase_39 WA -
testcase_40 AC 172 ms
79,360 KB
testcase_41 WA -
testcase_42 AC 190 ms
79,204 KB
testcase_43 AC 185 ms
79,104 KB
testcase_44 WA -
testcase_45 AC 188 ms
78,744 KB
testcase_46 WA -
testcase_47 AC 894 ms
84,556 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 187 ms
78,592 KB
testcase_52 AC 552 ms
91,612 KB
testcase_53 AC 137 ms
78,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from typing import List

from math import gcd

input = sys.stdin.readline

# https://qiita.com/Kiri8128/items/eca965fe86ea5f4cbb98
class PrimeFactorize:
    @staticmethod
    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

    @staticmethod
    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 PrimeFactorize.__isPrimeMR(g): return g
                elif PrimeFactorize.__isPrimeMR(n // g): return n // g
                return PrimeFactorize.__findFactorRho(g)

    @staticmethod
    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 PrimeFactorize.__isPrimeMR(n):
                        ret[n], n = 1, 1
                    else:
                        rhoFlg = 1
                        j = PrimeFactorize.__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

P = 998244353

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

parity = [(-1) ** bin(s).count('1') for s in range(1 << k)]

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

t_memo = dict()

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

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

    wi = x0
    for ai in map(int, input().split()):
        q, r = divmod(m, ai)
        if r == 0:
            if ai in t_memo:
                t = t_memo[ai]
            else:
                t = 0
                for j, p in enumerate(pf):
                    t |= (q % p != 0) << j
                t_memo[ai] = t
            prod[t] = prod[t] * (1 + wi) % P
        wi = (c * wi + d) % P
    
    subset_zeta_product(prod)

    ans = 0
    for s in range(1 << k):
        ans += parity[s] * prod[s]
    if m == 1:
        ans -= 1

    print(ans % P)

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