A - Alloy

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

高橋くんは A グラムの純金と B グラムの純銀 (0 \leq A,B,\ 0 \lt A+B) をよく溶かした上で混ぜ合わせ、新たな金属を生成しました。

生成された金属は「純金」「純銀」「合金」のいずれでしょうか?

なお、生成された金属は

  • 0 \lt A かつ B=0 なら「純金」
  • A=0 かつ 0 \lt B なら「純銀」
  • 0 \lt A かつ 0 \lt B なら「合金」

であるとみなします。

制約

  • 0 \leq A,B \leq 100
  • 0 \lt A+B
  • A,B は整数

入力

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

A B

出力

生成された金属が「純金」なら Gold と、「純銀」なら Silver と、「合金」なら Alloy と出力せよ。


入力例 1

50 50

出力例 1

Alloy

0 \lt A かつ 0 \lt B であるため、生成された金属は「合金」です。


入力例 2

100 0

出力例 2

Gold

0 \lt A かつ B=0 であるため、生成された金属は「純金」です。


入力例 3

0 100

出力例 3

Silver

A=0 かつ 0 \lt B であるため、生成された金属は「純銀」です。


入力例 4

100 2

出力例 4

Alloy

Score : 100 points

Problem Statement

Takahashi melted and mixed A grams of gold and B grams of silver (0 \leq A,B,\ 0 \lt A+B) to produce new metal.

What metal did he produce: pure gold, pure silver, or an alloy?

Formally, the product is called as follows.

  • Pure gold, if 0 \lt A and B=0.
  • Pure silver, if A=0 and 0 \lt B.
  • An alloy, if 0 \lt A and 0 \lt B.

Constraints

  • 0 \leq A,B \leq 100
  • 1 \leq A+B
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If the product is pure gold, print Gold; if it is pure silver, print Silver; if it is an alloy, print Alloy.


Sample Input 1

50 50

Sample Output 1

Alloy

We have 0 \lt A and 0 \lt B, so the product is an alloy.


Sample Input 2

100 0

Sample Output 2

Gold

We have 0 \lt A and B=0, so the product is pure gold.


Sample Input 3

0 100

Sample Output 3

Silver

We have A=0 and 0 \lt B, so the product is pure silver.


Sample Input 4

100 2

Sample Output 4

Alloy
B - Weak Password

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

4 桁の暗証番号 X_1X_2X_3X_4 が与えられます。 番号は先頭の桁が 0 であることもあり得ます。 暗証番号は以下のいずれかの条件をみたすとき弱い暗証番号と呼ばれます。

  • 4 桁とも同じ数字である。
  • 1\leq i\leq 3 をみたす任意の整数 i について、 X_{i+1} が、 X_i の次の数字である。 ただし、 0\leq j\leq 8 について j の次の数字は j+1 であり、 9 の次の数字は 0 である。

与えられた暗証番号が弱い暗証番号ならば Weak を、そうでないならば Strong を出力してください。

制約

  • 0 \leq X_1, X_2, X_3, X_4 \leq 9
  • X_1, X_2, X_3, X_4 は整数である。

入力

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

X_1X_2X_3X_4

出力

与えられた暗証番号が弱い暗証番号ならば Weak を、そうでないならば Strong を出力せよ。


入力例 1

7777

出力例 1

Weak

4 桁ともすべて 7 であるため、 1 つめの条件をみたしており、弱い暗証番号です。


入力例 2

0112

出力例 2

Strong

1 桁目と 2 桁目が異なっており、 3 桁目は 2 桁目の次の数字ではないため、どちらの条件もみたしていません。


入力例 3

9012

出力例 3

Weak

9 の次の数字が 0 であることに注意してください。

Score : 200 points

Problem Statement

You are given a 4-digit PIN: X_1X_2X_3X_4, which may begin with a 0. The PIN is said to be weak when it satisfies one of the following conditions:

  • All of the four digits are the same.
  • For each integer i such that 1\leq i\leq 3, X_{i+1} follows X_i. Here, j+1 follows j for each 0\leq j\leq 8, and 0 follows 9.

If the given PIN is weak, print Weak; otherwise, print Strong.

Constraints

  • 0 \leq X_1, X_2, X_3, X_4 \leq 9
  • X_1, X_2, X_3, and X_4 are integers.

Input

Input is given from Standard Input in the following format:

