A - 106

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

整数 N が与えられます。 3^A + 5^B = N を満たす正の整数の組 (A, B) が存在するか判定し、存在する場合は 1 組求めてください。

制約

  • 1 \leq N \leq 10^{18}
  • 入力はすべて整数である。

入力

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

N

出力

条件を満たす組 (A, B) が存在しない場合は -1 と出力せよ。

存在する場合は AB を空白区切りで出力せよ。答えが複数存在する場合はどれを出力してもかまわない。


入力例 1

106

出力例 1

4 2

3^4 + 5^2 = 81 + 25 = 106 なので、(A, B) = (4, 2) は条件を満たします。


入力例 2

1024

出力例 2

-1

入力例 3

10460353208

出力例 3

21 1

Score : 300 points

Problem Statement

Given is an integer N. Determine whether there is a pair of positive integers (A, B) such that 3^A + 5^B = N, and find one such pair if it exists.

Constraints

  • 1 \leq N \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

If there is no pair (A, B) that satisfies the condition, print -1.

If there is such a pair, print A and B of one such pair with space in between. If there are multiple such pairs, any of them will be accepted.


Sample Input 1

106

Sample Output 1

4 2

We have 3^4 + 5^2 = 81 + 25 = 106, so (A, B) = (4, 2) satisfies the condition.


Sample Input 2

1024

Sample Output 2

-1

Sample Input 3

10460353208

Sample Output 3

21 1
B - Values

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

N 頂点 M 辺の単純無向グラフが与えられます。 i 番目の辺は頂点 c_i と頂点 d_i を結んでいます。

はじめ、頂点 i には値 a_i が書かれています。あなたは次の操作を 0 回以上行うことで、操作後の各頂点の値をそれぞれ b_1,\cdots,b_N にしたいと思っています。

  • 辺を 1 つ選ぶ。選んだ辺が頂点 x と頂点 y を結んでいるとしたとき、次のいずれかを選んで行う。
    • a_x-1 し、値 a_y+1 する
    • a_x+1 し、値 a_y-1 する

適切に操作を行うことで目的を達成することが可能かどうかを判定してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • -10^9 \leq a_i,b_i \leq 10^9
  • 1 \leq c_i,d_i \leq N
  • 与えられるグラフは単純グラフである。すなわち、自己ループや多重辺は存在しない。
  • 入力はすべて整数である。

入力

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

N M
a_1 \cdots a_N
b_1 \cdots b_N
c_1 d_1
\vdots
c_M d_M

出力

適切に操作を行うことで目的を達成することが可能な場合は Yes 、可能でない場合は No と出力せよ。


入力例 1

3 2
1 2 3
2 2 2
1 2
2 3

出力例 1

Yes

例えば、以下のように操作を行うことで、目的を達成できます。

  • 1 回目の操作で頂点 12 を結ぶ辺を選び、a_1+1 し、 a_2-1 します。
  • 2 回目の操作で頂点 23 を結ぶ辺を選び、a_2+1 し、 a_3-1 します。

以上の操作により、a_1=2 かつ a_2=2 かつ a_3=2 となります。


入力例 2

1 0
5
5

出力例 2

Yes

はじめから目的が達成されていることもあります。


入力例 3

2 1
1 1
2 1
1 2

出力例 3

No

どのように操作を行っても目的を達成できません。


入力例 4

17 9
-905371741 -999219903 969314057 -989982132 -87720225 -175700172 -993990465 929461728 895449935 -999016241 782467448 -906404298 578539175 9684413 -619191091 -952046546 125053320
-440503430 -997661446 -912471383 -995879434 932992245 -928388880 -616761933 929461728 210953513 -994677396 648190629 -530944122 578539175 9684413 595786809 -952046546 125053320
2 10
6 12
9 11
11 5
7 6
3 15
3 1
1 9
10 4

出力例 4

Yes

Score : 400 points

Problem Statement

Given is a simple undirected graph with N vertices and M edges. The i-th edge connects Vertex c_i and Vertex d_i. Initially, Vertex i has the value a_i written on it. You want to change the values on Vertex 1, \ldots, Vertex N to b_1, \cdots, b_N, respectively, by doing the operation below zero or more times.

  • Choose an edge, and let Vertex x and Vertex y be the vertices connected by that edge. Choose one of the following and do it:
    • Decrease the value a_x by 1, and increase the value a_y by 1.
    • Increase the value a_x by 1, and decrease the value a_y by 1.

