A - 1-2-4 Test

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 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.

B - Hammer

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

数直線の原点に高橋君がいます。高橋君は座標 X にあるゴールに移動しようとしています。

座標 Y には壁があり、最初、高橋君は壁を超えて移動することができません。
座標 Z にあるハンマーを拾った後でなら、壁を破壊して通過できるようになります。

高橋君がゴールに到達することが可能か判定し、可能であれば移動距離の最小値を求めてください。

制約

  • -1000 \leq X,Y,Z \leq 1000
  • X,Y,Z は相異なり、いずれも 0 でない
  • 入力に含まれる値は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

X Y Z

出力

高橋君がゴールに到達することが可能であれば、移動距離の最小値を出力せよ。不可能であれば、かわりに -1 と出力せよ。


入力例 1

10 -10 1

出力例 1

10

高橋君はまっすぐゴールに向かうことができます。


入力例 2

20 10 -10

出力例 2

40

ゴールは壁の向こう側にあります。まずハンマーを拾い、壁を壊すことでゴールに到達することができます。


入力例 3

100 1 1000

出力例 3

-1

Score : 200 points

Problem Statement

Takahashi is at the origin of a number line. He wants to reach a goal at coordinate X.

There is a wall at coordinate Y, which Takahashi cannot go beyond at first.
However, after picking up a hammer at coordinate Z, he can destroy that wall and pass through.

Determine whether Takahashi can reach the goal. If he can, find the minimum total distance he needs to travel to do so.

Constraints

  • -1000 \leq X,Y,Z \leq 1000
  • X, Y, and Z are distinct, and none of them is 0.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

X Y Z

Output

If Takahashi can reach the goal, print the minimum total distance he needs to travel to do so. If he cannot, print -1 instead.


Sample Input 1

10 -10 1

Sample Output 1

10

Takahashi can go straight to the goal.


Sample Input 2

20 10 -10

Sample Output 2

40

The goal is beyond the wall. He can get there by first picking up the hammer and then destroying the wall.


Sample Input 3

100 1 1000

Sample Output 3

-1
C - Simple path

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N 頂点の木 T があり、 i (1\leq i\leq N-1) 番目の辺は頂点 U_i と頂点 V_i を結んでいます。

T 上の相異なる 2 頂点 X,Y が与えられるので、 頂点 X から頂点 Y への単純パス上の頂点(端点含む)を順に列挙してください。

ただし、木上の任意の相異なる 2 頂点 a,b について、a から b への単純パスがただ一つ存在することが証明できます。

単純パスとは? グラフ G 上の頂点 X,Y に対して、頂点列 v_1,v_2, \ldots, v_k であって、 v_1=X, v_k=Y かつ、1\leq i\leq k-1 に対して v_iv_{i+1} が辺で結ばれているようなものを頂点 X から頂点 Y への パス と呼びます。 さらに、v_1,v_2, \ldots, v_k がすべて異なるようなものを頂点 X から頂点 Y への 単純パス と呼びます。

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq X,Y\leq N
  • X\neq Y
  • 1\leq U_i,V_i\leq N
  • 入力はすべて整数
  • 与えられるグラフは木

入力

入力は以下の形式で標準入力から与えられる。

N X Y
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

出力

頂点 X から頂点 Y への単純パス上の頂点番号を順に空白区切りで出力せよ。


入力例 1

5 2 5
1 2
1 3
3 4
3 5

出力例 1

2 1 3 5

T は以下のような形であり、頂点 2 から頂点 5への単純パスは 頂点 2 \to 頂点 1 \to 頂点 3 \to 頂点 5 となります。
よって、2,1,3,5 をこの順に空白区切りで出力します。


入力例 2

6 1 2
3 1
2 5
1 2
4 1
2 6

出力例 2

1 2

T は以下のような形です。

Score : 300 points

Problem Statement

There is a tree T with N vertices. The i-th edge (1\leq i\leq N-1) connects vertex U_i and vertex V_i.

You are given two different vertices X and Y in T. List all vertices along the simple path from vertex X to vertex Y in order, including endpoints.

It can be proved that, for any two different vertices a and b in a tree, there is a unique simple path from a to b.