X_1X_2X_3X_4

Output

If the given PIN is weak, print Weak; otherwise, print Strong.


Sample Input 1

7777

Sample Output 1

Weak

All four digits are 7, satisfying the first condition, so this PIN is weak.


Sample Input 2

0112

Sample Output 2

Strong

The first and second digits differ, and the third digit does not follow the second digit, so neither condition is satisfied.


Sample Input 3

9012

Sample Output 3

Weak

Note that 0 follows 9.

C - Min Difference

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 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=34 つです。 この中で最小である 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
D - Querying Multiset

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋君は何も書かれていないたくさんのボールと 1 つの袋を持っています。 最初、袋は空で、高橋君は Q 回の操作を行います。 それぞれの操作は以下の 3 種類のうちのいずれかです。

  • 操作 1 : まだ何も書かれていないボール 1 つに整数 X_i を書き込み、袋に入れる。
  • 操作 2 : 袋に入っているすべてのボールについて、そこに書かれている数を、それに X_i を加えたものに書き換える。
  • 操作 3 : 袋に入っているボールのうち書かれている数が最小のもの(複数ある場合はそのうちの 1 つ)を取り出し、そこに書かれている数を記録する。その後、そのボールを捨てる。

1\leq i\leq Q について i 回目の操作の種類 P_i および操作 1 , 2 における X_i の値が与えられるので、操作 3 において記録された数を順に出力してください。

制約

  • 1 \leq Q \leq 2\times 10^5
  • 1 \leq P_i \leq 3
  • 1 \leq X_i \leq 10^9
  • 入力は全て整数である。
  • P_i=3 であるような i1 つ以上存在する。
  • P_i=3 であるとき、 i 回目の操作の直前の時点で、袋には 1 つ以上のボールが入っている。

入力

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

Q
query_1
query_2
:
query_Q

2 行目から Q+1 行目の各 query_i は次のいずれかの形で与えられる。

1 X_i
2 X_i
3

まず、1\leq P_i\leq 3 が与えられる。これは操作の種類を表す。 P_i=1 または P_i=2 ならば、その後に空白区切りで X_i が与えられる。

出力

Q 回の操作のうち操作の種類が P_i=3 であるような各操作について、記録された数を改行区切りで出力せよ。


入力例 1

5
1 3
1 5
3
2 2
3

出力例 1

3
7

高橋君は次のように操作を行います。

  • 3 の書かれたボールを袋に入れる。
  • 5 の書かれたボールを袋に入れる。
  • 今、袋には 3 の書かれたボールと 5 の書かれたボールが入っているため、このうち小さい 3 の書かれたボールを取り出し、 3 を記録した後に捨てる。
  • 今、袋には 5 の書かれたボールのみが入っているため、この数を 5+2=7 に書き換える。
  • 今、袋には 7 の書かれたボールのみが入っているため、このボールを取り出し、 7 を記録した後に捨てる。

よって、記録された順に 3 , 7 を出力します。


入力例 2

6
1 1000000000
2 1000000000
2 1000000000
2 1000000000
2 1000000000
3

出力例 2

5000000000

答えが 32 bit整数に収まらないことがある事に注意してください。

Score : 400 points

Problem Statement

Takahashi has many balls, on which nothing is written, and one bag. Initially, the bag is empty. Takahashi will do Q operations, each of which is of one of the following three types.

  • Type 1: Write an integer X_i on a blank ball and put it in the bag.
  • Type 2: For each ball in the bag, replace the integer written on it with that integer plus X_i.
  • Type 3: Pick up the ball with the smallest integer in the bag (if there are multiple such balls, pick up one of them). Record the integer written on this ball and throw it away.

For each 1\leq i\leq Q, you are given the type P_i of the i-th operation and the value of X_i if the operation is of Type 1 or 2. Print the integers recorded in the operations of Type 3 in order.

Constraints

  • 1 \leq Q \leq 2\times 10^5
  • 1 \leq P_i \leq 3
  • 1 \leq X_i \leq 10^9
  • All values in input are integers.
  • There is one or more i such that P_i=3.
  • If P_i=3, the bag contains at least one ball just before the i-th operation.

Input

Input is given from Standard Input in the following format:

Q
query_1
query_2
:
query_Q

Each query_i in the 2-nd through (Q+1)-th lines is in the following format:

1 X_i
2 X_i
3

