A - Arithmetic Progression

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

初項が A、末項が B、公差が D であるような等差数列を出力してください。

なお、そのような等差数列が存在する入力のみが与えられます。

制約

  • 1 \leq A \leq B \leq 100
  • 1\leq D \leq 100
  • 初項が A、末項が B、公差が D であるような等差数列が存在する
  • 入力は全て整数

入力

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

A B D

出力

初項が A、末項が B、公差が D であるような等差数列の項を順に空白区切りで出力せよ。


入力例 1

3 9 2

出力例 1

3 5 7 9

初項が 3、末項が 9、公差が 2 であるような等差数列は (3,5,7,9) です。


入力例 2

10 10 1

出力例 2

10

初項が 10、末項が 10、公差が 1 であるような等差数列は (10) です。

Score: 100 points

Problem Statement

Print an arithmetic sequence with first term A, last term B, and common difference D.

You are only given inputs for which such an arithmetic sequence exists.

Constraints

  • 1 \leq A \leq B \leq 100
  • 1 \leq D \leq 100
  • There is an arithmetic sequence with first term A, last term B, and common difference D.
  • All input values are integers.

Input

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

A B D

Output

Print the terms of the arithmetic sequence with first term A, last term B, and common difference D, in order, separated by spaces.


Sample Input 1

3 9 2

Sample Output 1

3 5 7 9

The arithmetic sequence with first term 3, last term 9, and common difference 2 is (3,5,7,9).


Sample Input 2

10 10 1

Sample Output 2

10

The arithmetic sequence with first term 10, last term 10, and common difference 1 is (10).

B - Append

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

空の数列 A があります。クエリが Q 個与えられるので、与えられた順に処理してください。
クエリは次の 2 種類のいずれかです。

  • 1 x: A の末尾に x を追加する。
  • 2 k: A の後ろから k 番目の値を求める。このクエリが与えられるとき、A の長さは k 以上であることが保証される。

制約

  • 1 \leq Q \leq 100
  • 1 種類目のクエリにおいて x1 \leq x \leq 10^9 を満たす整数
  • 2 種類目のクエリにおいて k はその時点の数列 A の長さ以下の正の整数

入力

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

Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

クエリは以下の 2 つのいずれかの形式である。

1 x
2 k

出力

2 種類目のクエリの個数を q として q 行出力せよ。
i 行目にはそのような i 回目のクエリに対する答えを出力せよ。


入力例 1

5
1 20
1 30
2 1
1 40
2 3

出力例 1

30
20
  • 最初 A は空である。
  • 1 番目のクエリにより A の末尾に 20 が追加され A=(20) となる。
  • 2 番目のクエリにより A の末尾に 30 が追加され A=(20,30) となる。
  • 3 番目のクエリの答えは A=(20,30) の後ろから 1 番目の値の 30 である。
  • 4 番目のクエリにより A の末尾に 40 が追加され A=(20,30,40) となる。
  • 5 番目のクエリの答えは A=(20,30,40) の後ろから 3 番目の値の 20 である。

Score: 200 points

Problem Statement

You have an empty sequence A. There are Q queries given, and you need to process them in the order they are given.
The queries are of the following two types:

  • 1 x: Append x to the end of A.
  • 2 k: Find the k-th value from the end of A. It is guaranteed that the length of A is at least k when this query is given.

Constraints

  • 1 \leq Q \leq 100
  • In the first type of query, x is an integer satisfying 1 \leq x \leq 10^9.
  • In the second type of query, k is a positive integer not greater than the current length of sequence A.

Input

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

Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is in one of the following two formats:

1 x
2 k

Output

Print q lines, where q is the number of queries of the second type.
The i-th line should contain the answer to the i-th such query.


Sample Input 1

5
1 20
1 30
2 1
1 40
2 3

Sample Output 1

