Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋くんは満月が好きです。
今日を 1 日目とすると、今日以降で満月を見られる最初の日は M 日目です。以後は P 日ごと、つまり M+P 日目、M+2P 日目、\ldots に満月を見られます。
1 日目から N 日目まで(両端を含む)の中で、高橋くんが満月を見られる日の数を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq M \leq P \leq 2\times 10^5
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M P
出力
答えを整数として出力せよ。
入力例 1
13 3 5
出力例 1
3
満月を見られる日は、3 日目、8 日目、13 日目、18 日目、\ldots です。
1 日目から 13 日目までの中で高橋くんが満月を見られる日は、3 日目、8 日目、13 日目の 3 個です。
入力例 2
5 6 6
出力例 2
0
高橋くんが満月を見られる日が存在しない場合もあります。
入力例 3
200000 314 318
出力例 3
628
Score : 100 points
Problem Statement
Takahashi likes full moons.
Let today be day 1. The first day on or after today on which he can see a full moon is day M. After that, he can see a full moon every P days, that is, on day M+P, day M+2P, and so on.
Find the number of days between day 1 and day N, inclusive, on which he can see a full moon.
Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq M \leq P \leq 2\times 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M P
Output
Print the answer as an integer.
Sample Input 1
13 3 5
Sample Output 1
3
He can see a full moon on day 3, 8, 13, 18, and so on.
From day 1 to 13, he can see a full moon on three days: day 3, 8, and 13.
Sample Input 2
5 6 6
Sample Output 2
0
There may be no days he can see a full moon.
Sample Input 3
200000 314 318
Sample Output 3
628
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
座標平面上に N 枚の長方形のシートが張られています。
各シートが覆う長方形領域の各辺はそれぞれ x 軸または y 軸と平行であり、
具体的には、i 枚目のシートはちょうど A_i \leq x\leq B_i かつ C_i \leq y\leq D_i をみたす領域全体を覆っています。
1 枚以上のシートによって覆われている領域 の面積を S とすると、
S は制約の条件下で整数となる事が証明できます。
S を整数の形で出力してください。
制約
- 2\leq N\leq 100
- 0\leq A_i<B_i\leq 100
- 0\leq C_i<D_i\leq 100
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 \vdots A_N B_N C_N D_N
出力
1 枚以上のシートによって覆われている領域の面積 S を整数で出力せよ。
入力例 1
3 0 5 1 3 1 4 0 5 2 5 2 4
出力例 1
20
3 枚のシートによって覆われている領域は次のようになります。
ここで、赤色・黄色・青色はそれぞれ 1 枚目・ 2 枚目・ 3 枚目のシートによって覆われている領域を表しています。
よって、1 枚以上のシートによって覆われている領域の面積は S=20 となります。
入力例 2
2 0 100 0 100 0 100 0 100
出力例 2
10000
異なるシートが同じ領域を覆っている事があることに注意してください。
入力例 3
3 0 1 0 1 0 3 0 5 5 10 0 10
出力例 3
65
Score : 200 points
Problem Statement
There are N rectangular sheets spread out on a coordinate plane.
Each side of the rectangular region covered by each sheet is parallel to the x- or y-axis.
Specifically, the i-th sheet covers exactly the region satisfying A_i \leq x\leq B_i and C_i \leq y\leq D_i.
Let S be the area of the region covered by one or more sheets. It can be proved that S is an integer under the constraints.
Print S as an integer.
Constraints
- 2\leq N\leq 100
- 0\leq A_i<B_i\leq 100
- 0\leq C_i<D_i\leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 \vdots A_N B_N C_N D_N
Output
Print the area S of the region covered by one or more sheets as an integer.
Sample Input 1
3 0 5 1 3 1 4 0 5 2 5 2 4
Sample Output 1
20
The three sheets cover the following regions.
Here, red, yellow, and blue represent the regions covered by the first, second, and third sheets, respectively.
Therefore, the area of the region covered by one or more sheets is S=20.
Sample Input 2
2 0 100 0 100 0 100 0 100
Sample Output 2
10000
Note that different sheets may cover the same region.
Sample Input 3
3 0 1 0 1 0 3 0 5 5 10 0 10
Sample Output 3
65
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は N 日間の鉄道旅行を計画しています。
高橋君はそれぞれの日について、運賃の通常料金を払うか、1 日周遊パスを 1 枚使用するか選ぶことができます。
ここで、1\leq i\leq N について、i 日目の旅行にかかる運賃の通常料金は F_i 円です。
一方、1 日周遊パスは D 枚セットで P 円で発売されており、何セットでも購入することが可能ですが、D 枚単位でしか購入することができません。
また、購入したパスは 1 枚ずつ好きな日に使うことができ、旅行が終了した時点で余っていても構いません。
N 日間の旅行でかかる金額、すなわち 1 日周遊パスの購入にかかった代金と、1 日周遊パスを利用しなかった日における運賃の通常料金の合計金額の和としてあり得る最小値を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq D\leq 2\times 10^5
- 1\leq P\leq 10^9
- 1\leq F_i\leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D P F_1 F_2 \ldots F_N
出力
N 日間の旅行でかかる金額としてあり得る最小値を出力せよ。
入力例 1
5 2 10 7 1 6 3 6
出力例 1
20
1 日周遊パスを 1 セットだけ購入し、1 日目と 3 日目に使用すると、合計金額は (10\times 1)+(0+1+0+3+6)=20 となり、このときかかる金額が最小となります。
よって、20 を出力します。
入力例 2
3 1 10 1 2 3
出力例 2
6
3 日間すべてにおいて運賃の通常料金を支払ったときに最小となります。
入力例 3
8 3 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
出力例 3
3000000000
1 日周遊パスを 3 セット購入し、8 日間すべてにおいて 1 日周遊パスを利用したときに最小となります。
答えが 32 bit 整数型に収まらないことがあることに注意してください。
Score : 300 points
Problem Statement
Takahashi is planning an N-day train trip.
For each day, he can pay the regular fare or use a one-day pass.
Here, for 1\leq i\leq N, the regular fare for the i-th day of the trip is F_i yen.
On the other hand, a batch of D one-day passes is sold for P yen. You can buy as many passes as you want, but only in units of D.
Each purchased pass can be used on any day, and it is fine to have some leftovers at the end of the trip.
Find the minimum possible total cost for the N-day trip, that is, the cost of purchasing one-day passes plus the total regular fare for the days not covered by one-day passes.
Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq D\leq 2\times 10^5
- 1\leq P\leq 10^9
- 1\leq F_i\leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D P F_1 F_2 \ldots F_N
Output
Print the minimum possible total cost for the N-day trip.
Sample Input 1
5 2 10 7 1 6 3 6
Sample Output 1
20
If he buys just one batch of one-day passes and uses them for the first and third days, the total cost will be (10\times 1)+(0+1+0+3+6)=20, which is the minimum cost needed.
Thus, print 20.
Sample Input 2
3 1 10 1 2 3
Sample Output 2
6
The minimum cost is achieved by paying the regular fare for all three days.
Sample Input 3
8 3 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 3
3000000000
The minimum cost is achieved by buying three batches of one-day passes and using them for all eight days.
Note that the answer may not fit into a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
頂点に 1 から N の番号が付いた N 頂点の重み付き無向完全グラフが与えられます。頂点 i と頂点 j\ (i< j) を結ぶ辺の重みは D_{i,j} です。
以下の条件を満たすように何本かの辺を選ぶとき、選んだ辺の重みの総和としてあり得る最大値を求めてください。
- 選んだ辺の端点はどの 2 個も相異なる。
制約
- 2\leq N\leq 16
- 1\leq D_{i,j} \leq 10^9
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N D_{1,2} D_{1,3} \ldots D_{1,N} D_{2,3} \ldots D_{2,N} \vdots D_{N-1,N}
出力
答えを整数として出力せよ。
入力例 1
4 1 5 4 7 8 6
出力例 1
13
頂点 1 と頂点 3 を結ぶ辺、頂点 2 と頂点 4 を結ぶ辺を選ぶと、辺の重みの総和が 5+8=13 となります。
これが達成可能な最大値であることが示せます。
入力例 2
3 1 2 3
出力例 2
3
N が奇数の場合もあります。
入力例 3
16 5 6 5 2 1 7 9 7 2 5 5 2 4 7 6 8 7 7 9 8 1 9 6 10 8 8 6 10 3 10 5 8 1 10 7 8 4 8 6 5 1 10 7 4 1 4 5 4 5 10 1 5 1 2 2 9 9 7 6 2 2 8 3 5 2 9 10 3 1 1 2 10 7 7 5 10 6 1 8 9 3 2 4 2 10 10 8 9 2 10 7 9 5 8 8 7 5 8 2 4 2 2 6 8 3 2 7 3 10 3 5 7 10 3 8 5 7 9 1 4
出力例 3
75
Score : 400 points
Problem Statement
You are given a weighted undirected complete graph with N vertices numbered from 1 to N. The edge connecting vertices i and j (i< j) has a weight of D_{i,j}.
When choosing some number of edges under the following condition, find the maximum possible total weight of the chosen edges.
- The endpoints of the chosen edges are pairwise distinct.
Constraints
- 2\leq N\leq 16
- 1\leq D_{i,j} \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D_{1,2} D_{1,3} \ldots D_{1,N} D_{2,3} \ldots D_{2,N} \vdots D_{N-1,N}
Output
Print the answer as an integer.
Sample Input 1
4 1 5 4 7 8 6
Sample Output 1
13
If you choose the edge connecting vertices 1 and 3, and the edge connecting vertices 2 and 4, the total weight of the edges is 5+8=13.
It can be shown that this is the maximum achievable value.
Sample Input 2
3 1 2 3
Sample Output 2
3
N can be odd.
Sample Input 3
16 5 6 5 2 1 7 9 7 2 5 5 2 4 7 6 8 7 7 9 8 1 9 6 10 8 8 6 10 3 10 5 8 1 10 7 8 4 8 6 5 1 10 7 4 1 4 5 4 5 10 1 5 1 2 2 9 9 7 6 2 2 8 3 5 2 9 10 3 1 1 2 10 7 7 5 10 6 1 8 9 3 2 4 2 10 10 8 9 2 10 7 9 5 8 8 7 5 8 2 4 2 2 6 8 3 2 7 3 10 3 5 7 10 3 8 5 7 9 1 4
Sample Output 3
75
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。以下の条件を全て満たす正整数組 (i,j,k) の個数を求めてください。
- 1\leq i < j < k\leq N
- A_i = A_k
- A_i \neq A_j
制約
- 3\leq N\leq 3\times 10^5
- 1\leq A_i \leq N
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを整数として出力せよ。
入力例 1
5 1 2 1 3 2
出力例 1
3
条件を全て満たす正整数組 (i,j,k) は以下の 3 個です。
- (i,j,k)=(1,2,3)
- (i,j,k)=(2,3,5)
- (i,j,k)=(2,4,5)
入力例 2
7 1 2 3 4 5 6 7
出力例 2
0
条件を全て満たす正整数組 (i,j,k) が存在しない場合もあります。
入力例 3
13 9 7 11 7 3 8 1 13 11 11 11 6 13
出力例 3
20
Score : 450 points
Problem Statement
You are given a sequence of positive integers of length N: A=(A_1,A_2,\ldots,A_N). Find the number of triples of positive integers (i,j,k) that satisfy all of the following conditions:
- 1\leq i < j < k\leq N,
- A_i = A_k,
- A_i \neq A_j.
Constraints
- 3\leq N\leq 3\times 10^5
- 1\leq A_i \leq N
- 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
Output
Print the answer as an integer.
Sample Input 1
5 1 2 1 3 2
Sample Output 1
3
The following three triples of positive integers (i,j,k) satisfy the conditions:
- (i,j,k)=(1,2,3)
- (i,j,k)=(2,3,5)
- (i,j,k)=(2,4,5)
Sample Input 2
7 1 2 3 4 5 6 7
Sample Output 2
0
There may be no triples of positive integers (i,j,k) that satisfy the conditions.
Sample Input 3
13 9 7 11 7 3 8 1 13 11 11 11 6 13
Sample Output 3
20
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 575 点
問題文
数直線上に 1 体のタコ型ロボットと N 個の宝があります。
i (1\leq i\leq N) 個目の宝はそれぞれ座標 X_i にあります。
タコ型ロボットは 1 つの頭と N 本の足を持っており、i 本目の足の長さは L_i (1\leq i\leq N) です。
タコ型ロボットが次のようにして N 個の宝すべてを掴む事ができるような整数 k の個数を求めてください。
- 頭を座標 k におく。
- i=1,2,\ldots,N の順に、「頭から距離 L_i 以下の範囲、すなわち k-L_i\leq x\leq k+L_i をみたす座標 x にまだ掴んでいない宝が存在する場合、そのうちの 1 つを選んで掴む」ことを繰り返す。
制約
- 1 \leq N\leq 200
- -10^{18} \leq X_1<X_2<\cdots<X_N\leq 10^{18}
- 1\leq L_1\leq L_2\leq\cdots\leq L_N\leq 10^{18}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X_1 X_2 \ldots X_N L_1 L_2 \ldots L_N
出力
問題文の条件をみたす整数 k の個数を出力せよ。
入力例 1
3 -6 0 7 3 5 10
出力例 1
6
k=-3,-2,-1,2,3,4 が条件をみたします。例えば、k=-3 のときは、次のようにして 3 個の宝をすべて掴む事ができます。
- 1 本目の足は -6\leq x\leq 0 にある宝を掴む事ができる。このうち座標 -6 にある 1 個目の宝を掴む。
- 2 本目の足は -8\leq x\leq 2 にある宝を掴む事ができる。このうち座標 0 にある 2 個目の宝を掴む。
- 3 本目の足は -13\leq x\leq 7 にある宝を掴む事ができる。このうち座標 7 にある 3 個目の宝を掴む。
入力例 2
1 0 1000000000000000000
出力例 2
2000000000000000001
-10^{18} 以上 10^{18} 以下のすべての整数が k として条件をみたします。
入力例 3
2 -100 100 1 1
出力例 3
0
条件をみたす k は存在しません。
Score : 575 points
Problem Statement
There is an octopus-shaped robot and N treasures on a number line.
The i-th treasure (1\leq i\leq N) is located at coordinate X_i.
The robot has one head and N legs, and the i-th leg (1\leq i\leq N) has a length of L_i.
Find the number of integers k such that the robot can grab all N treasures as follows.
- Place the head at coordinate k.
- Repeat the following for i=1,2,\ldots,N in this order: if there is a treasure that has not been grabbed yet within a distance of L_i from the head, that is, at a coordinate x satisfying k-L_i\leq x\leq k+L_i, choose one such treasure and grab it.
Constraints
- 1 \leq N\leq 200
- -10^{18} \leq X_1<X_2<\cdots<X_N\leq 10^{18}
- 1\leq L_1\leq L_2\leq\cdots\leq L_N\leq 10^{18}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X_1 X_2 \ldots X_N L_1 L_2 \ldots L_N
Output
Print the number of integers k that satisfy the condition in the statement.
Sample Input 1
3 -6 0 7 3 5 10
Sample Output 1
6
k=-3,-2,-1,2,3,4 satisfy the condition. For example, when k=-3, the robot can grab all three treasures as follows.
- The first leg can grab treasures at coordinates x satisfying -6\leq x\leq 0. Among them, grab the first treasure at coordinate -6.
- The second leg can grab treasures at coordinates x satisfying -8\leq x\leq 2. Among them, grab the second treasure at coordinate 0.
- The third leg can grab treasures at coordinates x satisfying -13\leq x\leq 7. Among them, grab the third treasure at coordinate 7.
Sample Input 2
1 0 1000000000000000000
Sample Output 2
2000000000000000001
All integers k from -10^{18} to 10^{18} satisfy the condition.
Sample Input 3
2 -100 100 1 1
Sample Output 3
0
No k satisfies the conditions.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 575 点
問題文
N 頂点 M 辺の連結な単純無向グラフ G が与えられます。
G の頂点および辺は頂点 1, 頂点 2, \ldots, 頂点 N および辺 1, 辺 2, \ldots, 辺 M と番号づけられており、
辺 i (1\leq i\leq M) は頂点 U_i と頂点 V_i を結んでいます。
また、G 上の相異なる頂点 A,B,C が与えられます。
頂点 B を経由して頂点 A と頂点 C を結ぶ単純パスが存在するか判定してください。
連結な単純無向グラフとは
グラフ G が連結な単純無向グラフであるとは、 G が連結かつ単純な無向グラフであることをいいます。グラフ G が無向グラフであるとは、G の辺に向きが無いことをいいます。
グラフ G が単純であるとは、G が自己ループや多重辺を含まないことをいいます。
グラフ G が連結であるとは、G に含まれるすべての頂点同士が辺を経由して互いに行き来できることをいいます。
頂点 Z を経由する単純パスとは
グラフ G 上の頂点 X,Y について、頂点 X と頂点 Y を結ぶ単純パスとは、相異なる頂点列 (v_1,v_2,\ldots,v_k) であって、v_1=X, v_k=Y かつ 任意の 1\leq i\leq k-1 をみたす整数 i について、頂点 v_i と頂点 v_{i+1} を結ぶ辺が G 上に存在するようなものを指します。また、単純パス (v_1,v_2,\ldots,v_k) が頂点 Z を経由するとは、ある i (2\leq i\leq k-1) が存在して v_i=Z をみたすことを指します。
制約
- 3 \leq N \leq 2\times 10^5
- N-1\leq M\leq\min\left(\frac{N(N-1)}{2},2\times 10^5\right)
- 1\leq A,B,C\leq N
- A,B,C はすべて異なる。
- 1\leq U_i<V_i\leq N
- (U_i,V_i) はすべて異なる。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A B C U_1 V_1 U_2 V_2 \vdots U_M V_M
出力
問題文の条件をみたすような単純パスが存在するならば Yes
を、存在しないならば No
を出力せよ。
入力例 1
6 7 1 3 2 1 2 1 5 2 3 2 5 2 6 3 4 4 5
出力例 1
Yes
頂点 3 を経由して頂点 1 と頂点 2 を結ぶ単純パスとしては、頂点 1 \to 頂点 5 \to 頂点 4 \to 頂点 3 \to 頂点 2 などが考えられます。
よって、Yes
を出力します。
入力例 2
6 6 1 3 2 1 2 2 3 2 5 2 6 3 4 4 5
出力例 2
No
条件をみたすような単純パスは存在しません。よって、No
を出力します。
入力例 3
3 2 1 3 2 1 2 2 3
出力例 3
No
Score : 575 points
Problem Statement
You are given a simple connected undirected graph G with N vertices and M edges.
The vertices and edges of G are numbered as vertex 1, vertex 2, \ldots, vertex N, and edge 1, edge 2, \ldots, edge M, respectively, and edge i (1\leq i\leq M) connects vertices U_i and V_i.
You are also given distinct vertices A,B,C on G. Determine if there is a simple path connecting vertices A and C via vertex B.
What is a simple connected undirected graph?
A graph G is said to be a simple connected undirected graph when G is an undirected graph that is simple and connected.A graph G is said to be an undirected graph when the edges of G have no direction.
A graph G is simple when G does not contain self-loops or multi-edges.
A graph G is connected when one can travel between all vertices of G via edges.
What is a simple path via vertex Z?
For vertices X and Y on a graph G, a simple path connecting X and Y is a sequence of distinct vertices (v_1,v_2,\ldots,v_k) such that v_1=X, v_k=Y, and for every integer i satisfying 1\leq i\leq k-1, there is an edge on G connecting vertices v_i and v_{i+1}.A simple path (v_1,v_2,\ldots,v_k) is said to be via vertex Z when there is an i (2\leq i\leq k-1) satisfying v_i=Z.
Constraints
- 3 \leq N \leq 2\times 10^5
- N-1\leq M\leq\min\left(\frac{N(N-1)}{2},2\times 10^5\right)
- 1\leq A,B,C\leq N
- A, B, and C are all distinct.
- 1\leq U_i<V_i\leq N
- The pairs (U_i,V_i) are all distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A B C U_1 V_1 U_2 V_2 \vdots U_M V_M
Output
If there is a simple path that satisfies the condition in the statement, print Yes
; otherwise, print No
.
Sample Input 1
6 7 1 3 2 1 2 1 5 2 3 2 5 2 6 3 4 4 5
Sample Output 1
Yes
One simple path connecting vertices 1 and 2 via vertex 3 is 1 \to 5 \to 4 \to 3 \to 2.
Thus, print Yes
.
Sample Input 2
6 6 1 3 2 1 2 2 3 2 5 2 6 3 4 4 5
Sample Output 2
No
No simple path satisfies the condition. Thus, print No
.
Sample Input 3
3 2 1 3 2 1 2 2 3
Sample Output 3
No
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 650 点
問題文
すぬけくんは以下の問題を考えました。
(1,2,\ldots,N) の順列 P=(P_1,P_2,\ldots,P_N),Q=(Q_1,Q_2,\ldots,Q_N) が与えられます。 以下の方法で N 頂点 N 辺のグラフを作ります。
- i=1,2,\ldots,N の順に、頂点 i と頂点 P_i を双方向に結ぶ重さ Q_i の辺を張る。
グラフが閉路を含まないように何本か辺を削除するとき、削除する辺の重みの総和の最小値を求めてください。
Alice と Bob は以下の解法をそれぞれ考えました。
Alice: 答えを 0 で初期化する。i=1,2,\ldots,N の順に、頂点 i と頂点 P_i を結ぶ辺が閉路に含まれるならその辺を削除し、削除した辺の重みを答えに加算する。
Bob: 答えを 0 で初期化する。i=N,N-1,\ldots,1 の順に、頂点 i と頂点 P_i を結ぶ辺が閉路に含まれるならその辺を削除し、削除した辺の重みを答えに加算する。
すぬけくんは Alice と Bob の解法がどちらも誤っていることに気付いたので、二人の解法の答えが共に正しい答えと異なるような入力の個数が知りたくなりました。
入力は (N!)^2 通り考えられますが、その内 Alice と Bob の解法の答えが共に正しい答えと異なるものの個数を 998244353 で割ったあまりを求めてください。
制約
- 1\leq N\leq 2\times 10^5
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを整数として出力せよ。
入力例 1
3
出力例 1
4
条件を満たす入力は以下の 4 通りです。
- P=(2,3,1),Q=(2,1,3)
- P=(2,3,1),Q=(3,1,2)
- P=(3,1,2),Q=(2,1,3)
- P=(3,1,2),Q=(3,1,2)
例えば P=(2,3,1),Q=(2,1,3) という入力では、正しい答えは 1 ですが、Alice の解法は 2 、Bob の解法は 3 を答えとします。
入力例 2
2
出力例 2
0
条件を満たす入力が存在しない場合もあります。
入力例 3
6
出力例 3
314708
入力例 4
318
出力例 4
321484323
Score : 650 points
Problem Statement
Snuke has come up with the following problem.
You are given permutations P=(P_1,P_2,\ldots,P_N) and Q=(Q_1,Q_2,\ldots,Q_N) of (1,2,\ldots,N). Let us build a graph with N vertices and N edges as follows.
- For i=1,2,\ldots,N in this order, draw an edge of weight Q_i connecting vertices i and P_i bidirectionally.
When removing some number of edges to eliminate cycles from the graph, find the minimum possible total weight of the removed edges.
Alice and Bob came up with the following solutions.
Alice: Initialize the answer to 0. For i=1,2,\ldots,N in this order, if the edge connecting vertices i and P_i is contained in a cycle, remove that edge and add its weight to the answer.
Bob: Initialize the answer to 0. For i=N,N-1,\ldots,1 in this order, if the edge connecting vertices i and P_i is contained in a cycle, remove that edge and add its weight to the answer.
Snuke has realized that their solutions are both incorrect, and he wants to know the number of inputs for which neither of their solutions gives the correct answer.
Among the (N!)^2 possible inputs, find the number, modulo 998244353, of inputs for which neither Alice's nor Bob's solution gives the correct answer.
Constraints
- 1\leq N\leq 2\times 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer as an integer.
Sample Input 1
3
Sample Output 1
4
The following four inputs satisfy the condition.
- P=(2,3,1),Q=(2,1,3)
- P=(2,3,1),Q=(3,1,2)
- P=(3,1,2),Q=(2,1,3)
- P=(3,1,2),Q=(3,1,2)
For example, for the input P=(2,3,1),Q=(2,1,3), the correct answer is 1, but Alice's solution gives 2 and Bob's gives 3.
Sample Input 2
2
Sample Output 2
0
There may be no inputs that satisfy the condition.
Sample Input 3
6
Sample Output 3
314708
Sample Input 4
318
Sample Output 4
321484323