結果
問題 | No.1753 Don't cheat. |
ユーザー |
|
提出日時 | 2021-06-19 17:05:07 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,309 ms / 3,000 ms |
コード長 | 2,812 bytes |
コンパイル時間 | 633 ms |
コンパイル使用メモリ | 82,248 KB |
実行使用メモリ | 85,892 KB |
最終ジャッジ日時 | 2024-12-31 18:24:13 |
合計ジャッジ時間 | 29,652 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 30 |
ソースコード
MOD = 998244353half = pow(2, MOD - 2, MOD)def int2frac(n, m=None, N = 10000, D = 10000):"""Return (r, s) s.t. r = s * n (mod m) if such a pair exists.Otherwise, return (0, 0).Parameters----------n: an integer that will be represented as a fraction.m: A modulus used to represent n.N: An upperbound of r, which is the numerator of n.D: An upperbound of s, which is the denominator of n."""def gcd(a, b):while b:a, b = b, a % breturn aif m is None:m = MODv = (m, 0)w = (n, 1)while w[0] > N:q = v[0] // w[0]v, w = w, (v[0] - q * w[0], v[1] - q * w[1])if w[1] < 0:w = (-w[0], -w[1])if w[1] <= D and gcd(w[0], w[1]) == 1:return welse:return (0, 0)def fwht(a) -> None:"""In-place Fast Walsh–Hadamard Transform of array a.Reference: https://en.wikipedia.org/wiki/Fast_Walsh%E2%80%93Hadamard_transform"""h = 1while h < len(a):for i in range(0, len(a), h * 2):for j in range(i, i + h):x = a[j]y = a[j + h]a[j] = (x + y) % MODa[j + h] = (x - y) % MODh *= 2def ifwht(a) -> None:"""In-place Inverse Fast Walsh–Hadamard Transform of array a.Reference: https://en.wikipedia.org/wiki/Fast_Walsh%E2%80%93Hadamard_transform"""h = 1while h < len(a):for i in range(0, len(a), h * 2):for j in range(i, i + h):x = a[j]y = a[j + h]a[j] = (x + y) * half % MODa[j + h] = (x - y) * half % MODh *= 2N = int(input())As = list(map(int, input().split()))assert 1 <= N <= 2 * 10 ** 3assert all(0 <= A <= 10 ** 5 for A in As)assert N + 1 == len(As)assert As[0]sumAs = sum(As)invsumAs = pow(sum(As), MOD - 2, MOD)for i in range(N + 1):As[i] *= invsumAsAs[i] %= MOD# print([int2frac(A) for A in As])z = 1 << N.bit_length()A0 = [0] * zA0[0] = As[0]fwht(A0)As_fwht = As + [0] * (z - N - 1)fwht(As_fwht)assert all((As[0] + 1 - As_fwht[k]) % MOD != 0 for k in range(z))q = [pow(As[0] + 1 - As_fwht[k], MOD - 2, MOD) * A0[k] % MOD for k in range(z)]ifwht(q)ps = []for x in range(1, N + 1):A0 = [0] * zA0[0] = As[x]fwht(A0)Ax = As + [0] * (z - N - 1)Ax[0] = 0Ax[x] = 0fwht(Ax)assert all((1 - Ax[k]) % MOD != 0 for k in range(z))p = [pow(1 - Ax[k], MOD - 2, MOD) * A0[k] % MOD for k in range(z)]ifwht(p)ps.append(p)# print(x, [int2frac(e) for e in p])answer = q[0]for p in ps:for xor in range(z):answer += p[xor] * q[xor] % MODanswer %= MODprint((1 - answer) % MOD)