30
20
  • Initially, A is empty.
  • The first query appends 20 to the end of A, making A=(20).
  • The second query appends 30 to the end of A, making A=(20,30).
  • The answer to the third query is 30, which is the 1-st value from the end of A=(20,30).
  • The fourth query appends 40 to the end of A, making A=(20,30,40).
  • The answer to the fifth query is 20, which is the 3-rd value from the end of A=(20,30,40).
C - Divide and Divide

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

黒板に整数 N1 個書かれています。
高橋君は黒板に書かれている 2 以上の整数が全て無くなるまで以下の一連の操作を繰り返します。

  • 黒板に書かれている 2 以上の整数を 1 つ選び x とする。
  • 黒板から x1 個消す。そして、2 個の整数 \left \lfloor \dfrac{x}{2} \right\rfloor, \left\lceil \dfrac{x}{2} \right\rceil を新たに黒板に書く。
  • この一連の操作を行うために高橋君は x 円払う必要がある。

ここで \lfloor a \rfloora 以下の整数のうち最大のものを、\lceil a \rceila 以上の整数のうち最小のものを意味します。

操作を行えなくなった時点で高橋君が払った金額の総和は何円ですか?
なお、どのような順番で操作を行っても高橋君が払う金額の総和は一定であることが証明できます。

制約

  • 2 \leq N \leq 10^{17}

入力

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

N

出力

高橋君が払った金額の総和が何円であるかを出力せよ。


入力例 1

3

出力例 1

5

高橋君が行う操作の一例を挙げると次のようになります。

  • はじめ、黒板には 31 個書かれている。
  • 高橋君は 3 を選ぶ。高橋君は 3 円を払い、黒板から 31 個消して \left \lfloor \dfrac{3}{2} \right\rfloor = 1, \left\lceil \dfrac{3}{2} \right\rceil = 2 を新たに黒板に書く。
  • 黒板には 21 個と 11 個書かれている。
  • 高橋君は 2 を選ぶ。高橋君は 2 円を払い、黒板から 21 個消して \left \lfloor \dfrac{2}{2} \right\rfloor = 1, \left\lceil \dfrac{2}{2} \right\rceil = 1 を新たに黒板に書く。
  • 黒板には 13 個書かれている。
  • 黒板から 2 以上の整数が全て無くなったので操作を終了する。

操作全体で高橋君は 3 + 2 = 5 円払ったので、5 を出力します。


入力例 2

340

出力例 2

2888

入力例 3

100000000000000000

出力例 3

5655884811924144128

Score: 300 points

Problem Statement

There is a single integer N written on a blackboard.
Takahashi will repeat the following series of operations until all integers not less than 2 are removed from the blackboard:

  • Choose one integer x not less than 2 written on the blackboard.
  • Erase one occurrence of x from the blackboard. Then, write two new integers \left \lfloor \dfrac{x}{2} \right\rfloor and \left\lceil \dfrac{x}{2} \right\rceil on the blackboard.
  • Takahashi must pay x yen to perform this series of operations.

Here, \lfloor a \rfloor denotes the largest integer not greater than a, and \lceil a \rceil denotes the smallest integer not less than a.

What is the total amount of money Takahashi will have paid when no more operations can be performed?
It can be proved that the total amount he will pay is constant regardless of the order in which the operations are performed.

Constraints

  • 2 \leq N \leq 10^{17}

Input

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

N

Output

Print the total amount of money Takahashi will have paid, in yen.


Sample Input 1

3

Sample Output 1

5

Here is an example of how Takahashi performs the operations:

  • Initially, there is one 3 written on the blackboard.
  • He chooses 3. He pays 3 yen, erases one 3 from the blackboard, and writes \left \lfloor \dfrac{3}{2} \right\rfloor = 1 and \left\lceil \dfrac{3}{2} \right\rceil = 2 on the blackboard.
  • There is one 2 and one 1 written on the blackboard.
  • He chooses 2. He pays 2 yen, erases one 2 from the blackboard, and writes \left \lfloor \dfrac{2}{2} \right\rfloor = 1 and \left\lceil \dfrac{2}{2} \right\rceil = 1 on the blackboard.
  • There are three 1s written on the blackboard.
  • Since all integers not less than 2 have been removed from the blackboard, the process is finished.