What is a simple path? For vertices X and Y in a graph G, a path from vertex X to vertex Y is a sequence of vertices v_1,v_2, \ldots, v_k such that v_1=X, v_k=Y, and v_i and v_{i+1} are connected by an edge for each 1\leq i\leq k-1. Additionally, if all of v_1,v_2, \ldots, v_k are distinct, the path is said to be a simple path from vertex X to vertex Y.

Constraints

  • 1\leq N\leq 2\times 10^5
  • 1\leq X,Y\leq N
  • X\neq Y
  • 1\leq U_i,V_i\leq N
  • All values in the input are integers.
  • The given graph is a tree.

Input

The input is given from Standard Input in the following format:

N X Y
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

Output

Print the indices of all vertices along the simple path from vertex X to vertex Y in order, with spaces in between.


Sample Input 1

5 2 5
1 2
1 3
3 4
3 5

Sample Output 1

2 1 3 5

The tree T is shown below. The simple path from vertex 2 to vertex 5 is 2 \to 1 \to 3 \to 5.
Thus, 2,1,3,5 should be printed in this order, with spaces in between.


Sample Input 2

6 1 2
3 1
2 5
1 2
4 1
2 6

Sample Output 2

1 2

The tree T is shown below.

D - Stones

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

数列 (A_1,\ldots,A_K) を使って、高橋君と青木君が石取りゲームをします。

最初、山には N 個の石があります。高橋君から順に、二人が交互に次の操作を行います。

  • 現在山にある石の個数以下であるような A_i1 つ選ぶ。山から A_i 個の石を取り除く。

山から石がなくなったとき、ゲームは終了します。

二人がともに、ゲーム終了までに自分が取り除いた石の個数を最大化しようとするとき、高橋君は何個の石を取り除くことができますか?

制約

  • 1 \leq N \leq 10^4
  • 1 \leq K \leq 100
  • 1 = A_1 < A_2 < \ldots < A_K \leq N
  • 入力に含まれる値は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N K
A_1 A_2 \ldots A_K

出力

答えを出力せよ。


入力例 1

10 2
1 4

出力例 1

5

ゲームの進行の一例は以下の通りです。

  • 高橋君が山から 4 個の石を取り除く。
  • 青木君が山から 4 個の石を取り除く。
  • 高橋君が山から 1 個の石を取り除く。
  • 青木君が山から 1 個の石を取り除く。

この例では高橋君は 5 個の石を取り除くことができます。高橋君が 6 個以上の石を取り除くことはできないためこれが最大です。

高橋君は 5 個の石を取り除くことができるゲームの進行の例には、他にも次のようなものがあります。

  • 高橋君が山から 1 個の石を取り除く。
  • 青木君が山から 4 個の石を取り除く。
  • 高橋君が山から 4 個の石を取り除く。
  • 青木君が山から 1 個の石を取り除く。

入力例 2

11 4
1 2 3 6

出力例 2

8

ゲームの進行の一例は以下の通りです。

  • 高橋君が山から 6 個の石を取り除く。
  • 青木君が山から 3 個の石を取り除く。
  • 高橋君が山から 2 個の石を取り除く。

この例では高橋君は 8 個の石を取り除くことができます。高橋君が 9 個以上の石を取り除くことはできないためこれが最大です。


入力例 3

10000 10
1 2 4 8 16 32 64 128 256 512

出力例 3

5136

Score : 400 points

Problem Statement

Takahashi and Aoki will play a game of taking stones using a sequence (A_1, \ldots, A_K).

There is a pile that initially contains N stones. The two players will alternately perform the following operation, with Takahashi going first.

  • Choose an A_i that is at most the current number of stones in the pile. Remove A_i stones from the pile.

The game ends when the pile has no stones.

If both players attempt to maximize the total number of stones they remove before the end of the game, how many stones will Takahashi remove?

Constraints

  • 1 \leq N \leq 10^4
  • 1 \leq K \leq 100
  • 1 = A_1 < A_2 < \ldots < A_K \leq N
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_K

Output

Print the answer.


Sample Input 1

10 2
1 4

Sample Output 1

5

Below is one possible progression of the game.

  • Takahashi removes 4 stones from the pile.
  • Aoki removes 4 stones from the pile.
  • Takahashi removes 1 stone from the pile.
  • Aoki removes 1 stone from the pile.

In this case, Takahashi removes 5 stones. There is no way for him to remove 6 or more stones, so this is the maximum.

