実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
正整数 N が与えられます。
N ページからなる資料をコピー用紙に印刷します。
印刷には両面印刷を使い、コピー用紙 1 枚の表と裏にそれぞれ 1 ページずつ印刷することができます。
N ページを全て印刷するために必要なコピー用紙の枚数の最小値を求めてください。
制約
- 1\le N\le 100
- 入力される値は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
3
出力例 1
2
1 枚のコピー用紙の両面に、もう 1 枚のコピー用紙の片面に印刷することで 2 枚のコピー用紙で 3 ページ印刷することができます。
入力例 2
6
出力例 2
3
入力例 3
59
出力例 3
30
Score : 100 points
Problem Statement
You are given a positive integer N.
You will print a document consisting of N pages on sheets of copy paper.
You will use double-sided printing, so one page can be printed on each of the front and back of a sheet.
Find the minimum number of sheets of copy paper needed to print all N pages.
Constraints
- 1\le N\le 100
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
3
Sample Output 1
2
By printing on both sides of one sheet of copy paper and on one side of another, you can print 3 pages using 2 sheets.
Sample Input 2
6
Sample Output 2
3
Sample Input 3
59
Sample Output 3
30
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
1 から N までの番号が書かれた(表裏の区別できる)N 枚のカードがあり、 最初、上から 1,2,\ldots,N の順ですべて表向きで積まれています。
高橋君はこの山に Q 回の操作を行います。i 回目(1\leq i\leq Q)の操作では以下の操作を行います。
上から K_i 枚のカードをひとまとめにして裏返し、元の位置に戻す。
これにより、上から K_i 枚のカードの順序が上下逆になり、それぞれのカードの表裏も反転する。
すべての操作を終えた後のカードの順序と表裏を求めてください。
具体的には、i=1,2,\ldots,N について、上から i 枚目に置かれているカードに書かれている番号が X_i のとき、そのカードが表向きで積まれている時 X_i を、裏向きなら -X_i を出力してください。
制約
- 1 \leq N,Q \leq 100
- 1 \leq K_i \leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q K_1 K_2 \cdots K_Q
出力
N 行出力せよ。i 行目(1\leq i\leq N)には、Q 回の操作の後で上から i 枚目に置かれているカードに書かれている番号が X_i のとき、そのカードが表向きで積まれている時 X_i を、裏向きなら -X_i を出力せよ。
入力例 1
5 2 3 5
出力例 1
-5 -4 1 2 3
以下では番号 i が書かれたカードが表向きで置かれている状態を i 表、裏向きで置かれている状態を i 裏、で表します。
最初、積まれているカードの状態は上から順に(1 表、2 表、3 表、4 表、5 表)です。
1 回目の操作で上から 3 枚のカードをひとまとめにして裏返すと、状態は上から順に(3 裏、2 裏、1 裏、4 表、5 表)となります。
次に、2 回目の操作で上から 5 枚のカードをすべてひとまとめにして裏返すと、状態は上から順に(5 裏、4 裏、1 表、2 表、3 表)となります。
よって、-5,-4,1,2,3 を各行にこの順に出力します。
入力例 2
1 1 1
出力例 2
-1
1 回の操作の後で、1 の書かれたカードが裏向きで置かれています。 よって、-1 を出力します。
Score : 200 points
Problem Statement
There are N cards numbered 1 to N, each with a distinguishable front and back. Initially, they are stacked in the order 1,2,\ldots,N from the top, all face up.
Takahashi performs Q operations on this stack. The i-th operation (1\leq i\leq Q) is as follows.
Flip the top K_i cards over together as a single block and put them back in place.
This reverses the top-to-bottom order of the top K_i cards and also turns each of those cards over.
Find the order and orientation of the cards after all operations.
Specifically, for each i=1,2,\ldots,N, let X_i be the number written on the i-th card from the top; print X_i if that card is face up, and -X_i if it is face down.
Constraints
- 1 \leq N,Q \leq 100
- 1 \leq K_i \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q K_1 K_2 \cdots K_Q
Output
Print N lines. For the i-th line (1\leq i\leq N), let X_i be the number written on the i-th card from the top after the Q operations; print X_i if that card is face up, and -X_i if it is face down.
Sample Input 1
5 2 3 5
Sample Output 1
-5 -4 1 2 3
Below, we write "i up" for the card numbered i placed face up, and "i down" for the card numbered i placed face down.
Initially, the states of the stacked cards, from the top, are (1 up, 2 up, 3 up, 4 up, 5 up).
The 1-st operation flips the top 3 cards together, so the states become (3 down, 2 down, 1 down, 4 up, 5 up) from the top.
Next, the 2-nd operation flips all of the top 5 cards together, so the states become (5 down, 4 down, 1 up, 2 up, 3 up) from the top.
Thus, print -5,-4,1,2,3 in this order, one per line.
Sample Input 2
1 1 1
Sample Output 2
-1
After the single operation, the card numbered 1 is face down. Thus, print -1.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
AtCoder 村で村長を決める投票を行うことになりました。
候補者は N 人おり、候補者 1, 候補者 2, \ldots, 候補者 N と番号づけられています。
投票者は M 人おり、i 人目 (1\leq i\leq M) の投票者の投票先の第一希望は候補者 A_i、第二希望は候補者 B_i です。
候補者のうち一人のみが辞退した時、それぞれの投票者は以下のように投票します。
- 第一希望の候補者が辞退していなければ、第一希望の候補者に投票する。
- 第一希望の候補者が辞退していれば、第二希望の候補者に投票する。
辞退していない候補者のうち、得票数が最大の候補者が当選します。そのような候補者が複数いる場合は、番号が最小の候補者が当選します。
k=1,2,\ldots,N それぞれについて、候補者 k のみが辞退した場合に当選する候補者の番号を求めてください。
制約
- 2 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1 \leq A_i,B_i \leq N
- A_i\neq B_i
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
出力
N 行出力せよ。i 行目には、候補者 i のみが辞退した場合に当選する候補者の番号を出力せよ。
入力例 1
5 6 1 3 1 3 2 1 2 1 3 4 4 2
出力例 1
3 1 1 2 1
候補者 1 のみが辞退した場合、それぞれの投票者の投票先は次のようになります。
- 投票者 1 の第一希望の候補者は辞退しているため、第二希望の候補者 3 に投票する。
- 投票者 2 の第一希望の候補者は辞退しているため、第二希望の候補者 3 に投票する。
- 投票者 3 の第一希望の候補者は辞退していないため、第一希望の候補者 2 に投票する。
- 投票者 4 の第一希望の候補者は辞退していないため、第一希望の候補者 2 に投票する。
- 投票者 5 の第一希望の候補者は辞退していないため、第一希望の候補者 3 に投票する。
- 投票者 6 の第一希望の候補者は辞退していないため、第一希望の候補者 4 に投票する。
よって得票数は、候補者 2 が 2 票、候補者 3 が 3 票、候補者 4 が 1 票となり、このとき得票数が最大の候補者 3 が当選します。
他の候補者が辞退した時はそれぞれ以下のようになります。
- 候補者 2 のみが辞退した時、候補者 1 が 4 票、候補者 3 が 1 票、候補者 4 が 1 票となり、得票数が最大の候補者 1 が当選します。
- 候補者 3 のみが辞退した時、候補者 1 が 2 票、候補者 2 が 2 票、候補者 4 が 2 票となり、得票数が最大の候補者のうち番号が最小の候補者 1 が当選します。
- 候補者 4 のみが辞退した時、候補者 1 が 2 票、候補者 2 が 3 票、候補者 3 が 1 票となり、得票数が最大の候補者 2 が当選します。
- 候補者 5 のみが辞退した時、候補者 1 が 2 票、候補者 2 が 2 票、候補者 3 が 1 票、候補者 4 が 1 票となり、得票数が最大の候補者のうち番号が最小の候補者 1 が当選します。
よって、3,1,1,2,1 をこの順に各行に出力します。
Score : 300 points
Problem Statement
AtCoder Village is holding an election to choose its mayor.
There are N candidates, numbered candidate 1, candidate 2, \ldots, candidate N.
There are M voters. The i-th voter (1\leq i\leq M) has candidate A_i as their first choice and candidate B_i as their second choice.
When exactly one of the candidates withdraws, each voter votes as follows.
- If the voter's first-choice candidate has not withdrawn, the voter votes for their first-choice candidate.
- If the voter's first-choice candidate has withdrawn, the voter votes for their second-choice candidate.
Among the candidates who have not withdrawn, the candidate with the most votes wins. If there are multiple such candidates, the one with the smallest number wins.
For each k=1,2,\ldots,N, find the number of the candidate who wins when only candidate k withdraws.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1 \leq M \leq 2\times 10^5
- 1 \leq A_i,B_i \leq N
- A_i\neq B_i
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 A_2 B_2 \vdots A_M B_M
Output
Print N lines. The i-th line should contain the number of the candidate who wins when only candidate i withdraws.
Sample Input 1
5 6 1 3 1 3 2 1 2 1 3 4 4 2
Sample Output 1
3 1 1 2 1
When only candidate 1 withdraws, the voters vote as follows.
- Voter 1's first-choice candidate has withdrawn, so they vote for their second-choice candidate, candidate 3.
- Voter 2's first-choice candidate has withdrawn, so they vote for their second-choice candidate, candidate 3.
- Voter 3's first-choice candidate has not withdrawn, so they vote for their first-choice candidate, candidate 2.
- Voter 4's first-choice candidate has not withdrawn, so they vote for their first-choice candidate, candidate 2.
- Voter 5's first-choice candidate has not withdrawn, so they vote for their first-choice candidate, candidate 3.
- Voter 6's first-choice candidate has not withdrawn, so they vote for their first-choice candidate, candidate 4.
Thus, candidate 2 receives 2 votes, candidate 3 receives 3 votes, and candidate 4 receives 1 vote, so candidate 3, who has the most votes, wins.
When each of the other candidates withdraws instead, the results are as follows.
- When only candidate 2 withdraws, candidate 1 receives 4 votes, candidate 3 receives 1 vote, and candidate 4 receives 1 vote, so candidate 1, who has the most votes, wins.
- When only candidate 3 withdraws, candidate 1 receives 2 votes, candidate 2 receives 2 votes, and candidate 4 receives 2 votes, so candidate 1, who has the smallest number among those with the most votes, wins.
- When only candidate 4 withdraws, candidate 1 receives 2 votes, candidate 2 receives 3 votes, and candidate 3 receives 1 vote, so candidate 2, who has the most votes, wins.
- When only candidate 5 withdraws, candidate 1 receives 2 votes, candidate 2 receives 2 votes, candidate 3 receives 1 vote, and candidate 4 receives 1 vote, so candidate 1, who has the smallest number among those with the most votes, wins.
Thus, print 3,1,1,2,1 in this order, one per line.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
数直線上にロボットがいます。はじめ、ロボットは座標 0 にいます。 また、高橋君は長さ N の整数列 A=(A_1,A_2,\ldots,A_N) を持っています。
高橋君は次の操作を行います。
-
A の要素を 高々 1 つ 選び、その符号を反転させる。すなわち、1\leq i\leq N をみたす i を 0 または 1 つ選び、A_i を -A_i に書き換える。
-
ロボットを起動する。ロボットは起動後、N 回の行動を行う。i 回目 (1\leq i\leq N) の行動では、現在の座標を x として、座標 x+A_i へ直線的に移動する。
高橋君はロボットが一度以上通る整数座標の個数をなるべく多くしたいです。
高橋君が最適に行動したときの、「ロボットが一度以上通る整数座標の個数」としてあり得る最大の値を求めてください。
なお、ロボットは連続的に移動するものとし、ある移動で座標 S から T まで移動したとき、S\leq T ならば S 以上 T 以下の任意の整数座標、S>T ならば T 以上 S 以下の任意の整数座標を通ったとみなします。
制約
- 1 \leq N \leq 2\times 10^5
- -10^9 \leq A_i \leq 10^9
- A_i\neq 0
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \cdots A_N
出力
高橋君が最適に行動したとき、ロボットが一度以上通る整数座標の個数としてあり得る最大の値を出力せよ。
入力例 1
3 -2 3 -2
出力例 1
8
最初、A=(-2,3,-2) です。高橋君が A_2 の符号を反転させると、A=(-2,-3,-2) となります。 このとき、ロボットは以下のように動きます。
- ロボットは 1 回目の移動で、0 から -2 へ動きます。この移動でロボットが通る整数座標は -2,-1,0 です。
- ロボットは 2 回目の移動で、-2 から -5 へ動きます。この移動でロボットが通る整数座標は -5,-4,-3,-2 です。
- ロボットは 3 回目の移動で、-5 から -7 へ動きます。この移動でロボットが通る整数座標は -7,-6,-5 です。
よって、ロボットが一度以上通る整数座標は -7,-6,-5,-4,-3,-2,-1,0 の 8 つとなり、このとき個数が最大となります。よって、8 を出力します。
なお、高橋君がどの要素の符号も反転させなかったとすると、ロボットは 0\to -2\to 1\to -1 と動き、一度以上通る整数座標は -2,-1,0,1 の 4 つとなります。
入力例 2
2 2 3
出力例 2
6
A の要素を書き換えないとき、ロボットは 0\to 2\to 5 と動き、一度以上通る整数座標の個数は 0,1,2,3,4,5 の 6 個です。
このときが最大であるため、 6 を出力します。
Score : 400 points
Problem Statement
There is a robot on a number line. Initially, the robot is at coordinate 0. Also, Takahashi has an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.
Takahashi performs the following steps in order.
-
Choose at most 1 element of A and flip its sign. That is, choose 0 or 1 index i satisfying 1\leq i\leq N, and replace A_i with -A_i.
-
Start the robot. After starting, the robot makes N moves. In the i-th move (1\leq i\leq N), letting x be its current coordinate, the robot moves in a straight line to coordinate x+A_i.
Takahashi wants to maximize the number of integer coordinates that the robot passes through at least once.
Find the maximum possible value of "the number of integer coordinates that the robot passes through at least once" when Takahashi acts optimally.
Here, the robot moves continuously. When a move takes the robot from coordinate S to coordinate T, it is considered to have passed through every integer coordinate between S and T, inclusive.
Constraints
- 1 \leq N \leq 2\times 10^5
- -10^9 \leq A_i \leq 10^9
- A_i\neq 0
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \cdots A_N
Output
Print the maximum possible value of the number of integer coordinates that the robot passes through at least once when Takahashi acts optimally.
Sample Input 1
3 -2 3 -2
Sample Output 1
8
Initially, A=(-2,3,-2). If Takahashi flips the sign of A_2, then A=(-2,-3,-2). In this case, the robot moves as follows.
- In its 1-st move, the robot moves from 0 to -2. The integer coordinates it passes through in this move are -2,-1,0.
- In its 2-nd move, the robot moves from -2 to -5. The integer coordinates it passes through in this move are -5,-4,-3,-2.
- In its 3-rd move, the robot moves from -5 to -7. The integer coordinates it passes through in this move are -7,-6,-5.
Thus, the integer coordinates the robot passes through at least once are the 8 coordinates -7,-6,-5,-4,-3,-2,-1,0, and this is the maximum possible count, so print 8.
Note that if Takahashi did not flip the sign of any element, the robot would move as 0\to -2\to 1\to -1, and the integer coordinates it passes through at least once would be the 4 coordinates -2,-1,0,1.
Sample Input 2
2 2 3
Sample Output 2
6
If no element of A is changed, the robot moves as 0\to 2\to 5, and the integer coordinates it passes through at least once are the 6 coordinates 0,1,2,3,4,5.
This is the maximum possible count, so print 6.
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
英小文字のみからなる長さ M の文字列が N 個あります。 i 個目 (1\leq i\leq N) の文字列は S_i です。
以下をみたす整数の組 (i,j) (1\leq i < j \leq N) の個数を求めてください。
- S_i と S_j のハミング距離が 2 以下である。
ハミング距離とは
長さがともに k である文字列 A,B のハミング距離とは、1\leq i\leq k をみたす整数のうち、A の i 文字目と B の i 文字目が異なるようなものの個数を表します。
たとえば、abc と bac のハミング距離は 2 であり、atcoder と contest のハミング距離は 7 です。
制約
- 2 \leq N \leq 2\times 10^5
- 2 \leq M \leq 6
- N,M は整数
- S_i は英小文字のみからなる長さ M の文字列
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 S_2 \vdots S_N
出力
条件をみたす整数の組 (i,j) (1\leq i < j \leq N) の個数を出力せよ。
入力例 1
3 3 abc abd bac
出力例 1
2
- S_1=
abcと S_2=abdのハミング距離は 1 です。 - S_1=
abcと S_3=bacのハミング距離は 2 です。 - S_2=
abdと S_3=bacのハミング距離は 3 です。
よって、条件をみたす組は (i,j)=(1,2), (1,3) の 2 つであるため、2 を出力します。
入力例 2
9 5 aaaaa aaaaa aaaab aaaba aaabb aabaa aabab aabba aabbb
出力例 2
31
同じ文字列が複数回登場することもあります。
Score : 450 points
Problem Statement
There are N strings of length M consisting of lowercase English letters. The i-th string (1\leq i\leq N) is S_i.
Find the number of integer pairs (i,j) (1\leq i < j \leq N) satisfying the following condition.
- The Hamming distance between S_i and S_j is at most 2.
What is Hamming distance?
The Hamming distance between strings A and B, both of length k, is the number of integers i satisfying 1\leq i\leq k such that the i-th character of A and the i-th character of B are different.
For example, the Hamming distance between abc and bac is 2, and the Hamming distance between atcoder and contest is 7.
Constraints
- 2 \leq N \leq 2\times 10^5
- 2 \leq M \leq 6
- N and M are integers.
- S_i is a string of length M consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N M S_1 S_2 \vdots S_N
Output
Print the number of integer pairs (i,j) (1\leq i < j \leq N) satisfying the condition.
Sample Input 1
3 3 abc abd bac
Sample Output 1
2
- The Hamming distance between S_1=
abcand S_2=abdis 1. - The Hamming distance between S_1=
abcand S_3=bacis 2. - The Hamming distance between S_2=
abdand S_3=bacis 3.
Thus, the pairs satisfying the condition are the 2 pairs (i,j)=(1,2), (1,3), so print 2.
Sample Input 2
9 5 aaaaa aaaaa aaaab aaaba aaabb aabaa aabab aabba aabbb
Sample Output 2
31
The same string may appear multiple times.
実行時間制限: 2.5 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
整数 N,Q と長さ N の整数列 A=(A_1,A_2,\ldots,A_N), B=(B_1,B_2,\ldots,B_N) が与えられます。
Q 個のクエリが与えられるのでそれぞれについて答えてください。各クエリでは整数 x が与えられるので、\displaystyle \sum_{i=1}^N ((x+A_i) \bmod B_i) の値を求めてください。
制約
- 1\le N\le 5\times 10^5
- 0\le A_i < B_i \le 5\times 10^5
- 1\le Q\le 2\times 10^5
- 0\le x\le 5\times 10^5
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 B_1
A_2 B_2
\vdots
A_N B_N
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
i 番目のクエリ \text{query}_i は以下の形式で与えられる。
x
出力
Q 行出力せよ。
i 行目 (1\le i\le Q) には、\text{query}_i に対する答えを出力せよ。
入力例 1
3 1 3 2 5 0 1 5 0 1 2 3 200000
出力例 1
3 5 4 1 2
各クエリに対する答えは以下のように求められます。
- 1 番目のクエリでは x=0 なので、((0+1)\bmod 3)+((0+2)\bmod 5)+((0+0)\bmod 1)=1+2+0=3 です。
- 2 番目のクエリでは x=1 なので、((1+1)\bmod 3)+((1+2)\bmod 5)+((1+0)\bmod 1)=2+3+0=5 です。
- 3 番目のクエリでは x=2 なので、((2+1)\bmod 3)+((2+2)\bmod 5)+((2+0)\bmod 1)=0+4+0=4 です。
- 4 番目のクエリでは x=3 なので、((3+1)\bmod 3)+((3+2)\bmod 5)+((3+0)\bmod 1)=1+0+0=1 です。
- 5 番目のクエリでは x=200000 なので、((200000+1)\bmod 3)+((200000+2)\bmod 5)+((200000+0)\bmod 1)=0+2+0=2 です。
入力例 2
3 2 3 5 7 0 9 4 9 3 6 17
出力例 2
2 6 12 10
入力例 3
8 0 10 3 8 1 3 5 7 4 8 4 9 2 6 5 7 7 130140 70510 22719 104735 119072 127841 191631
出力例 3
16 21 31 29 19 21 27
Score : 500 points
Problem Statement
You are given integers N,Q and integer sequences A=(A_1,A_2,\ldots,A_N), B=(B_1,B_2,\ldots,B_N) of length N.
You are given Q queries, so answer each of them. In each query, you are given an integer x; find the value of \displaystyle \sum_{i=1}^N ((x+A_i) \bmod B_i).
Constraints
- 1\le N\le 5\times 10^5
- 0\le A_i < B_i \le 5\times 10^5
- 1\le Q\le 2\times 10^5
- 0\le x\le 5\times 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_1 B_1
A_2 B_2
\vdots
A_N B_N
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
The i-th query \text{query}_i is given in the following format:
x
Output
Print Q lines.
The i-th line (1\le i\le Q) should contain the answer to \text{query}_i.
Sample Input 1
3 1 3 2 5 0 1 5 0 1 2 3 200000
Sample Output 1
3 5 4 1 2
The answer to each query is obtained as follows.
- In the 1-st query, x=0, so the answer is ((0+1)\bmod 3)+((0+2)\bmod 5)+((0+0)\bmod 1)=1+2+0=3.
- In the 2-nd query, x=1, so the answer is ((1+1)\bmod 3)+((1+2)\bmod 5)+((1+0)\bmod 1)=2+3+0=5.
- In the 3-rd query, x=2, so the answer is ((2+1)\bmod 3)+((2+2)\bmod 5)+((2+0)\bmod 1)=0+4+0=4.
- In the 4-th query, x=3, so the answer is ((3+1)\bmod 3)+((3+2)\bmod 5)+((3+0)\bmod 1)=1+0+0=1.
- In the 5-th query, x=200000, so the answer is ((200000+1)\bmod 3)+((200000+2)\bmod 5)+((200000+0)\bmod 1)=0+2+0=2.
Sample Input 2
3 2 3 5 7 0 9 4 9 3 6 17
Sample Output 2
2 6 12 10
Sample Input 3
8 0 10 3 8 1 3 5 7 4 8 4 9 2 6 5 7 7 130140 70510 22719 104735 119072 127841 191631
Sample Output 3
16 21 31 29 19 21 27
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 575 点
問題文
整数 N,K が与えられます。与えられる整数は \displaystyle K \le \frac N2 を満たすことが保証されます。
(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N),Q=(Q_1,Q_2,\ldots,Q_N) に対し、f(P,Q) を以下のように定義します:
人 1 から人 N までの N 人の人がコンテスト A, B の 2 つのコンテストに出場した。人 i (1\le i\le N) はコンテスト A で P_i 位、コンテスト B で Q_i 位であった。
この N 人の中から 2K 人を以下の 2 つの選抜方法で選ぶことを考える。
- ①:コンテスト A で順位の良い K 人を選ぶ。その後、まだ選ばれていない N-K 人のうちコンテスト B で順位の良い K 人を選ぶ。
- ②:コンテスト B で順位の良い K 人を選ぶ。その後、まだ選ばれていない N-K 人のうちコンテスト A で順位の良い K 人を選ぶ。
選抜方法①で選ばれ、選抜方法②で選ばれないような人数を f(P,Q) とする。
(1,2,\ldots,N) の順列の組 (P,Q) は N!^2 通りありますが、それら全てに対する f(P,Q) の総和を 998244353 で割ったあまりを求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T\le 2\times 10^5
- 2\le N\le 10^6
- \displaystyle 1\le K \le \frac N2
- 全てのテストケースにおける N の総和は 10^6 以下
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N K
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
入力例 1
4 3 1 6 3 9 2 483 101
出力例 1
6 0 982675409 845255482
1 番目のテストケースについて考えます。
例えば P=(2,1,3),Q=(3,1,2) のとき、選抜方法①で選ばれる人は人 2,3 の 2 人で、選抜方法②で選ばれる人は人 1,2 の 2 人です。したがって、この P,Q に対し f(P,Q)=1 となります。
f(P,Q)=1 となる (P,Q) の組は 6 通りで、残りの 30 通りの (P,Q) の組に対して f(P,Q)=0 が成り立ちます。したがって、答えは 6\times 1+30\times 0=6 となります。
Score : 575 points
Problem Statement
You are given integers N,K. It is guaranteed that the given integers satisfy \displaystyle K \le \frac N2.
For permutations P=(P_1,P_2,\ldots,P_N),Q=(Q_1,Q_2,\ldots,Q_N) of (1,2,\ldots,N), define f(P,Q) as follows:
N people, person 1 through person N, participated in 2 contests, contest A and contest B. Person i (1\le i\le N) placed P_i-th in contest A and Q_i-th in contest B.
Consider choosing 2K of these N people by the following 2 selection methods.
- Method ①: Choose the K people with the best ranks in contest A. Then, among the N-K people not yet chosen, choose the K people with the best ranks in contest B.
- Method ②: Choose the K people with the best ranks in contest B. Then, among the N-K people not yet chosen, choose the K people with the best ranks in contest A.
Let f(P,Q) be the number of people who are chosen by method ① but not chosen by method ②.
There are N!^2 pairs (P,Q) of permutations of (1,2,\ldots,N). Find the sum of f(P,Q) over all of them, modulo 998244353.
You are given T test cases; solve each of them.
Constraints
- 1\le T\le 2\times 10^5
- 2\le N\le 10^6
- \displaystyle 1\le K \le \frac N2
- The sum of N over all test cases is at most 10^6.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N K
Output
Print the answers to the test cases in order, separated by newlines.
Sample Input 1
4 3 1 6 3 9 2 483 101
Sample Output 1
6 0 982675409 845255482
Consider the 1-st test case.
For example, when P=(2,1,3),Q=(3,1,2), the people chosen by method ① are persons 2 and 3, and the people chosen by method ② are persons 1 and 2. Therefore, f(P,Q)=1 for this pair P,Q.
There are 6 pairs (P,Q) with f(P,Q)=1, and f(P,Q)=0 holds for the remaining 30 pairs (P,Q). Therefore, the answer is 6\times 1+30\times 0=6.