Takahashi has paid a total of 3 + 2 = 5 yen for the entire process, so print 5.


Sample Input 2

340

Sample Output 2

2888

Sample Input 3

100000000000000000

Sample Output 3

5655884811924144128
D - Super Takahashi Bros.

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 425

問題文

高橋君はゲームをプレイしています。

ゲームは 1,2,\ldots,N の番号がついた N 個のステージからなり、現在はステージ 1 のみを遊ぶことができます。

各ステージ i ( 1\leq i \leq N-1 )が遊べるとき、ステージ i では以下の 2 つのどちらかの行動を行えます。

  • A_i 秒掛けてステージ i をクリアする。ステージ i+1 を遊べるようになる。
  • B_i 秒掛けてステージ i をクリアする。ステージ X_i を遊べるようになる。

各ステージをクリアするためにかかる時間以外は無視できるとき、ステージ N を遊べるようになるのは最短で何秒後ですか?

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq A_i, B_i \leq 10^9
  • 1 \leq X_i \leq N
  • 入力は全て整数

入力

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

N
A_1 B_1 X_1
A_2 B_2 X_2
\vdots
A_{N-1} B_{N-1} X_{N-1}

出力

答えを出力せよ。


入力例 1

5
100 200 3
50 10 1
100 200 5
150 1 2

出力例 1

350

次のように行動することで、350 秒でステージ 5 を遊べるようになります。

  • 100 秒掛けてステージ 1 をクリアし、ステージ 2 を遊べるようになる。
  • 50 秒掛けてステージ 2 をクリアし、ステージ 3 を遊べるようになる。
  • 200 秒掛けてステージ 3 をクリアし、ステージ 5 を遊べるようになる。

入力例 2

10
1000 10 9
1000 10 10
1000 10 2
1000 10 3
1000 10 4
1000 10 5
1000 10 6
1000 10 7
1000 10 8

出力例 2

90

入力例 3

6
1000000000 1000000000 1
1000000000 1000000000 1
1000000000 1000000000 1
1000000000 1000000000 1
1000000000 1000000000 1

出力例 3

5000000000

Score: 425 points

Problem Statement

Takahashi is playing a game.

The game consists of N stages numbered 1,2,\ldots,N. Initially, only stage 1 can be played.

For each stage i ( 1\leq i \leq N-1 ) that can be played, you can perform one of the following two actions at stage i:

  • Spend A_i seconds to clear stage i. This allows you to play stage i+1.
  • Spend B_i seconds to clear stage i. This allows you to play stage X_i.

Ignoring the times other than the time spent to clear the stages, how many seconds will it take at the minimum to be able to play stage N?

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq A_i, B_i \leq 10^9
  • 1 \leq X_i \leq N
  • All input values are integers.

Input

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

N
A_1 B_1 X_1
A_2 B_2 X_2
\vdots
A_{N-1} B_{N-1} X_{N-1}

Output

Print the answer.


Sample Input 1

5
100 200 3
50 10 1
100 200 5
150 1 2

Sample Output 1

350

By acting as follows, you will be allowed to play stage 5 in 350 seconds.

  • Spend 100 seconds to clear stage 1, which allows you to play stage 2.
  • Spend 50 seconds to clear stage 2, which allows you to play stage 3.
  • Spend 200 seconds to clear stage 3, which allows you to play stage 5.

Sample Input 2

10
1000 10 9
1000 10 10
1000 10 2
1000 10 3
1000 10 4
1000 10 5
1000 10 6
1000 10 7
1000 10 8

Sample Output 2

90

Sample Input 3

6
1000000000 1000000000 1
1000000000 1000000000 1
1000000000 1000000000 1
1000000000 1000000000 1
1000000000 1000000000 1

Sample Output 3

5000000000
E - Mancala 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 475

問題文