The first number in each line is 1\leq P_i\leq 3, representing the type of the operation. If P_i=1 or P_i=2, it is followed by a space, and then by X_i.

Output

For each operation with P_i=3 among the Q operations, print the recorded integer in its own line.


Sample Input 1

5
1 3
1 5
3
2 2
3

Sample Output 1

3
7

Takahashi will do the following operations.

  • Write 3 on a ball and put it in the bag.
  • Write 5 on a ball and put it in the bag.
  • The bag now contains a ball with 3 and another with 5. Pick up the ball with the smaller of them, or 3. Record 3 and throw it away.
  • The bag now contains just a ball with 5. Replace this integer with 5+2=7.
  • The bag now contains just a ball with 7. Pick up this ball, record 7, and throw it away.

Therefore, we should print 3 and 7, in the order they are recorded.


Sample Input 2

6
1 1000000000
2 1000000000
2 1000000000
2 1000000000
2 1000000000
3

Sample Output 2

5000000000

Note that the outputs may not fit into a 32-bit integer.

E - Safety Journey

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

AtCoder国には N 個の都市があり、都市 1 , 都市 2 , \ldots , 都市 N と番号付けられています。 最初、どの 2 つの相異なる都市の間も双方向に通れる道で結ばれていましたが、老朽化が進み、これらのうち M 本の道が使えなくなってしまいました。具体的には 1\leq i \leq M について都市 U_i と都市 V_i を結ぶ道が使えなくなってしまいました。

いま、高橋君は都市 1 で始まり、都市 1 で終わる K 日間の旅をしようと考えました。都市 1 で始まり、都市 1 で終わる K 日間の旅とは、 K+1 個の都市の列 (A_0, A_1, \ldots, A_K) であって、A_0=A_K=1 をみたし、 0\leq i\leq K-1 について A_iA_{i+1} が相異なり、かつ都市 A_i と都市 A_{i+1} が現在も使用可能な道で結ばれているものを指します。

都市 1 で始まり、都市 1 で終わる K 日間の相異なる旅の数を 998244353 で割った余りを出力してください。ただし、 2 つの K 日間の旅 (A_0, A_1, \ldots, A_K)(B_0, B_1, \ldots, B_K) が相異なるとは、ある i が存在して A_i\neq B_i となることを言います。

制約

  • 2 \leq N \leq 5000
  • 0 \leq M \leq \min\left( \frac{N(N-1)}{2},5000 \right)
  • 2 \leq K \leq 5000
  • 1 \leq U_i<V_i \leq N
  • (U_i, V_i) は全て互いに相異なる。
  • 入力は全て整数である。

入力

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

N M K
U_1 V_1
:
U_M V_M

出力

答えを出力せよ。


入力例 1

3 1 4
2 3

出力例 1

4

次のような 4 種類の旅が存在します。

  • (1,2,1,2,1)
  • (1,2,1,3,1)
  • (1,3,1,2,1)
  • (1,3,1,3,1)

これ以外に条件をみたすようなものは無いため、 4 を出力します。


入力例 2

3 3 3
1 2
1 3
2 3

出力例 2

0

使える道が 1 本も残っておらず、条件をみたすような旅は存在しません。


入力例 3

5 3 100
1 2
4 5
2 3

出力例 3

428417047

Score : 500 points

Problem Statement

The Republic of AtCoder has N cities, called City 1, City 2, \ldots, City N. Initially, there was a bidirectional road between every pair of different cities, but M of these roads have become unusable due to deterioration over time. More specifically, for each 1\leq i \leq M, the road connecting City U_i and City V_i has become unusable.

Takahashi will go for a K-day trip that starts and ends in City 1. Formally speaking, a K-day trip that starts and ends in City 1 is a sequence of K+1 cities (A_0, A_1, \ldots, A_K) such that A_0=A_K=1 holds and for each 0\leq i\leq K-1, A_i and A_{i+1} are different and there is still a usable road connecting City A_i and City A_{i+1}.

Print the number of different K-day trips that start and end in City 1, modulo 998244353. Here, two K-day trips (A_0, A_1, \ldots, A_K) and (B_0, B_1, \ldots, B_K) are said to be different when there exists an i such that A_i\neq B_i.