Determine whether it is possible to achieve the objective by properly doing the operation.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • -10^9 \leq a_i,b_i \leq 10^9
  • 1 \leq c_i,d_i \leq N
  • The given graph is simple, that is, has no self-loops and no multi-edges.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 \cdots a_N
b_1 \cdots b_N
c_1 d_1
\vdots
c_M d_M

Output

Print Yes if it is possible to achieve the objective by properly doing the operation, and No otherwise.


Sample Input 1

3 2
1 2 3
2 2 2
1 2
2 3

Sample Output 1

Yes

You can achieve the objective by, for example, doing the operation as follows:

  • In the first operation, choose the edge connecting Vertex 1 and 2. Then, increase a_1 by 1 and decrease a_2 by 1.
  • In the second operation, choose the edge connecting Vertex 2 and 3. Then, increase a_2 by 1 and decrease a_3 by 1.

This sequence of operations makes a_1=2, a_2=2, and a_3=2.


Sample Input 2

1 0
5
5

Sample Output 2

Yes

The objective may be achieved already in the beginning.


Sample Input 3

2 1
1 1
2 1
1 2

Sample Output 3

No

There is no way to do the operation to achieve the objective.


Sample Input 4

17 9
-905371741 -999219903 969314057 -989982132 -87720225 -175700172 -993990465 929461728 895449935 -999016241 782467448 -906404298 578539175 9684413 -619191091 -952046546 125053320
-440503430 -997661446 -912471383 -995879434 932992245 -928388880 -616761933 929461728 210953513 -994677396 648190629 -530944122 578539175 9684413 595786809 -952046546 125053320
2 10
6 12
9 11
11 5
7 6
3 15
3 1
1 9
10 4

Sample Output 4

Yes
C - Solutions

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

2 つの区間 [L_1:R_1], [L_2:R_2] について, L_1 \leq R_2 かつ L_2 \leq R_1 であるとき, この 2 つの区間が交わると定義します。

以下の問題 P を考えます。

入力: N 個の区間 [L_1: R_1], \cdots, [L_N:R_N]
      L_1, L_2, \cdots, L_N, R_1, R_2, \cdots, R_Nは全て異なる。
出力: どの 2 つの区間も交わらないように選べる区間の最大数

高橋君は、以下のように動作するプログラムを実装しました。

R_i の値が昇順となるように, 入力された区間を [L_{p_1}:R_{p_1}], [L_{p_2}:R_{p_2}], \cdots , [L_{p_N}:R_{p_N}] と並び替える。
i = 1, 2, \cdots , N について、以下を行う。
  これまでに選んだどの区間とも交わらないならば、 [L_{p_i}:R_{p_i}] を選ぶ。
選んだ区間の数を出力する。

一方、青木君は、以下のように動作するプログラムを実装しました。

L_i の値が昇順となるように, 入力された区間を [L_{p_1}:R_{p_1}], [L_{p_2}:R_{p_2}], \cdots , [L_{p_N}:R_{p_N}] と並び替える。
i = 1, 2, \cdots , N について、以下を行う。
  これまでに選んだどの区間とも交わらないならば、 [L_{p_i}:R_{p_i}] を選ぶ。
選んだ区間の数を出力する。

整数 N, Mが与えられます。 N 個の区間から成る問題 P の入力であって、

$$ (高橋君のプログラムが出力する値) - (青木君のプログラムが出力する値) = M $$

となるものを構築してください。

制約

  • 入力は全て整数
  • 1 \leq N \leq 2 \times 10^5
  • -N \leq M \leq N

入力

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

N M

出力

条件を満たす N 個の区間が存在しない場合は -1 と出力せよ。

存在する場合は、 以下のように N 行出力せよ。

L_1 R_1
L_2 R_2
  \vdots
L_N R_N

ただし, [L_1:R_1], \cdots, [L_N:R_N]は以下の条件を満たさなければならない。

  • 1 \leq L_i < R_i \leq 10^9
  • L_i \neq L_j (i \neq j)
  • R_i \neq R_j (i \neq j)
  • L_i \neq R_j
  • L_1, \cdots , L_N , R_1, \cdots , R_N は整数 (21:46 追記)

答えが複数存在する場合は、どれを出力しても構わない。

出力の最後には必ず改行を行うこと。


入力例 1

5 1

出力例 1

1 10
8 12
13 20
11 14
2 4

5 つの区間を順に区間 1 、区間 2\cdots 、区間 5 と名付けます。

高橋君のプログラムは以下のように動作します。