0 から N-1 の番号がついた N 個の箱があります。最初、箱 i には A_i 個のボールが入っています。

高橋君は i=1,2,\ldots,M の順に以下の操作を行います。

  • 変数 C0 とする。
  • B_i の中のボールを全て取り出し、手に持つ。
  • 手にボールを 1 個以上持っている間、次の処理を繰り返す:
    • C の値を 1 増やす。
    • 手に持っているボールを 1 個、箱 (B_i+C) \bmod N に入れる。

全ての操作を終えた後、各箱に入っているボールの個数を求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq M \leq 2\times 10^5
  • 0 \leq A_i \leq 10^9
  • 0 \leq B_i < N
  • 入力は全て整数

入力

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

N M
A_0 A_1 \ldots A_{N-1}
B_1 B_2 \ldots B_M

出力

全ての操作を終えた後、箱 i に入っているボールの個数を X_i とする。X_0,X_1,\ldots,X_{N-1} をこの順に空白区切りで出力せよ。


入力例 1

5 3
1 2 3 4 5
2 4 0

出力例 1

0 4 2 7 2

操作は次のように進行します。

図


入力例 2

3 10
1000000000 1000000000 1000000000
0 1 0 1 0 1 0 1 0 1

出力例 2

104320141 45436840 2850243019

入力例 3

1 4
1
0 0 0 0

出力例 3

1

Score: 475 points

Problem Statement

There are N boxes numbered 0 to N-1. Initially, box i contains A_i balls.

Takahashi will perform the following operations for i=1,2,\ldots,M in order:

  • Set a variable C to 0.
  • Take out all the balls from box B_i and hold them in hand.
  • While holding at least one ball in hand, repeat the following process:
    • Increase the value of C by 1.
    • Put one ball from hand into box (B_i+C) \bmod N.

Determine the number of balls in each box after completing all operations.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq M \leq 2\times 10^5
  • 0 \leq A_i \leq 10^9
  • 0 \leq B_i < N
  • All input values are integers.

Input

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

N M
A_0 A_1 \ldots A_{N-1}
B_1 B_2 \ldots B_M

Output

Let X_i be the number of balls in box i after completing all operations. Print X_0,X_1,\ldots,X_{N-1} in this order, separated by spaces.


Sample Input 1

5 3
1 2 3 4 5
2 4 0

Sample Output 1

0 4 2 7 2

The operations proceed as follows:

Figure


Sample Input 2

3 10
1000000000 1000000000 1000000000
0 1 0 1 0 1 0 1 0 1

Sample Output 2

104320141 45436840 2850243019

Sample Input 3

1 4
1
0 0 0 0

Sample Output 3

1
F - S = 1

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 525

問題文

整数 X, Y が与えられます。X, YX \neq 0Y \neq 0 の少なくとも一方を満たします。
次の条件を全て満たす整数の組 (A, B) を発見してください。そのような整数の組が存在しない場合はそれを報告してください。

  • -10^{18} \leq A, B \leq 10^{18}
  • xy 平面上の点 (0, 0), (X, Y), (A, B) を頂点とする三角形の面積は 1

制約

  • -10^{17} \leq X, Y \leq 10^{17}
  • (X, Y) \neq (0, 0)
  • X, Y は整数

入力

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

X Y

出力

条件を満たす整数の組 (A, B) が存在する場合は以下の形式で出力せよ。

A B

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


入力例 1

3 5

出力例 1

1 1

(0, 0), (3, 5), (1, 1) を頂点とする三角形の面積は 1 です。よって (A, B) = (1, 1) は条件を満たします。


入力例 2

-2 0

出力例 2

0 1

入力例 3

8752654402832944 -6857065241301125

出力例 3

-1

Score: 525 points

Problem Statement

You are given integers X and Y, which satisfy at least one of X \neq 0 and Y \neq 0.
Find a pair of integers (A, B) that satisfies all of the following conditions. If no such pair exists, report so.

  • -10^{18} \leq A, B \leq 10^{18}
  • The area of the triangle with vertices at points (0, 0), (X, Y), (A, B) on the xy-plane is 1.