Constraints

  • 2 \leq N \leq 5000
  • 0 \leq M \leq \min\left( \frac{N(N-1)}{2},5000 \right)
  • 2 \leq K \leq 5000
  • 1 \leq U_i<V_i \leq N
  • All pairs (U_i, V_i) are pairwise distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
U_1 V_1
:
U_M V_M

Output

Print the answer.


Sample Input 1

3 1 4
2 3

Sample Output 1

4

There are four different trips as follows.

  • (1,2,1,2,1)
  • (1,2,1,3,1)
  • (1,3,1,2,1)
  • (1,3,1,3,1)

No other trip is valid, so we should print 4.


Sample Input 2

3 3 3
1 2
1 3
2 3

Sample Output 2

0

No road remains usable, so there is no valid trip.


Sample Input 3

5 3 100
1 2
4 5
2 3

Sample Output 3

428417047
F - Greedy Takahashi

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1 から N までの番号が振られた N 個の都市と、それらの間を運行する M 本のバスがあります。i\ (1 \leq i \leq M) 本目のバスは時刻 S_i+0.5 に都市 A_i を出発し、時刻 T_i+0.5 に都市 B_i に到着します。

さて、これら N 個の都市の間を高橋くんが移動します。高橋くんは、時刻 t に都市 p にいるとき、以下のように行動します。

  1. 時刻 t 以降(時刻 t ちょうどを含む)に都市 p を出発するバスが存在するなら、そのようなバスのうち最も出発時刻が早いものに乗り、他の都市に移動する。
  2. そのようなバスが存在しないなら、何もせず都市 p に居続ける。

高橋くんは上記の行動を 2. の状態になるまで繰り返します。M 本のバスの出発時刻は互いに異なることが保証されるため、高橋くんが乗るバスは常に一意に定まります。また、バスの乗り換えにかかる時間は無視することができます。

それでは本題に入りましょう。Q 個のクエリに答えてください。i\ (1 \leq i \leq Q) 個目のクエリの内容は以下の通りです。

  • 高橋くんが時刻 X_i に都市 Y_i から行動を開始するとき、時刻 Z_i にはどの都市にいるか、あるいはどのバスに乗っているか。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq A_i,B_i \leq N\ (1 \leq i \leq M)
  • A_i \neq B_i\ (1 \leq i \leq M)
  • 1 \leq S_i \lt T_i \leq 10^9\ (1 \leq i \leq M)
  • S_i \neq S_j\ (i \neq j)
  • 1 \leq X_i \lt Z_i \leq 10^9\ (1 \leq i \leq Q)
  • 1 \leq Y_i \leq N\ (1 \leq i \leq Q)
  • 入力は全て整数

入力

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

N M Q
A_1 B_1 S_1 T_1
A_2 B_2 S_2 T_2
\hspace{1.8cm}\vdots
A_M B_M S_M T_M
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\hspace{1.2cm}\vdots
X_Q Y_Q Z_Q

出力

Q 行に渡って出力せよ。i 行目には、 i 個目のクエリに対する答えを以下の指示にしたがって出力すること。

  • 高橋くんが時刻 Z_i にいずれかのバスに乗っているならば、そのバスの始点、終点となる都市の番号をこの順に空白区切りで出力する。
  • そうでない、すなわち高橋くんが時刻 Z_i にいずれかの都市にいるならば、その都市の番号を出力する。

入力例 1

3 2 3
1 2 1 3
2 3 3 5
1 1 5
2 2 3
1 3 2

出力例 1

2 3
2
3

1 つ目のクエリにおいて、高橋くんは以下の通りに動きます。

  1. はじめ、都市 1 に時刻 1 にいる。
  2. 時刻 1.5 に都市 1 を出発するバスに乗り、時刻 3.5 に都市 2 に到着する。
  3. 時刻 3.5 に都市 2 を出発するバスに乗り、時刻 5.5 に都市 3 に到着する。
  4. 時刻 5.5 以降に都市 3 を出発するバスは存在しないため、都市 3 に(永遠に)居続ける。

時刻 5 において、高橋くんは都市 2 を出発して都市 3 に到着するバスに乗っています。そのため、「出力」の項に書かれている通り、2, 3 を空白区切りで出力します。


入力例 2