区間を区間 5 、区間 1 、区間 2 、区間 4 、区間 3 と並び替えます。
区間 5 を選びます。
区間 1 は選びません。(区間 5 と交わっている為)
区間 2 を選びます。
区間 4 は選びません。 (区間 2 と交わっている為)
区間 3 を選びます。

以上より、高橋君のプログラムが出力する値は 3 となります。

青木君のプログラムは以下のように動作します。

区間を区間 1 、区間 5 、区間 2 、区間 4 、区間 3 と並び替えます。
区間 1 を選びます。
区間 5 は選びません。(区間 1 と交わっている為)
区間 2 は選びません。 (区間 1 と交わっている為)
区間 4 を選びます。
区間 3 は選びません。 (区間 4 と交わっている為)

以上より、青木君のプログラムが出力する値は 2 となります。

このとき、 3 - 2 = 1 \left(= M \right) であり、この 5 つの区間は条件を満たします。


入力例 2

10 -10

出力例 2

-1

Score : 500 points

Problem Statement

Two segments [L_1:R_1] and [L_2:R_2] are said to intersect when L_1 \leq R_2 and L_2 \leq R_1 holds.

Consider the following problem P:

Input: N segments [L_1: R_1], \cdots, [L_N:R_N]
       L_1, L_2, \cdots, L_N, R_1, R_2, \cdots, R_N are pairwise distinct.
Output: the maximum number of segments that can be chosen so that no two of them intersect.

Takahashi has implemented a program that works as follows:

Sort the given segments in the increasing order of R_i and let the result be [L_{p_1}:R_{p_1}], [L_{p_2}:R_{p_2}], \cdots , [L_{p_N}:R_{p_N}].
For each i = 1, 2, \cdots , N, do the following:
  Choose [L_{p_i}:R_{p_i}] if it intersects with none of the segments chosen so far.
Print the number of chosen segments.

Aoki, on the other hand, has implemented a program that works as follows:

Sort the given segments in the increasing order of L_i and let the result be [L_{p_1}:R_{p_1}], [L_{p_2}:R_{p_2}], \cdots , [L_{p_N}:R_{p_N}].
For each i = 1, 2, \cdots , N, do the following:
  Choose [L_{p_i}:R_{p_i}] if it intersects with none of the segments chosen so far.
Print the number of chosen segments.

Given are integers N and M. Construct an input for the problem P, consisting of N segments, such that the following holds:

$$ (\text{The value printed by Takahashi's program}) - (\text{The value printed by Aoki's program}) = M $$

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 2 \times 10^5
  • -N \leq M \leq N

Input

Input is given from Standard Input in the following format:

N M

Output

If there is no set of N segments that satisfies the condition, print -1.

Otherwise, print N lines as follows:

L_1 R_1
L_2 R_2
  \vdots
L_N R_N

Here, [L_1:R_1], \cdots, [L_N:R_N] must satisfy the following:

  • 1 \leq L_i < R_i \leq 10^9
  • L_i \neq L_j (i \neq j)
  • R_i \neq R_j (i \neq j)
  • L_i \neq R_j
  • L_1, \cdots , L_N , R_1, \cdots , R_N are integers (21:46 added)

If there are multiple answers, any of them will be accepted.

Be sure to print a newline at the end of the output.


Sample Input 1

5 1

Sample Output 1

1 10
8 12
13 20
11 14
2 4

Let us call the five segments Segment 1, Segment 2, \cdots, Segment 5.

Takahashi's program will work as follows:

Rearrange the segments into the order Segment 5, Segment 1, Segment 2, Segment 4, Segment 3.
Choose Segment 5.
Skip Segment 1 (because it intersects with Segment 5).
Choose Segment 2.
Skip Segment 4 (because it intersects with Segment 2).
Choose Segment 3.

Thus, Takahashi's program will print 3.

Aoki's program will work as follows:

Rearrange the segments into the order Segment 1, Segment 5, Segment 2, Segment 4, Segment 3.
Choose Segment 1.
Skip Segment 5 (because it intersects with Segment 1).
Skip Segment 2 (because it intersects with Segment 1).
Choose Segment 4.
Skip Segment 3 (because it intersects with Segment 4).

Thus, Aoki's program will print 2.

Here, we have 3 - 2 = 1 \left(= M \right), and thus these five segments satisfy the condition.


Sample Input 2

10 -10

Sample Output 2

-1
D - Powers

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