Below is another possible progression of the game where Takahashi removes 5 stones.

  • Takahashi removes 1 stone from the pile.
  • Aoki removes 4 stones from the pile.
  • Takahashi removes 4 stones from the pile.
  • Aoki removes 1 stone from the pile.

Sample Input 2

11 4
1 2 3 6

Sample Output 2

8

Below is one possible progression of the game.

  • Takahashi removes 6 stones.
  • Aoki removes 3 stones.
  • Takahashi removes 2 stones.

In this case, Takahashi removes 8 stones. There is no way for him to remove 9 or more stones, so this is the maximum.


Sample Input 3

10000 10
1 2 4 8 16 32 64 128 256 512

Sample Output 3

5136
E - Apple Baskets on Circle

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1,2,\ldots,N の番号がついた N 個のかごが円状に置かれています。
1\leq i \leq N-1 についてかご i の右隣にはかご i+1 があり、かご N の右隣にはかご 1 があります。

かご i の中には A_i 個りんごが入っています。

高橋君は最初かご 1 の前におり、以下の行動を繰り返します。

  • 目の前にあるかごの中にりんごがあれば 1 個かごから取り出して食べる。その後、りんごを食べたかどうかにかかわらず、右隣のかごの前に移動する。

高橋君がちょうど K 個のりんごを食べた時点で、各かごの中に残っているりんごの個数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq A_i \leq 10^{12}
  • 1 \leq K \leq 10^{12}
  • りんごは全部で K 個以上ある。すなわち、\sum_{i=1}^{N}A_i\geq K
  • 入力に含まれる値は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N K
A_1 A_2 \ldots A_N

出力

N 個の整数を空白区切りで出力せよ。
i 番目には、高橋君がちょうど K 個のりんごを食べた時点で、かご i の中に残っているりんごの個数を出力せよ。


入力例 1

3 3
1 3 0

出力例 1

0 1 0 

高橋君は次のように行動します。

  • 目の前にあるかご 1 の中にりんごがあるので 1 個かごから取り出して食べる。その後、かご 2 の前に移動する。この時点で各かごの中のりんごの個数は (0,3,0) である。
  • 目の前にあるかご 2 の中にりんごがあるので 1 個かごから取り出して食べる。その後、かご 3 の前に移動する。この時点で各かごの中のりんごの個数は (0,2,0) である。
  • 目の前にあるかご 3 の中にりんごはない。かご 1 の前に移動する。この時点で各かごの中のりんごの個数は (0,2,0) である。
  • 目の前にあるかご 1 の中にりんごはない。かご 2 の前に移動する。この時点で各かごの中のりんごの個数は (0,2,0) である。
  • 目の前にあるかご 2 の中にりんごがあるので 1 個かごから取り出して食べる。その後、かご 3 の前に移動する。この時点で各かごの中のりんごの個数は (0,1,0) である。

入力例 2

2 1000000000000
1000000000000 1000000000000

出力例 2

500000000000 500000000000 

Score : 500 points

Problem Statement

There are N baskets numbered 1, 2, \ldots, N arranged in a circle.
For each 1\leq i \leq N-1, basket i+1 is to the immediate right of basket i, and basket 1 is to the immediate right of basket N.

Basket i now contains A_i apples.

Takahashi starts in front of basket 1 and repeats the following action.

  • If the basket he is facing contains an apple, take one and eat it. Then, regardless of whether he has eaten an apple now, go on to the next basket to the immediate right.

Find the number of apples remaining in each basket when Takahashi has eaten exactly K apples in total.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq A_i \leq 10^{12}
  • 1 \leq K \leq 10^{12}
  • There are at least K apples in total. That is, \sum_{i=1}^{N}A_i\geq K.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_N

Output

Print N integers, with spaces in between.
The i-th integer should be the number of apples remaining in basket i when Takahashi has eaten exactly K apples in total.


Sample Input 1

3 3
1 3 0

Sample Output 1

0 1 0 

Takahashi will do the following.

  • Basket 1, which he is facing, contains an apple, so he takes one and eats it. Then, he goes on to basket 2. Now, the baskets have 0,3,0 apples.
  • Basket 2, which he is facing, contains an apple, so he takes one and eats it. Then, he goes on to basket 3. Now, the baskets have 0,2,0 apples.
  • Basket 3, which he is facing, contains no apple. Then, he goes on to basket 1. Now, the baskets have 0,2,0 apples.
  • Basket 1, which he is facing, contains no apple. Then, he goes on to basket 2. Now, the baskets have 0,2,0 apples.
  • Basket 2, which he is facing, contains an apple, so he takes one and eats it. Then, he goes on to basket 3. Now, the baskets have 0,1,0 apple(s).