8 10 10
4 3 329982133 872113932
6 8 101082040 756263297
4 7 515073851 793074419
8 7 899017043 941751547
5 7 295510441 597348810
7 2 688716395 890599546
6 1 414221915 748470452
6 4 810915860 904512496
3 1 497469654 973509612
4 1 307142272 872178157
374358788 4 509276232
243448834 6 585993193
156350864 4 682491610
131643541 8 836902943
152874385 6 495945159
382276121 1 481368090
552433623 2 884584430
580376205 2 639442239
108790644 7 879874292
883275610 1 994982498

出力例 2

4
6 1
4 1
8
6 1
1
2
2
7 2
1

Score : 500 points

Problem Statement

There are N cities numbered 1 through N, and M buses that go between these cities. The i-th bus (1 \leq i \leq M) departs from City A_i at time S_i+0.5 and arrive at City B_i at time T_i+0.5.

Takahashi will travel between these N cities. When he is in City p at time t, he will do the following.

  1. If there is a bus that departs from City p not earlier than time t, take the bus that departs the earliest among those buses to get to another city.
  2. If there is no such bus, do nothing and stay in City p.

Takahashi repeats doing the above until there is no bus to take. It is guaranteed that all M buses depart at different times, so the bus to take is always uniquely determined. Additionally, the time needed to change buses is negligible.

Here is your task: process Q queries, the i-th (1 \leq i \leq Q) of which is as follows.

  • If Takahashi begins his travel in City Y_i at time X_i, in which city or on which bus will he be at time Z_i?

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq A_i,B_i \leq N\ (1 \leq i \leq M)
  • A_i \neq B_i\ (1 \leq i \leq M)
  • 1 \leq S_i \lt T_i \leq 10^9\ (1 \leq i \leq M)
  • S_i \neq S_j\ (i \neq j)
  • 1 \leq X_i \lt Z_i \leq 10^9\ (1 \leq i \leq Q)
  • 1 \leq Y_i \leq N\ (1 \leq i \leq Q)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M Q
A_1 B_1 S_1 T_1
A_2 B_2 S_2 T_2
\hspace{1.8cm}\vdots
A_M B_M S_M T_M
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\hspace{1.2cm}\vdots
X_Q Y_Q Z_Q

Output

Print Q lines. The i-th line should contain the response to the i-th query as follows.

  • If Takashi is on some bus at time Z_i, print two integers representing the city from which the bus departs and the city at which the bus arrives, in this order, with a space between them.
  • Otherwise, that is, if Takahashi is in some city at time Z_i, print an integer representing that city.

Sample Input 1

3 2 3
1 2 1 3
2 3 3 5
1 1 5
2 2 3
1 3 2

Sample Output 1

2 3
2
3

In the first query, Takahashi will travel as follows.

  1. Start in City 1 at time 1.
  2. Take the bus that departs from City 1 at time 1.5 and arrives at City 2 at time 3.5.
  3. Take the bus that departs from City 2 at time 3.5 and arrives at City 3 at time 5.5.
  4. Since no bus departs from City 3 at time 5.5 or later, stay in City 3 (forever).

At time 5, he will be on the bus that departs from City 2 and arrives at City 3. Thus, as specified in the Output section, we should print 2 and 3 with a space between them.


Sample Input 2

8 10 10
4 3 329982133 872113932
6 8 101082040 756263297
4 7 515073851 793074419
8 7 899017043 941751547
5 7 295510441 597348810
7 2 688716395 890599546
6 1 414221915 748470452
6 4 810915860 904512496
3 1 497469654 973509612
4 1 307142272 872178157
374358788 4 509276232
243448834 6 585993193
156350864 4 682491610
131643541 8 836902943
152874385 6 495945159
382276121 1 481368090
552433623 2 884584430
580376205 2 639442239
108790644 7 879874292
883275610 1 994982498

Sample Output 2

4
6 1
4 1
8
6 1
1
2
2
7 2
1
G - Power Pair

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

素数 P が与えられます。

以下の条件を満たす整数の組 (x, y) はいくつありますか?

  • 0 \leq x \leq P-1
  • 0 \leq y \leq P-1
  • ある正整数 n が存在して、x^n \equiv y \pmod{P} を満たす

ただし答えは非常に大きくなる可能性があるので、998244353 で割った余りを出力してください。

制約

  • 2 \leq P \leq 10^{12}
  • P は素数

入力

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

P

出力

答えを 998244353 で割った余りを出力せよ。


入力例 1

3

出力例 1

4

