実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
1 から N の番号が付けられた N 人の人がおり、この中で一対一の勝敗のつくゲームを何度か行いました。N 人は最初にそれぞれ持ち点として 0 を持っており、各ゲームでは勝者の持ち点が 1 増え、敗者の持ち点が 1 減ります(持ち点が負になることもあります)。最終的に人 i\ (1\leq i\leq N-1) の持ち点が A_i になったとき、人 N の持ち点を求めてください。なお、ゲームの進行によらず最終的な人 N の持ち点は一意に定まることが示せます。
制約
- 2\leq N\leq 100
- -100\leq A_i\leq 100
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \ldots A_{N-1}
出力
答えを出力せよ。
入力例 1
4 1 -2 -1
出力例 1
2
最終的に人 1,2,3 の持ち点が 1,-2,-1 となるような進行として、例えば次のようなものが考えられます。
- 最初、人 1,2,3,4 の持ち点はそれぞれ 0,0,0,0 である。
- 人 1 と 2 が対戦して、人 1 が勝利する。4 人の持ち点はそれぞれ 1,-1,0,0 である。
- 人 1 と 4 が対戦して、人 4 が勝利する。4 人の持ち点はそれぞれ 0,-1,0,1 である。
- 人 1 と 2 が対戦して、人 1 が勝利する。4 人の持ち点はそれぞれ 1,-2,0,1 である。
- 人 2 と 3 が対戦して、人 2 が勝利する。4 人の持ち点はそれぞれ 1,-1,-1,1 である。
- 人 2 と 4 が対戦して、人 4 が勝利する。4 人の持ち点はそれぞれ 1,-2,-1,2 である。
この場合、人 4 の持ち点は 2 になります。ほかにも別の進行が考えられますが、どのような進行であっても人 4 の持ち点は 2 になります。
入力例 2
3 0 0
出力例 2
0
入力例 3
6 10 20 30 40 50
出力例 3
-150
Score: 100 points
Problem Statement
There are N people labeled 1 to N, who have played several one-on-one games without draws. Initially, each person started with 0 points. In each game, the winner's score increased by 1 and the loser's score decreased by 1 (scores can become negative). Determine the final score of person N if the final score of person i\ (1\leq i\leq N-1) is A_i. It can be shown that the final score of person N is uniquely determined regardless of the sequence of games.
Constraints
- 2 \leq N \leq 100
- -100 \leq A_i \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_{N-1}
Output
Print the answer.
Sample Input 1
4 1 -2 -1
Sample Output 1
2
Here is one possible sequence of games where the final scores of persons 1, 2, 3 are 1, -2, -1, respectively.
- Initially, persons 1, 2, 3, 4 have 0, 0, 0, 0 points, respectively.
- Persons 1 and 2 play, and person 1 wins. The players now have 1, -1, 0, 0 point(s).
- Persons 1 and 4 play, and person 4 wins. The players now have 0, -1, 0, 1 point(s).
- Persons 1 and 2 play, and person 1 wins. The players now have 1, -2, 0, 1 point(s).
- Persons 2 and 3 play, and person 2 wins. The players now have 1, -1, -1, 1 point(s).
- Persons 2 and 4 play, and person 4 wins. The players now have 1, -2, -1, 2 point(s).
In this case, the final score of person 4 is 2. Other possible sequences of games exist, but the score of person 4 will always be 2 regardless of the progression.
Sample Input 2
3 0 0
Sample Output 2
0
Sample Input 3
6 10 20 30 40 50
Sample Output 3
-150
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
3 問の問題からなる試験があり、それぞれの問題の配点は 1 点、2 点、4 点でした。
高橋君、青木君、すぬけ君の 3 人がこの試験を受け、 高橋君は A 点、青木君は B 点を取りました。
すぬけ君は、高橋君と青木君のうち少なくとも一方が解けた問題は解け、 2 人とも解けなかった問題は解けませんでした。
すぬけ君の点数を求めてください。
ただし、この問題の制約下で、すぬけ君の点数は一意に定まる事が証明できます。
制約
- 0\leq A,B \leq 7
- A,B は整数
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
すぬけ君の点数を整数で出力せよ。
入力例 1
1 2
出力例 1
3
高橋君は 1 点を取った事から、 1 点の問題のみを正解し、それ以外の 2 問は解けなかったことがわかります。
同様に、青木君は 2 点を取った事から、 2 点の問題のみを正解し、それ以外の 2 問は解けなかったことがわかります。
よって、すぬけ君は 1 点の問題と 2 点の問題を正解し、高橋君と青木君がともに解けなかった 4 点の問題はすぬけ君も解けなかったことになるので、3 点を取ったことがわかります。よって、3 を出力します。
入力例 2
5 3
出力例 2
7
高橋君は 5 点を取った事から、 1 点の問題と 4 点の問題を正解し、 2 点の問題は解けなかったことがわかります。
同様に、青木君は 3 点を取った事から、 1 点の問題と 2 点の問題を正解し、 4 点の問題は解けなかったことがわかります。
よって、3 問すべてについて、高橋君と青木君の少なくとも一方が正解しているため、すぬけ君はすベての問題に正解し、7 点を取ったことがわかります。 よって、7 を出力します。
入力例 3
0 0
出力例 3
0
高橋君と青木君は 2 人ともいずれの問題も解けていません。 よって、すぬけ君もいずれの問題も解けておらず、 0 を出力します。
Score : 100 points
Problem Statement
There was an exam consisting of three problems worth 1, 2, and 4 points.
Takahashi, Aoki, and Snuke took this exam. Takahashi scored A points, and Aoki scored B points.
Snuke solved all of the problems solved by at least one of Takahashi and Aoki, and failed to solve any of the problems solved by neither of them.
Find Snuke's score.
It can be proved that Snuke's score is uniquely determined under the Constraints of this problem.
Constraints
- 0\leq A,B \leq 7
- A and B are integers.
Input
The input is given from Standard Input in the following format:
A B
Output
Print Snuke's score as an integer.
Sample Input 1
1 2
Sample Output 1
3
Since Takahashi scored 1 point, we see that he solved only the 1-point problem and failed to solve the other two.
Similarly, since Aoki scored 2 points, we see that he solved only the 2-point problem and failed to solve the other two.
Therefore, Snuke must have solved the 1- and 2-point problems, but not the 4-point one, which Takahashi and Aoki both failed to solve, for a score of 3 points. Thus, 3 should be printed.
Sample Input 2
5 3
Sample Output 2
7
Since Takahashi scored 5 points, we see that he solved the 1- and 4-point problems but not the 2-point one.
Similarly, since Aoki scored 3 points, we see that he solved the 1- and 2-point problems but not the 4-point one.
Therefore, each of the three problems is solved by at least one of Takahashi and Aoki, so we see that Snuke solved all of the problems, for a score of 7 points. Thus, 7 should be printed.
Sample Input 3
0 0
Sample Output 3
0
Both Takahashi and Aoki solved none of the problems. Therefore, so did Snuke. Thus, 0 should be printed.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 人の人がいます。N 人の人には人 1, 人 2,\dots, 人 N と番号がついています。
人 i(2 \le i \le N) の親は人 P_i です。ここで、P_i < i が保証されます。
人 1 が人 N の何代前か求めてください。
制約
- 2 \le N \le 50
- 1 \le P_i < i(2 \le i \le N)
- 入力は全て整数。
入力
入力は以下の形式で標準入力から与えられる。
N P_2 P_3 \dots P_N
出力
答えを整数として出力せよ。
入力例 1
3 1 2
出力例 1
2
人 2 は人 3 の親であるため、人 3 の 1 代前です。
人 1 は人 2 の親であるため、人 3 の 2 代前です。
よって解は 2 です。
入力例 2
10 1 2 3 4 5 6 7 8 9
出力例 2
9
Score : 200 points
Problem Statement
There are N people, called Person 1, Person 2, \ldots, Person N.
The parent of Person i (2 \le i \le N) is Person P_i. Here, it is guaranteed that P_i < i.
How many generations away from Person N is Person 1?
Constraints
- 2 \le N \le 50
- 1 \le P_i < i(2 \le i \le N)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N P_2 P_3 \dots P_N
Output
Print the answer as a positive integer.
Sample Input 1
3 1 2
Sample Output 1
2
Person 2 is a parent of Person 3, and thus is one generation away from Person 3.
Person 1 is a parent of Person 2, and thus is two generations away from Person 3.
Therefore, the answer is 2.
Sample Input 2
10 1 2 3 4 5 6 7 8 9
Sample Output 2
9
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
おもり 1, おもり 2, \dots, おもり N の N 個のおもりがあります。おもり i の重さは A_i です。
以下の条件を満たす正整数 n を 良い整数 と呼びます。
- \bf{3} 個以下 の異なるおもりを自由に選んで、選んだおもりの重さの和を n にすることができる。
W 以下の正整数のうち、良い整数は何個ありますか?
制約
- 1 \leq N \leq 300
- 1 \leq W \leq 10^6
- 1 \leq A_i \leq 10^6
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N W A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
2 10 1 3
出力例 1
3
おもり 1 のみを選ぶと重さの和は 1 になります。よって 1 は良い整数です。
おもり 2 のみを選ぶと重さの和は 3 になります。よって 3 は良い整数です。
おもり 1 とおもり 2 を選ぶと重さの和は 4 になります。よって 4 は良い整数です。
これら以外に良い整数は存在しません。また、1,3,4 のいずれも W 以下の整数です。よって答えは 3 個になります。
入力例 2
2 1 2 3
出力例 2
0
W 以下の良い整数は存在しません。
入力例 3
4 12 3 3 3 3
出力例 3
3
良い整数は 3,6,9 の 3 個です。
たとえばおもり 1, おもり 2, おもり 3 を選ぶと重さの和は 9 になるので、9 は良い整数です。
12 は良い整数 ではない ことに注意してください。
入力例 4
7 251 202 20 5 1 4 2 100
出力例 4
48
Score : 200 points
Problem Statement
There are N weights called Weight 1, Weight 2, \dots, Weight N. Weight i has a mass of A_i.
Let us say a positive integer n is a good integer if the following condition is satisfied:
- We can choose at most 3 different weights so that they have a total mass of n.
How many positive integers less than or equal to W are good integers?
Constraints
- 1 \leq N \leq 300
- 1 \leq W \leq 10^6
- 1 \leq A_i \leq 10^6
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N W A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
2 10 1 3
Sample Output 1
3
If we choose only Weight 1, it has a total mass of 1, so 1 is a good integer.
If we choose only Weight 2, it has a total mass of 3, so 3 is a good integer.
If we choose Weights 1 and 2, they have a total mass of 4, so 4 is a good integer.
No other integer is a good integer. Also, all of 1, 3, and 4 are integers less than or equal to W. Therefore, the answer is 3.
Sample Input 2
2 1 2 3
Sample Output 2
0
There are no good integers less than or equal to W.
Sample Input 3
4 12 3 3 3 3
Sample Output 3
3
There are 3 good integers: 3, 6, and 9.
For example, if we choose Weights 1, 2, and 3, they have a total mass of 9, so 9 is a good integer.
Note that 12 is not a good integer.
Sample Input 4
7 251 202 20 5 1 4 2 100
Sample Output 4
48
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は部屋に PC を沢山置こうとしています。そこで最大何台の PC を部屋に置けるか調べるプログラムを書くことにしました。
H 個の長さ W の ., T からなる文字列 S_1,S_2,\ldots,S_H が与えられます。
高橋君は以下の操作を 0 回以上何回でも行うことができます。
- 1\leq i \leq H, 1 \leq j \leq W-1 を満たす整数であって、 S_i の j 番目の文字も j+1 番目の文字も
Tであるようなものを選ぶ。 S_i の j 番目の文字をPで置き換え、S_i の j+1 番目の文字をCで置き換える。
高橋君が操作回数の最大化を目指すとき、操作終了後の S_1,S_2,\ldots,S_H としてあり得るものの一例を出力してください。
制約
- 1\leq H \leq 100
- 2\leq W \leq 100
- H と W は整数である
- S_i は
.,Tからなる長さ W の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
高橋君が操作回数の最大化を目指すとき、操作終了後の S_1,S_2,\ldots,S_H としてあり得るものの一例を改行区切りで出力せよ。
解が複数存在する場合、どれを出力しても正答とみなされる。
入力例 1
2 3 TTT T.T
出力例 1
PCT T.T
可能な操作回数の最大値は 1 です。
例えば、 (i,j)=(1,1) として操作を行うと、S_1 が PCT に変化します。
入力例 2
3 5 TTT.. .TTT. TTTTT
出力例 2
PCT.. .PCT. PCTPC
Score : 300 points
Problem Statement
Planning to place many PCs in his room, Takahashi has decided to write a code that finds how many PCs he can place in his room.
You are given H strings S_1,S_2,\ldots,S_H, each of length W, consisting of . and T.
Takahashi may perform the following operation any number of times (possibly zero):
- Choose integers satisfying 1\leq i \leq H and 1 \leq j \leq W-1 such that the j-th and (j+1)-th characters of S_i are both
T. Replace the j-th character of S_i withP, and (j+1)-th withC.
He tries to maximize the number of times he performs the operation. Find possible resulting S_1,S_2,\ldots,S_H.
Constraints
- 1\leq H \leq 100
- 2\leq W \leq 100
- H and W are integers.
- S_i is a string of length W consisting of
.andT.
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
Print a sequence of strings, S_1,S_2,\ldots,S_H, separated by newlines, possibly resulting from maximizing the number of times he performs the operation.
If multiple solutions exist, print any of them.
Sample Input 1
2 3 TTT T.T
Sample Output 1
PCT T.T
He can perform the operation at most once.
For example, an operation with (i,j)=(1,1) makes S_1 PCT.
Sample Input 2
3 5 TTT.. .TTT. TTTTT
Sample Output 2
PCT.. .PCT. PCTPC
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
それぞれ N 個、M 個の正整数からなる 2 つの数列 A=(A_1,A_2, \ldots ,A_N) と B=(B_1, \ldots ,B_M) が与えられます。
それぞれの数列から 1 つずつ要素を選んだときの 2 つの値の差の最小値、すなわち、 \displaystyle \min_{ 1\leq i\leq N}\displaystyle\min_{1\leq j\leq M} \lvert A_i-B_j\rvert を求めてください。
制約
- 1 \leq N,M \leq 2\times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_M
出力
答えを出力せよ。
入力例 1
2 2 1 6 4 9
出力例 1
2
それぞれの数列から 1 つずつ要素を選んだときの 2 つの値の差としてあり得るのは、 \lvert 1-4\rvert=3 、 \lvert 1-9\rvert=8 、 \lvert 6-4\rvert=2 、 \lvert 6-9\rvert=3 の 4 つです。 この中で最小である 2 を出力します。
入力例 2
1 1 10 10
出力例 2
0
入力例 3
6 8 82 76 82 82 71 70 17 39 67 2 45 35 22 24
出力例 3
3
Score : 300 points
Problem Statement
You are given two sequences: A=(A_1,A_2, \ldots ,A_N) consisting of N positive integers, and B=(B_1, \ldots ,B_M) consisting of M positive integers.
Find the minimum difference of an element of A and an element of B, that is, \displaystyle \min_{ 1\leq i\leq N}\displaystyle\min_{1\leq j\leq M} \lvert A_i-B_j\rvert.
Constraints
- 1 \leq N,M \leq 2\times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_M
Output
Print the answer.
Sample Input 1
2 2 1 6 4 9
Sample Output 1
2
Here is the difference for each of the four pair of an element of A and an element of B: \lvert 1-4\rvert=3, \lvert 1-9\rvert=8, \lvert 6-4\rvert=2, and \lvert 6-9\rvert=3. We should print the minimum of these values, or 2.
Sample Input 2
1 1 10 10
Sample Output 2
0
Sample Input 3
6 8 82 76 82 82 71 70 17 39 67 2 45 35 22 24
Sample Output 3
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
1 以上 N 以下の整数であって、A の倍数でも B の倍数でもないものの総和を求めてください。
制約
- 1 \leq N, A,B \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A B
出力
答えを出力せよ。
入力例 1
10 3 5
出力例 1
22
1 以上 10 以下の整数で 3 の倍数でも 5 の倍数でもないのは 1,2,4,7,8 です。それらの総和は 1+2+4+7+8 =22 です。
入力例 2
1000000000 314 159
出力例 2
495273003954006262
Score : 400 points
Problem Statement
Find the sum of integers between 1 and N (inclusive) that are not multiples of A or B.
Constraints
- 1 \leq N, A,B \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the answer.
Sample Input 1
10 3 5
Sample Output 1
22
The integers between 1 and 10 (inclusive) that are not multiples of 3 or 5 are 1,2,4,7, and 8, whose sum is 1+2+4+7+8 =22.
Sample Input 2
1000000000 314 159
Sample Output 2
495273003954006262
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
AtCoder社の社員である青木さんの今月の給料は、整数 N と長さ N の数列 A を用いて以下のように決められます。
まず、青木さんに 1 から N までの整数が等確率で出る N 面ダイスと変数 x=0 を渡します。
その後、以下の手順を終了まで繰り返します。
- ダイスを 1 度振り、出た目を y とする。
- もし x<y なら A_y 円支給し、 x=y と更新する。
- そうでないなら終了する。
青木さんの今月の給料は、この手順によって支給された金額の合計です。
青木さんの今月の給料の期待値を {}\bmod{998244353} で求めてください。
期待値 {}\bmod{998244353} の定義
この問題で求める期待値は必ず有理数になることが証明できます。 また、この問題の制約下では、求める期待値を既約分数 \frac yx で表したときに x が 998244353 で割り切れないことが保証されます。 このとき、y\equiv xz\pmod{998244353} を満たす 0\leq z\lt998244353 がただ一つ存在するので、z を出力してください。制約
- 入力は全て整数
- 1 \le N \le 3 \times 10^5
- 0 \le A_i < 998244353
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
3 3 2 6
出力例 1
776412280
手順の一例は、以下の通りです。
- 最初、 x=0 である。
- ダイスを 1 度振り、出た目が 1 であった。 0<1 であるため、 A_1 = 3 円支給し、 x=1 とする。
- ダイスを 1 度振り、出た目が 3 であった。 1<3 であるため、 A_3 = 6 円支給し、 x=3 とする。
- ダイスを 1 度振り、出た目が 1 であった。 3 \ge 1 であるため、終了する。
この例では、青木さんの今月の給料は 9 円です。
なお、青木さんの今月の給料の期待値は \frac{49}{9} 円と求めることができ、これを {}\bmod{998244353} 上で表現すると 776412280 となります。
入力例 2
1 998244352
出力例 2
998244352
入力例 3
9 3 14 159 2653 58979 323846 2643383 27950288 419716939
出力例 3
545252774
Score : 450 points
Problem Statement
Aoki, an employee at AtCoder Inc., has his salary for this month determined by an integer N and a sequence A of length N as follows.
First, he is given an N-sided die (dice) that shows the integers from 1 to N with equal probability, and a variable x=0.
Then, the following steps are repeated until terminated.
- Roll the die once and let y be the result.
- If x<y, pay him A_y yen and let x=y.
- Otherwise, terminate the process.
Aoki's salary for this month is the total amount paid through this process.
Find the expected value of Aoki's salary this month, modulo 998244353.
How to find an expected value modulo 998244353
It can be proved that the sought expected value in this problem is always a rational number. Also, the constraints of this problem guarantee that if the sought expected value is expressed as a reduced fraction \frac yx, then x is not divisible by 998244353. Here, there is exactly one 0\leq z\lt998244353 such that y\equiv xz\pmod{998244353}. Print this z.Constraints
- All inputs are integers.
- 1 \le N \le 3 \times 10^5
- 0 \le A_i < 998244353
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
3 3 2 6
Sample Output 1
776412280
Here is an example of how the process goes.
- Initially, x=0.
- Roll the die once, and it shows 1. Since 0<1, pay him A_1 = 3 yen and let x=1.
- Roll the die once, and it shows 3. Since 1<3, pay him A_3 = 6 yen and let x=3.
- Roll the die once, and it shows 1. Since 3 \ge 1, terminate the process.
In this case, his salary for this month is 9 yen.
It can be calculated that the expected value of his salary this month is \frac{49}{9} yen, whose representation modulo 998244353 is 776412280.
Sample Input 2
1 998244352
Sample Output 2
998244352
Sample Input 3
9 3 14 159 2653 58979 323846 2643383 27950288 419716939
Sample Output 3
545252774
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
(1,2, \dots, N) を並び替えた長さ N の順列 P = (p_1, p_2, \dots, p_N) に対して、 P のスコア S(P) を次のように定めます。
- N 人の人とすぬけ君がいて、N 人の人には 1,2,\dots,N の番号がついています。はじめ、人 i (1 \leq i \leq N) はボール i を持っています。
すぬけ君が叫ぶたびに、i \neq p_i であるようなすべての人 i は人 p_i に持っているボールを同時に渡します。
すぬけ君は、1 回以上叫んだ後にすべての人 i がボール i を持っている状態になると叫ぶのをやめます。
すぬけ君が叫ぶのをやめるまでに叫んだ回数が順列のスコアとなります。ここでスコアは有限の値を取ることが保証されます。
P としてあり得るものは N! 通りありますが、それらのスコアを K 乗した値の総和を 998244353 で割ったあまりを計算してください。
-
厳密に言い換えると、(1,2, \dots, N) を並び替えた長さ N の順列全体の集合を S_N として
\displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}
を計算してください。
制約
- 2 \leq N \leq 50
- 1 \leq K \leq 10^4
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N K
出力
\displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353} を出力せよ。
入力例 1
2 2
出力例 1
5
N = 2 のとき P としてあり得る順列は (1,2),(2,1) の 2 つです。
順列 (1,2) のスコアは次のように決まります。
- はじめ人 1 はボール 1 を、人 2 はボール 2 を持っています。
すぬけ君が 1 回目に叫んだ後に、人 1 はボール 1 を、人 2 はボール 2 を持っています。
このとき、すべての人が自身の番号と同じ番号が書かれたボールを持っているので、すぬけ君は叫ぶのをやめます。
よってスコアは 1 となります。
順列 (2,1) のスコアは次のように決まります。
- はじめ人 1 はボール 1 を、人 2 はボール 2 を持っています。
すぬけ君が 1 回目に叫んだ後に、人 1 はボール 2 を、人 2 はボール 1 を持っています。
すぬけ君が 2 回目に叫んだ後に、人 1 はボール 1 を、人 2 はボール 2 を持っています。
このとき、すべての人が自身の番号と同じ番号が書かれたボールを持っているので、すぬけ君は叫ぶのをやめます。
よってスコアは 2 となります。
よって 1^2 + 2^2 = 5 がこの問題の答えになります。
入力例 2
3 3
出力例 2
79
すべての順列とスコアの組を列挙すると以下のようになります。
- 順列 : (1,2,3), スコア : 1
- 順列 : (1,3,2), スコア : 2
- 順列 : (2,1,3), スコア : 2
- 順列 : (2,3,1), スコア : 3
- 順列 : (3,1,2), スコア : 3
- 順列 : (3,2,1), スコア : 2
よって 1^3 + 2^3 + 2^3 + 3^3 + 3^3 + 2^3 = 79 を出力します。
入力例 3
50 10000
出力例 3
77436607
Score : 500 points
Problem Statement
For a permutation P = (p_1, p_2, \dots, p_N) of (1,2, \dots, N), let us define the score S(P) of P as follows.
- There are N people, numbered 1,2,\dots,N. Additionally, Snuke is there. Initially, Person i (1 \leq i \leq N) has Ball i.
Each time Snuke screams, every Person i such that i \neq p_i gives their ball to Person p_i simultaneously.
If, after screaming at least once, every Person i has Ball i, Snuke stops screaming.
The score is the number of times Snuke screams until he stops. Here, it is guaranteed that the score will be a finite value.
There are N! permutations P of (1,2, \dots, N). Find the sum, modulo 998244353, of the scores of those permutations, each raised to the K-th power.
- Formally, let S_N be the set of the permutations of (1,2, \dots, N). Compute the following: \displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}.
Constraints
- 2 \leq N \leq 50
- 1 \leq K \leq 10^4
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print \displaystyle \left(\sum_{P \in S_N} S(P)^K \right) \bmod {998244353}.
Sample Input 1
2 2
Sample Output 1
5
When N = 2, there are two possible permutations P: (1,2),(2,1).
The score of the permutation (1,2) is found as follows.
- Initially, Person 1 has Ball 1, and Person 2 has Ball 2.
After Snuke's first scream, Person 1 has Ball 1, and Person 2 has Ball 2.
Here, every Person i has Ball i, so he stops screaming.
Thus, the score is 1.
The score of the permutation (2,1) is found as follows.
- Initially, Person 1 has Ball 1, and Person 2 has Ball 2.
After Snuke's first scream, Person 1 has Ball 2, and Person 2 has Ball 1.
After Snuke's second scream, Person 1 has Ball 1, and Person 2 has Ball 2.
Here, every Person i has Ball i, so he stops screaming.
Thus, the score is 2.
Therefore, the answer in this case is 1^2 + 2^2 = 5.
Sample Input 2
3 3
Sample Output 2
79
All permutations and their scores are listed below.
- (1,2,3): The score is 1.
- (1,3,2): The score is 2.
- (2,1,3): The score is 2.
- (2,3,1): The score is 3.
- (3,1,2): The score is 3.
- (3,2,1): The score is 2.
Thus, we should print 1^3 + 2^3 + 2^3 + 3^3 + 3^3 + 2^3 = 79.
Sample Input 3
50 10000
Sample Output 3
77436607