Sample Input 2

2 1000000000000
1000000000000 1000000000000

Sample Output 2

500000000000 500000000000 
F - Transportation

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 500

問題文

AtCoder 国には N 個の島があり、 最初、どの島にも空港・港はなく、どの島の間にも道路はありません。 王である高橋君はこれらの島の間に交通手段を用意することにしました。 具体的には、高橋君は次の操作のうち 1 つを選んで行うことを好きなだけ繰り返す事ができます。

  • 1\leq i\leq N をみたす i を選び、コスト X_i を払って、島 i に空港を建設する。
  • 1\leq i\leq N をみたす i を選び、コスト Y_i を払って、島 i に港を建設する。
  • 1\leq i\leq M をみたす i を選び、コスト Z_i を払って、島 A_i と島 B_i の間を双方向に結ぶ道路を建設する。

高橋君の目標は、任意の相異なる 2 つの島 U, V について、 島 U からはじめて次の行動のうち 1 つを選んで行うことを好きなだけ繰り返す事で、 島 V に到達することができるようにする事です。

  • S,T の両方に空港がある時、島 S から島 T まで移動する。
  • S,T の両方に港がある時、島 S から島 T まで移動する。
  • S,T を結ぶ道路が存在する時、島 S から島 T まで移動する。

高橋君が目標を達成するのに必要な最小コストを求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq M \leq 2\times 10^5
  • 1\leq X_i\leq 10^9
  • 1\leq Y_i\leq 10^9
  • 1\leq A_i<B_i\leq N
  • 1\leq Z_i\leq 10^9
  • i\neq j ならば (A_i,B_i)\neq (A_j,B_j)
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N M
X_1 X_2 \ldots X_N
Y_1 Y_2 \ldots Y_N
A_1 B_1 Z_1
A_2 B_2 Z_2
\vdots
A_M B_M Z_M

出力

高橋君が目標を達成するのに必要な最小コストを出力せよ。


入力例 1

4 2
1 20 4 7
20 2 20 3
1 3 5
1 4 6

出力例 1

16

高橋君は次のように交通手段を建設します。

  • コスト X_1=1 を払って、島 1 に空港を建設する。
  • コスト X_3=4 を払って、島 3 に空港を建設する。
  • コスト Y_2=2 を払って、島 2 に港を建設する。
  • コスト Y_4=3 を払って、島 4 に港を建設する。
  • コスト Z_2=6 を払って、島 1 と島 4 の間を結ぶ道路を建設する。

このとき、目標は達成されており、かかったコストは 16 となります。 コスト 15 以下で目標を達成する方法はないため、16 を出力します。


入力例 2

3 1
1 1 1
10 10 10
1 2 100

出力例 2

3

空港・港・道路のうち、一度も建設されないものがあっても構いません。


入力例 3

7 8
35 29 36 88 58 15 25
99 7 49 61 67 4 57
2 3 3
2 5 36
2 6 89
1 6 24
5 7 55
1 3 71
3 4 94
5 6 21

出力例 3

160

Score : 500 points

Problem Statement

The Republic of AtCoder has N islands. Initially, none of the islands has an airport or harbor, and there is no road between any two of them. Takahashi, the king, will provide some means of transportation between these islands. Specifically, he can do the following operations any number of times in any order.

  • Choose an integer i such that 1\leq i\leq N and pay a cost of X_i to build an airport on island i.
  • Choose an integer i such that 1\leq i\leq N and pay a cost of Y_i to build a harbor on island i.
  • Choose an integer i such that 1\leq i\leq M and pay a cost of Z_i to build a road that bidirectionally connects island A_i and island B_i.

Takahashi's objective is to make it possible, for every pair of different islands U and V, to reach island V from island U when one can perform the following actions any number of times in any order.

  • When islands S and T both have airports, go from island S to island T.
  • When islands S and T both have harbors, go from island S to island T.
  • When islands S and T are connected by a road, go from island S to island T.

