結果

問題 No.2578 Jewelry Store
ユーザー suisensuisen
提出日時 2023-02-15 01:05:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,169 ms / 3,500 ms
コード長 3,303 bytes
コンパイル時間 458 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 123,968 KB
最終ジャッジ日時 2023-12-05 23:33:57
合計ジャッジ時間 19,125 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
68,232 KB
testcase_01 AC 60 ms
68,232 KB
testcase_02 AC 125 ms
78,328 KB
testcase_03 AC 110 ms
78,208 KB
testcase_04 AC 155 ms
78,468 KB
testcase_05 AC 100 ms
78,180 KB
testcase_06 AC 147 ms
78,200 KB
testcase_07 AC 158 ms
78,472 KB
testcase_08 AC 114 ms
78,332 KB
testcase_09 AC 156 ms
78,340 KB
testcase_10 AC 205 ms
78,348 KB
testcase_11 AC 100 ms
78,036 KB
testcase_12 AC 110 ms
78,212 KB
testcase_13 AC 117 ms
78,312 KB
testcase_14 AC 108 ms
78,188 KB
testcase_15 AC 109 ms
78,324 KB
testcase_16 AC 200 ms
78,348 KB
testcase_17 AC 159 ms
78,472 KB
testcase_18 AC 110 ms
78,180 KB
testcase_19 AC 135 ms
78,332 KB
testcase_20 AC 157 ms
78,472 KB
testcase_21 AC 157 ms
78,596 KB
testcase_22 AC 115 ms
78,340 KB
testcase_23 AC 118 ms
77,960 KB
testcase_24 AC 99 ms
77,964 KB
testcase_25 AC 111 ms
77,968 KB
testcase_26 AC 115 ms
78,208 KB
testcase_27 AC 306 ms
78,584 KB
testcase_28 AC 462 ms
78,596 KB
testcase_29 AC 475 ms
78,708 KB
testcase_30 AC 577 ms
79,364 KB
testcase_31 AC 301 ms
78,456 KB
testcase_32 AC 277 ms
115,060 KB
testcase_33 AC 308 ms
104,880 KB
testcase_34 AC 381 ms
121,112 KB
testcase_35 AC 163 ms
90,824 KB
testcase_36 AC 315 ms
123,968 KB
testcase_37 AC 222 ms
78,840 KB
testcase_38 AC 249 ms
80,248 KB
testcase_39 AC 240 ms
82,668 KB
testcase_40 AC 248 ms
80,248 KB
testcase_41 AC 236 ms
78,948 KB
testcase_42 AC 230 ms
79,220 KB
testcase_43 AC 293 ms
78,580 KB
testcase_44 AC 175 ms
78,332 KB
testcase_45 AC 260 ms
79,480 KB
testcase_46 AC 221 ms
82,672 KB
testcase_47 AC 1,012 ms
83,444 KB
testcase_48 AC 465 ms
122,816 KB
testcase_49 AC 2,169 ms
102,004 KB
testcase_50 AC 2,131 ms
101,628 KB
testcase_51 AC 221 ms
78,700 KB
testcase_52 AC 514 ms
79,348 KB
testcase_53 AC 128 ms
78,056 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)

def supset_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] = f[i + block] * f[i] % P
            offset += 2 * block
        block <<= 1

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

    prod = [1] * (1 << k)
    for i in range(k):
        prod[((1 << k) - 1) ^ (1 << i)] = -1

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

    ans = sum(prod) - (m == 1)

    print(ans * (-1) ** k % P)

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