長さ N の整数列 A = (A_1, A_2, \cdots, A_N) と整数 K が与えられます。

1 \le X \le K を満たす整数 X それぞれについて、以下の値を求めてください。

\left(\displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} (A_L+A_R)^X\right) \bmod 998244353

制約

  • 入力は全て整数
  • 2 \le N \le 2 \times 10^5
  • 1 \le K \le 300
  • 1 \le A_i \le 10^8

入力

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

N K
A_1 A_2 \cdots A_N

出力

K 行出力せよ。

X 行目には、\left(\displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} (A_L+A_R)^X \right) \bmod 998244353 の値を出力せよ。


入力例 1

3 3
1 2 3

出力例 1

12
50
216

1 行目には、(1+2)^1 + (1+3)^1 + (2+3)^1 = 3 + 4 + 5 = 12 を出力します。

2 行目には、(1+2)^2 + (1+3)^2 + (2+3)^2 = 9 + 16 + 25 = 50 を出力します。

3 行目には、(1+2)^3 + (1+3)^3 + (2+3)^3 = 27 + 64 + 125 = 216 を出力します。


入力例 2

10 10
1 1 1 1 1 1 1 1 1 1

出力例 2

90
180
360
720
1440
2880
5760
11520
23040
46080

入力例 3

2 5
1234 5678

出力例 3

6912
47775744
805306038
64822328
838460992

\bmod 998244353 での値を出力してください。

Score : 600 points

Problem Statement

Given are integer sequence of length N, A = (A_1, A_2, \cdots, A_N), and an integer K.

For each X such that 1 \le X \le K, find the following value:

\left(\displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} (A_L+A_R)^X\right) \bmod 998244353

Constraints

  • All values in input are integers.
  • 2 \le N \le 2 \times 10^5
  • 1 \le K \le 300
  • 1 \le A_i \le 10^8

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_N

Output

Print K lines.

The X-th line should contain the value \left(\displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} (A_L+A_R)^X \right) \bmod 998244353.


Sample Input 1

3 3
1 2 3

Sample Output 1

12
50
216

In the 1-st line, we should print (1+2)^1 + (1+3)^1 + (2+3)^1 = 3 + 4 + 5 = 12.

In the 2-nd line, we should print (1+2)^2 + (1+3)^2 + (2+3)^2 = 9 + 16 + 25 = 50.

In the 3-rd line, we should print (1+2)^3 + (1+3)^3 + (2+3)^3 = 27 + 64 + 125 = 216.


Sample Input 2

10 10
1 1 1 1 1 1 1 1 1 1

Sample Output 2

90
180
360
720
1440
2880
5760
11520
23040
46080

Sample Input 3

2 5
1234 5678

Sample Output 3

6912
47775744
805306038
64822328
838460992

Be sure to print the sum modulo 998244353.

E - Medals

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 700

問題文

あなたは N 人の従業員を持つ店の店長です。 各従業員は一定の周期で出勤します。 より正確には、i 番目の従業員は今日から「A_i 日連続で働いた後 A_i 日連続で休む」ことを繰り返します。

あなたは今日から毎日出勤し、その日に出勤している従業員の中から 1 人選び、メダルを 1 枚配ります。(その日に出勤している従業員が 1 人もいなければ何もしません)

全ての従業員に K 枚以上のメダルを配るためには、最短で何日かかるでしょうか。

制約

  • 入力は全て整数
  • 1 \le N \le 18
  • 1 \le K \le 10^5
  • 1 \le A_i \le 10^5

入力

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

N K
A_1 A_2 \cdots A_N

出力

答えを出力せよ。


入力例 1

3 3
1 2 3

出力例 1

10

例えば、以下のようにメダルを配ることができます。

  • 1 番目の従業員には、1, 5, 9 日目にメダルを配る
  • 2 番目の従業員には、2, 6, 10 日目にメダルを配る
  • 3 番目の従業員には、3, 7, 8 日目にメダルを配る

4 日目には出勤している従業員が 1 人もいないため、これは最短となる配り方の 1 つです。


入力例 2

10 10
1 1 1 1 1 1 1 1 1 1

出力例 2

199

入力例 3

2 5
1234 5678

出力例 3

10

Score : 700 points

Problem Statement

You run a shop employing N employees. Each employee comes to work in a fixed cycle. More specifically, starting today, the i-th employee repeats the following: working for A_i consecutive days and then taking A_i consecutive days off.

Every day starting today, you will come to work and give a medal to an employee of your choice who comes to work that day. (If no employee comes to work, you will do nothing that day.)