(x, y) = (0, 0), (1, 1), (2, 1), (2, 2)4 組が条件を満たします。


入力例 2

11

出力例 2

64

入力例 3

998244353

出力例 3

329133417

Score : 600 points

Problem Statement

Given is a prime number P.

How many pairs of integers (x, y) satisfy the following conditions?

  • 0 \leq x \leq P-1
  • 0 \leq y \leq P-1
  • There exists a positive integer n such that x^n \equiv y \pmod{P}.

Since the answer may be enormous, print it modulo 998244353.

Constraints

  • 2 \leq P \leq 10^{12}
  • P is a prime number.

Input

Input is given from Standard Input in the following format:

P

Output

Print the answer modulo 998244353.


Sample Input 1

3

Sample Output 1

4

Four pairs (x, y) = (0, 0), (1, 1), (2, 1), (2, 2) satisfy the conditions.


Sample Input 2

11

Sample Output 2

64

Sample Input 3

998244353

Sample Output 3

329133417
H - Nim Counting

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 600 points

Problem Statement

Given are positive integers N, K, and a sequence of K integers (A_1, A_2, \ldots, A_K).

Takahashi and Aoki will play a game with stones. Initially, there are some heaps of stones, each of which contains one or more stones. The players take turns doing the following operation, with Takahashi going first.

  • Choose a heap with one or more stones remaining. Remove any number of stones between 1 and X (inclusive) from that heap, where X is the number of stones remaining.

The player who first gets unable to do the operation loses.

Now, consider the initial arrangements of stones satisfying the following.

  • 1\leq M\leq N holds, where M is the number of heaps of stones.
  • The number of stones in each heap is one of the following: A_1, A_2, \ldots, A_K.

Assuming that the heaps are ordered, there are K+K^2+\cdots +K^N such initial arrangements of stones. Among them, find the number, modulo 998244353, of arrangements that lead to Takahashi's win, assuming that both players play optimally.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq K < 2^{16}
  • 1 \leq A_i < 2^{16}
  • All A_i are distinct.
  • All values in input are integers.

Input

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

2 2
1 2

Sample Output 1

4

There are six possible initial arrangements of stones: (1), (2), (1,1), (1,2), (2,1), and (2,2).
Takahashi has a winning strategy for four of them: (1), (2), (1,2), and (2,1), and Aoki has a winning strategy for the other two. Thus, we should print 4.


Sample Input 2

100 3
3 5 7

Sample Output 2

112184936

Be sure to find the count modulo 998244353.

配点 : 600

問題文

正の整数 N , K と長さ K の整数列 (A_1, A_2, \ldots, A_K) が与えられます。

高橋君と青木君が石取りゲームをすることを考えます。最初、山がいくつかあり、それぞれの山には 1 個以上の石があります。 高橋君から始めて 2 人は交互に以下の操作を繰り返します。

  • 石が 1 つ以上残っているような山を 1 つ選ぶ。 選んだ山に X 個の石があるとき、 1 個以上 X 個以下の任意の個数の石をその山から取り除く。

先に操作ができなくなったプレイヤーが負けとなります。

さて、ゲームを始める前の初期状態として次のようなものを考えます。

  • 山の個数を M としたとき、 1\leq M\leq N をみたす。
  • 各山にある石の個数は A_1, A_2, \ldots, A_K のいずれかである。

山の順番を並び替えて一致するものも区別するとしたとき、これをみたす初期状態として考えられるものは K+K^2+\cdots +K^N 通りあります。このうち、 2 人が自分が勝つために最適な行動をしたとき、高橋君が勝てるような初期状態の個数を 998244353 で割ったあまりを求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq K < 2^{16}
  • 1 \leq A_i < 2^{16}
  • A_i は全て相異なる。
  • 入力は全て整数である。

入力

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

N K
A_1 A_2 \ldots A_K

出力

答えを出力せよ。


入力例 1

2 2
1 2

出力例 1

4

最初の状態における各山の石の個数として考えられるのは (1) , (2) , (1,1) , (1,2) , (2,1) , (2,2)6 通りです。
これらのうち、 (1) , (2) , (1,2) , (2,1)4 通りについては高橋君に必勝法が存在し、残りの 2 通りについては青木君に必勝法が存在します。よって、 4 を出力します。


入力例 2

100 3
3 5 7

出力例 2

112184936

998244353 で割った余りを求めることに注意してください。