Contest Duration: - (local time) (120 minutes) Back to Home

Submission #20952464

Source Code Expand

Copy
MOD = 998_244_353

def geom_seq_sum(r, n, MOD):
"""return \sum_{i=0}^{n-1} r^i"""
if r == 1:
return n % MOD
r1 = r - 1
return ((pow(r, n, r1 * MOD) - 1) // r1) % MOD

def geom_seq_sum_1(r,n,MOD):
"""
return \sum_{i=0}^{n-1} ir^i mod MOD
"""
if r == 1:
return n*(n-1)//2%MOD
r1 = r-1
x = pow(r, n, r1 * r1 * MOD)
return (-(x-1) // r1 + (n-1) * x + 1) // r1 % MOD

def main(N, M):
ans = 0
"""
size == N
"""
ans += pow(M, N, MOD)
if N == 1:
return ans
"""
size == N - 1
"""
for x in range(1, M):
ans += 2 * pow(x, N - 1, MOD)

c = pow(M, N-1, MOD)
M_inv = pow(M, -1, MOD)

for H in range(1, M + 1):
s = 0
r1 = (M - H + 1) * M_inv
r2 = (M - H) * M_inv
x = r1 * geom_seq_sum(r1, N - 2, MOD)
x -= r2 * geom_seq_sum(r2, N - 2, MOD)
ans += x * c * 2 * (H - 1)

for H in range(1, M + 1):
s = 0
c = pow(M, N - 3, MOD)
r1 = (M - H + 1) * M_inv
r2 = (M - H) * M_inv
s += (N-2) * (M-H+1) * geom_seq_sum(r1, N-2, MOD)
s -= (N-2) * (M-H) * geom_seq_sum(r2, N-2, MOD)

s -= (M - H + 1) * geom_seq_sum_1(r1, N-2, MOD)
s += (M - H) * geom_seq_sum_1(r2, N-2, MOD)
ans += (H - 1) * (H - 1) * c * s
return ans % MOD

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

print(main(a, b))

#### Submission Info

Submission Time 2021-03-15 04:58:17+0900 C - Sequence Scores maspy Python (3.8.2) 600 1478 Byte AC 164 ms 9236 KB

#### Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
 AC × 3
 AC × 22
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 29 ms 9096 KB
02.txt AC 49 ms 9232 KB
03.txt AC 96 ms 9236 KB
04.txt AC 129 ms 9096 KB
05.txt AC 19 ms 8904 KB
06.txt AC 79 ms 9236 KB
07.txt AC 104 ms 9044 KB
08.txt AC 138 ms 9232 KB
09.txt AC 40 ms 8912 KB
10.txt AC 72 ms 8896 KB
11.txt AC 100 ms 9092 KB
12.txt AC 141 ms 9116 KB
13.txt AC 55 ms 9008 KB
14.txt AC 85 ms 8916 KB
15.txt AC 100 ms 9092 KB
16.txt AC 144 ms 8904 KB
17.txt AC 18 ms 9020 KB
18.txt AC 157 ms 9112 KB
19.txt AC 164 ms 9088 KB
s1.txt AC 21 ms 8912 KB
s2.txt AC 21 ms 9088 KB
s3.txt AC 19 ms 9112 KB