At least how many days does it takes to give every employee K or more medals?

Constraints

  • All values in input are integers.
  • 1 \le N \le 18
  • 1 \le K \le 10^5
  • 1 \le A_i \le 10^5

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_N

Output

Print the answer.


Sample Input 1

3 3
1 2 3

Sample Output 1

10

For example, you can choose to give them medals as follows:

  • Give a medal to the 1-st employee on the 1-st, 5-th, and 9-th days.
  • Give a medal to the 2-nd employee on the 2-nd, 6-th, and 10-th days.
  • Give a medal to the 3-rd employee on the 3-rd, 7-th, and 8-th days.

Since no employee comes to work on the 4-th day, this is one of the ways to achieve the objective in the minimum number of days.


Sample Input 2

10 10
1 1 1 1 1 1 1 1 1 1

Sample Output 2

199

Sample Input 3

2 5
1234 5678

Sample Output 3

10
F - Figures

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

高橋君はフィギュアを組み立てようとしています。このフィギュアは、 N 個の部品(部品 1 , 部品 2 , ..., 部品 N )と、 N-1 個の接続用部品から成ります。部品同士は区別が出来ますが、接続用部品同士は区別が出来ません。

部品 i には、d_i 個の接続用部品を挿す穴(穴 1 , 穴 2 , ..., 穴 d_i )が空いています。各部品の穴同士は区別が出来ます。 各接続用部品は、 2 個の部品の穴に挿し込まれ、それら 2 個の部品を接続します。 1 つの穴に複数の接続用部品を挿し込むことは出来ません。

以下の性質を満たすフィギュアのことを、完成形と呼びます。

  • N-1 個の接続用部品が全て部品の接続に使われている。
  • 部品を頂点とし、 接続用部品で接続された部品に対応する頂点組に辺が存在する N 頂点 N-1 辺の無向グラフを考えた際に、このグラフは連結である。

2 つの完成形について、全ての穴の組についてその 2 つを接続する接続用部品が存在するか否かが一致するとき、2 つの完成形が同じであると見なします。

完成形が何種類あるかを答えてください。 ただし、答えは非常に大きくなることがあるので、 998244353 で割った余りを出力してください。

制約

  • 入力は全て整数
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq d_i < 998244353

入力

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

N
d_1 d_2 \cdots d_N

出力

答えを出力せよ。


入力例 1

3
1 1 3

出力例 1

6

例えば、部品 1 の穴 1 と部品 3 の穴 3 を接続し、部品 2 の穴 1 と部品 3 の穴 1 を接続したフィギュアは、完成形として認められます。


入力例 2

3
1 1 1

出力例 2

0

入力例 3

6
7 3 5 10 6 4

出力例 3

389183858

入力例 4

9
425656 453453 4320 1231 9582 54336 31435436 14342 423543

出力例 4

667877982

Score : 800 points

Problem Statement

Takahashi is about to assemble a character figure, consisting of N parts called Part 1, Part 2, ..., Part N and N-1 connecting components. Parts are distinguishable, but connecting components are not.

Part i has d_i holes, called Hole 1, Hole 2, ..., Hole d_i, into which a connecting component can be inserted. These holes in the parts are distinguishable. Each connecting component will be inserted into two holes in different parts, connecting these two parts. It is impossible to insert multiple connecting components into a hole.

The character figure is said to be complete when it has the following properties:

  • All of the N-1 components are used to connect parts.
  • Consider a graph with N vertices corresponding to the parts and N-1 undirected edges corresponding to the pairs of vertices connected by a connecting component. Then, this graph is connected.

Two ways A and B to make the figure complete are considered the same when the following is satisfied: for every pair of holes, A uses a connecting component to connect these holes if and only if B uses one to connect them.

Find the number of ways to make the figure complete. Since the answer can be enormous, find the count modulo 998244353.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq d_i < 998244353

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 \cdots d_N

Output

Print the answer.


Sample Input 1

3
1 1 3

Sample Output 1

6

One way to make the figure complete is to connect Hole 1 in Part 1 and Hole 3 in Part 3 and then connect Hole 1 in Part 2 and Hole 1 in Part 3.


Sample Input 2

3
1 1 1

Sample Output 2

0

Sample Input 3

6
7 3 5 10 6 4

Sample Output 3

389183858

Sample Input 4

9
425656 453453 4320 1231 9582 54336 31435436 14342 423543

Sample Output 4

667877982