Constraints

  • -10^{17} \leq X, Y \leq 10^{17}
  • (X, Y) \neq (0, 0)
  • X and Y are integers.

Input

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

X Y

Output

If there is a pair of integers (A, B) that satisfies the conditions, print it in the following format:

A B

Otherwise, print -1.


Sample Input 1

3 5

Sample Output 1

1 1

The area of the triangle with vertices at points (0, 0), (3, 5), (1, 1) is 1. Thus, (A, B) = (1, 1) satisfies the conditions.


Sample Input 2

-2 0

Sample Output 2

0 1

Sample Input 3

8752654402832944 -6857065241301125

Sample Output 3

-1
G - Leaf Color

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

頂点に 1 から N までの番号がついた N 頂点の木 T があります。i 番目の辺は頂点 u_i と頂点 v_i を結んでいます。また、頂点 i は色 A_i で塗られています。
T の頂点集合の(非空な)部分集合 S のうち、次の条件を満たすものの個数を 998244353 で割った余りを求めてください。

  • TS による誘導部分グラフ G は次の条件を全て満たす。
    • G は木である。
    • 次数 1 の頂点に塗られた色が全て一致している。
誘導部分グラフとは S をグラフ G の頂点の部分集合とします。このとき、GS による誘導部分グラフとは、頂点集合が S で、辺集合が「G の辺であって両端が S に含まれるもの全て」であるようなグラフです。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • 1 \leq u_i \lt v_i \leq N
  • 入力で与えられるグラフは木
  • 入力される値は全て整数

入力

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

N
A_1 A_2 \dots A_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

出力

問題文の条件を満たす頂点集合の(非空な)部分集合 S の個数を 998244353 で割った余りを出力せよ。


入力例 1

3
1 2 1
1 2
2 3

出力例 1

4

条件を満たす頂点の集合は次の 4 通りです。

  • \lbrace 1 \rbrace
  • \lbrace 1, 2, 3 \rbrace
  • \lbrace 2 \rbrace
  • \lbrace 3 \rbrace

入力例 2

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

出力例 2

9

入力例 3

15
5 3 5 1 1 4 4 4 2 5 5 4 4 2 5
3 13
4 10
7 11
8 9
2 10
2 14
5 11
5 6
6 13
12 13
9 14
9 13
1 13
1 15

出力例 3

48

Score: 600 points

Problem Statement

There is a tree T with N vertices numbered from 1 to N. The i-th edge connects vertices u_i and v_i. Additionally, vertex i is painted with color A_i.
Find the number, modulo 998244353, of (non-empty) subsets S of the vertex set of T that satisfy the following condition:

  • The induced subgraph G of T by S satisfies all of the following conditions:
    • G is a tree.
    • All vertices with degree 1 have the same color.
What is an induced subgraph? Let S be a subset of the vertices of a graph G. The induced subgraph of G by S is a graph whose vertex set is S and whose edge set consists of all edges in G that have both endpoints in S.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq N
  • 1 \leq u_i \lt v_i \leq N
  • The graph given in the input is a tree.
  • All input values are integers.

Input

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

N
A_1 A_2 \dots A_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

Output

Print the number, modulo 998244353, of (non-empty) subsets S of the vertex set of T that satisfy the condition in the problem statement.


Sample Input 1

3
1 2 1
1 2
2 3

Sample Output 1

4

The following four sets of vertices satisfy the condition.

  • \lbrace 1 \rbrace
  • \lbrace 1, 2, 3 \rbrace
  • \lbrace 2 \rbrace
  • \lbrace 3 \rbrace

Sample Input 2

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

Sample Output 2

9

Sample Input 3

15
5 3 5 1 1 4 4 4 2 5 5 4 4 2 5
3 13
4 10
7 11
8 9
2 10
2 14
5 11
5 6
6 13
12 13
9 14
9 13
1 13
1 15

Sample Output 3

48