Find the minimum total cost Takahashi needs to pay to achieve his objective.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq M \leq 2\times 10^5
  • 1\leq X_i\leq 10^9
  • 1\leq Y_i\leq 10^9
  • 1\leq A_i<B_i\leq N
  • 1\leq Z_i\leq 10^9
  • (A_i,B_i)\neq (A_j,B_j), if i\neq j.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N M
X_1 X_2 \ldots X_N
Y_1 Y_2 \ldots Y_N
A_1 B_1 Z_1
A_2 B_2 Z_2
\vdots
A_M B_M Z_M

Output

Print the minimum total cost Takahashi needs to pay to achieve his objective.


Sample Input 1

4 2
1 20 4 7
20 2 20 3
1 3 5
1 4 6

Sample Output 1

16

Takahashi will build the following infrastructure.

  • Pay a cost of X_1=1 to build an airport on island 1.
  • Pay a cost of X_3=4 to build an airport on island 3.
  • Pay a cost of Y_2=2 to build a harbor on island 2.
  • Pay a cost of Y_4=3 to build a harbor on island 4.
  • Pay a cost of Z_2=6 to build a road connecting island 1 and island 4.

Then, the objective will be achieved at a cost of 16. There is no way to achieve the objective for a cost of 15 or less, so 16 should be printed.


Sample Input 2

3 1
1 1 1
10 10 10
1 2 100

Sample Output 2

3

It is not mandatory to build all three types of facilities at least once.


Sample Input 3

7 8
35 29 36 88 58 15 25
99 7 49 61 67 4 57
2 3 3
2 5 36
2 6 89
1 6 24
5 7 55
1 3 71
3 4 94
5 6 21

Sample Output 3

160
G - Sequence in mod P

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 600

問題文

次の漸化式で定められる数列 X=(X_0,X_1,\ldots) があります。

X_i = \left\{ \begin{array}{ll} S & (i = 0)\\ (A X_{i-1}+B) \bmod P & (i \geq 1) \end{array} \right.

X_i=G となる i が存在するか判定し、存在するならばそのような最小の i を求めてください。
ここで、x \bmod y は、xy で割ったあまり(最小非負剰余)を表すものとします。

1 ファイルにつき T 個のテストケースが与えられます。

制約

  • 1 \leq T \leq 100
  • 2 \leq P \leq 10^9
  • P は素数
  • 0\leq A,B,S,G < P
  • 入力に含まれる値は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる。

P A B S G

出力

T 行出力せよ。
t 行目には、\mathrm{case}_t について、X_i=G となる最小の i を出力せよ。そのような i が存在しないならかわりに -1 を出力せよ。


入力例 1

3
5 2 1 1 0
5 2 2 3 0
11 1 1 0 10

出力例 1

3
-1
10

1 番目のケースについて、X=(1,3,2,0,\ldots) であることから、X_i=0 となる最小の i3 です。
2 番目のケースについて、X=(3,3,3,3,\ldots) であることから、X_i=0 となる i は存在しません。

Score : 600 points

Problem Statement

There is a sequence X=(X_0, X_1, \ldots) defined by the following recurrence relation.

X_i = \left\{ \begin{array}{ll} S & (i = 0)\\ (A X_{i-1}+B) \bmod P & (i \geq 1) \end{array} \right.

Determine whether there is an i such that X_i=G. If it exists, find the smallest such i.
Here, x \bmod y denotes the remainder when x is divided by y (the least non-negative residue).

You are given T test cases for each input file.

Constraints

  • 1 \leq T \leq 100
  • 2 \leq P \leq 10^9
  • P is a prime.
  • 0\leq A,B,S,G < P
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is in the following format:

P A B S G

Output

Print T lines.
The t-th line should contain the smallest i such that X_i=G for \mathrm{case}_t, or -1 if there is no such i.


Sample Input 1

3
5 2 1 1 0
5 2 2 3 0
11 1 1 0 10

Sample Output 1

3
-1
10

For the first test case, we have X=(1,3,2,0,\ldots), so the smallest i such that X_i=0 is 3.
For the second test case, we have X=(3,3,3,3,\ldots), so there is no i such that X_i=0.

Ex - add 1

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

A_1=0 かつ A_N>0 であるような、N 個の非負整数の組 A=(A_1,A_2,\ldots,A_N) が与えられます。

高橋君は N 個のカウンターを持っており、最初、全てのカウンターの値は 0 です。
高橋君は、全ての 1\leq i\leq N について i 番目のカウンターの値が A_i 以上となるまで次の操作を繰り返します。

N 個のカウンターの中から 1 つを等確率に選び、その値を 0 にする。(選択は毎回独立に行う。)
選んだカウンター 以外 のカウンターの値を 1 増加させる。

高橋君の操作回数の期待値を \mathrm{mod} 998244353 で出力してください(注記参照)。

注記

求める期待値は必ず有限値かつ有理数となることが証明できます。また、この問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ一つ存在することが証明できます。この R を求めてください。

制約

  • 2\leq N\leq 2\times 10^5
  • 0=A_1\leq A_2\leq \cdots \leq A_N\leq 10^{18}
  • A_N>0
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2 \ldots A_N

出力

高橋君の操作回数の期待値を \mathrm{mod} 998244353 で出力せよ。


入力例 1

2
0 2

出力例 1

6

i 番目のカウンターの値を C_i で表します。

高橋君の操作が終了するまでの一連の流れの例は次の通りです。

  • 1 番目のカウンターの値を 0 にした後、それ以外のカウンターの値を 1 増加させる。 (C_1,C_2)=(0,1) となる。
  • 2 番目のカウンターの値を 0 にした後、それ以外のカウンターの値を 1 増加させる。 (C_1,C_2)=(1,0) となる。
  • 1 番目のカウンターの値を 0 にした後、それ以外のカウンターの値を 1 増加させる。 (C_1,C_2)=(0,1) となる。
  • 1 番目のカウンターの値を 0 にした後、それ以外のカウンターの値を 1 増加させる。 (C_1,C_2)=(0,2) となる。

この場合の操作回数は 4 となります。

1,2,3,4,5,\ldots 回で操作が終了する確率はそれぞれ 0,\frac{1}{4}, \frac{1}{8}, \frac{1}{8}, \frac{3}{32},\ldots であり、 期待値は 2\times\frac{1}{4}+3\times\frac{1}{8}+4\times\frac{1}{8}+5\times\frac{3}{32}+\dots=6 となります。 よって、 6 を出力します。


入力例 2

5
0 1 3 10 1000000000000000000

出力例 2

874839568

Score : 600 points

Problem Statement

You are given a tuple of N non-negative integers A=(A_1,A_2,\ldots,A_N) such that A_1=0 and A_N>0.

Takahashi has N counters. Initially, the values of all counters are 0.
He will repeat the following operation until, for every 1\leq i\leq N, the value of the i-th counter is at least A_i.

Choose one of the N counters uniformly at random and set its value to 0. (Each choice is independent of others.)
Increase the values of the other counters by 1.

Print the expected value of the number of times Takahashi repeats the operation, modulo 998244353 (see Notes).

Notes

It can be proved that the sought expected value is always finite and rational. Additionally, under the Constraints of this problem, when that value is represented as \frac{P}{Q} using two coprime integers P and Q, one can prove that there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Find this R.

Constraints

  • 2\leq N\leq 2\times 10^5
  • 0=A_1\leq A_2\leq \cdots \leq A_N\leq 10^{18}
  • A_N>0
  • All values in the input 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 expected value of the number of times Takahashi repeats the operation, modulo 998244353.


Sample Input 1

2
0 2

Sample Output 1

6

Let C_i denote the value of the i-th counter.

Here is one possible progression of the process.

  • Set the value of the 1-st counter to 0, and then increase the value of the other counter by 1. Now, (C_1,C_2)=(0,1).
  • Set the value of the 2-nd counter to 0, and then increase the value of the other counter by 1. Now, (C_1,C_2)=(1,0).
  • Set the value of the 1-st counter to 0, and then increase the value of the other counter by 1. Now, (C_1,C_2)=(0,1).
  • Set the value of the 1-st counter to 0, and then increase the value of the other counter by 1. Now, (C_1,C_2)=(0,2).

In this case, the operation is performed four times.

The probabilities that the process ends after exactly 1,2,3,4,5,\ldots operation(s) are 0,\frac{1}{4}, \frac{1}{8}, \frac{1}{8}, \frac{3}{32},\ldots, respectively, so the sought expected value is 2\times\frac{1}{4}+3\times\frac{1}{8}+4\times\frac{1}{8}+5\times\frac{3}{32}+\dots=6. Thus, 6 should be printed.


Sample Input 2

5
0 1 3 10 1000000000000000000

Sample Output 2

874839568