A - 9x9

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

1 文字目が数字、2 文字目が文字 x3 文字目が数字であるような 3 文字の文字列 S が与えられます。

S2 つの数の積を求めてください。

制約

  • S1 文字目が 1 以上 9 以下の整数、2 文字目が文字 x3 文字目が 1 以上 9 以下の整数であるような 3 文字の文字列

入力

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

S

出力

答えを整数として出力せよ。


入力例 1

3x8

出力例 1

24

3 \times 8 = 24 より、24 を出力します。


入力例 2

9x9

出力例 2

81

9 \times 9 = 81 より、81 を出力します。

Score : 100 points

Problem Statement

You are given a 3-character string S, where the first character is a digit, the second character is the character x, and the third character is a digit.

Find the product of the two numbers in S.

Constraints

  • S is a 3-character string where the first character is an integer between 1 and 9, inclusive, the second character is the character x, and the third character is an integer between 1 and 9, inclusive.

Input

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

S

Output

Print the answer as an integer.


Sample Input 1

3x8

Sample Output 1

24

From 3 \times 8 = 24, print 24.


Sample Input 2

9x9

Sample Output 2

81

From 9 \times 9 = 81, print 81.

B - tcaF

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 150

問題文

2 以上の整数 X が与えられます。

N!=X を満たすような正の整数 N を求めてください。

ただし、N!N の階乗を表し、そのような N がただ一つ存在することは保証されています。

制約

  • 2 \leq X \leq 3 \times 10^{18}
  • N!=X を満たすような正の整数 N がただ一つ存在する
  • 入力は全て整数

入力

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

X

出力

答えを出力せよ。


入力例 1

6

出力例 1

3

3!=3\times2\times1=6 より、3 を出力します。


入力例 2

2432902008176640000

出力例 2

20

20!=2432902008176640000 より、20 を出力します。

Score : 150 points

Problem Statement

You are given an integer X not less than 2.

Find the positive integer N such that N! = X.

Here, N! denotes the factorial of N, and it is guaranteed that there is exactly one such N.

Constraints

  • 2 \leq X \leq 3 \times 10^{18}
  • There is exactly one positive integer N such that N!=X.
  • All input values are integers.

Input

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

X

Output

Print the answer.


Sample Input 1

6

Sample Output 1

3

From 3!=3\times2\times1=6, print 3.


Sample Input 2

2432902008176640000

Sample Output 2

20

From 20!=2432902008176640000, print 20.

C - Snake Queue

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

ヘビの待ち行列があります。最初、列は空です。

クエリが Q 個与えられるので、与えられた順に処理してください。クエリは以下の 3 種類です。

  • タイプ 1 : 1 l の形式で与えられる。長さ l のヘビが列の末尾に追加される。このとき追加するヘビの頭の位置は、元の列が空の場合は座標 0、そうでない場合は最後尾のヘビの頭の座標に最後尾のヘビの長さを加えた座標となる。
  • タイプ 2 : 2 の形式で与えられる。列の先頭にいるヘビが列から抜ける。このとき、列が空でないことは保証される。抜けたヘビの長さを m として、列に残っている全てのヘビの頭の座標が m だけ減少する。
  • タイプ 3 : 3 k の形式で与えられる。列の先頭から数えて k 番目にいるヘビの頭の座標を出力せよ。このとき、列には少なくとも k 匹のヘビがいることが保証される。

制約

  • 1 \leq Q \leq 3 \times 10^{5}
  • タイプ 1 のクエリにおいて、1 \leq l \leq 10^{9}
  • タイプ 2 のクエリにおいて、列が空でないことが保証される
  • タイプ 3 のクエリにおいて、列にいるヘビの数を n として、1 \leq k \leq n
  • 入力は全て整数

入力

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

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

ただし、\text{query}_ii 個目のクエリを表し、以下のいずれかの形式である。

1 l
2
3 k

出力

タイプ 3 のクエリの個数を q として、q 行出力せよ。i 行目には、i 個目のタイプ 3 のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

5
10
  • 1 個目のクエリ : 長さ 5 のヘビが列に追加される。列にヘビはいないため、追加されたヘビの頭の座標は 0 となる。
  • 2 個目のクエリ : 長さ 7 のヘビが列に追加される。追加する前の最後尾のヘビの頭の座標が 0 で長さが 5 のため、追加されたヘビの頭の座標は 5 となる。
  • 3 個目のクエリ : 前から 2 番目にいるヘビの頭の座標を出力する。列にいるヘビの頭の座標は前から順に 0, 5 であるため、5 を出力する。
  • 4 個目のクエリ : 長さ 3 のヘビが列に追加される。追加する前の最後尾のヘビの頭の座標が 5 で長さが 7 のため、追加されたヘビの頭の座標は 12 となる。
  • 5 個目のクエリ : 長さ 4 のヘビが列に追加される。追加する前の最後尾のヘビの頭の座標が 12 で長さが 3 のため、追加されたヘビの頭の座標は 15 となる。
  • 6 個目のクエリ : 先頭のヘビが列から抜ける。抜けたヘビの長さが 5 であるため、列にいるヘビの頭の座標は 5 だけ減少する。列に残っているヘビの頭の座標は先頭から順に 0,7,10 となる。
  • 7 個目のクエリ : 前から 3 番目にいるヘビの頭の座標を出力する。列にいるヘビの頭の座標は前から順に 0, 7, 10 であるため、10 を出力する。

入力例 2

3
1 1
2
1 3

出力例 2



タイプ 3 のクエリが 1 つもない場合もあります。


入力例 3

10
1 15
1 10
1 5
2
1 5
1 10
1 15
2
3 4
3 2

出力例 3

20
5

Score : 300 points

Problem Statement

There is a queue of snakes. Initially, the queue is empty.

You are given Q queries, which should be processed in the order they are given. There are three types of queries:

  • Type 1: Given in the form 1 l. A snake of length l is added to the end of the queue. If the queue was empty before adding, the head position of the newly added snake is 0; otherwise, it is the sum of the head coordinate of the last snake in the queue and the last snake’s length.
  • Type 2: Given in the form 2. The snake at the front of the queue leaves the queue. It is guaranteed that the queue is not empty at this time. Let m be the length of the snake that left, then the head coordinate of every snake remaining in the queue decreases by m.
  • Type 3: Given in the form 3 k. Output the head coordinate of the snake that is k-th from the front of the queue. It is guaranteed that there are at least k snakes in the queue at this time.

Constraints

  • 1 \leq Q \leq 3 \times 10^{5}
  • For a query of type 1, 1 \leq l \leq 10^{9}
  • For a query of type 2, it is guaranteed that the queue is not empty.
  • For a query of type 3, let n be the number of snakes in the queue, then 1 \leq k \leq n.
  • All input values are integers.

Input

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

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

Here, \text{query}_i is the i-th query in one of the following forms:

1 l
2
3 k

Output

Let q be the number of queries of type 3. Print q lines. The i-th line should contain the answer to the i-th type 3 query.


Sample Input 1

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

Sample Output 1

5
10
  • 1st query: A snake of length 5 is added to the queue. Since the queue was empty, the head coordinate of this snake is 0.
  • 2nd query: A snake of length 7 is added to the queue. Before adding, the last snake has head coordinate 0 and length 5, so the newly added snake’s head coordinate is 5.
  • 3rd query: Output the head coordinate of the snake that is 2nd from the front. Currently, the head coordinates of the snakes in order are 0, 5, so output 5.
  • 4th query: A snake of length 3 is added to the queue. Before adding, the last snake has head coordinate 5 and length 7, so the new snake’s head coordinate is 12.
  • 5th query: A snake of length 4 is added to the queue. Before adding, the last snake has head coordinate 12 and length 3, so the new snake’s head coordinate is 15.
  • 6th query: The snake at the front leaves the queue. The length of the snake that left is 5, so the head coordinate of each remaining snake decreases by 5. The remaining snake’s head coordinate becomes 0, 7, 10.
  • 7th query: Output the head coordinate of the snake that is 3rd from the front. Currently, the head coordinates of the snakes in order are 0, 7, 10, so output 10.

Sample Input 2

3
1 1
2
1 3

Sample Output 2



It is possible that there are no queries of type 3.


Sample Input 3

10
1 15
1 10
1 5
2
1 5
1 10
1 15
2
3 4
3 2

Sample Output 3

20
5
D - Squares in Circle

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

二次元座標平面上に 1\times 1 の正方形が無限に敷き詰められています。

ある正方形の中心を中心として、半径 R の円を描いたとき、円に完全に内包される正方形は何個あるでしょうか?

より厳密には、整数組 (i,j) であって、4(i+0.5,j+0.5),(i+0.5,j-0.5),(i-0.5,j+0.5),(i-0.5,j-0.5) 全てが原点との距離が R 以下という条件を満たすものの個数を求めてください。

制約

  • 1\leq R\leq 10^{6}
  • 入力される数値は全て整数

入力

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

R

出力

答えを出力せよ。


入力例 1

2

出力例 1

5

正方形の中心が円の中心と一致するような正方形、及びその正方形に隣接する正方形 4 個の、合計 5 個の正方形が円に完全に内包されています。


入力例 2

4

出力例 2

37

入力例 3

26

出力例 3

2025

Score : 400 points

Problem Statement

On the two-dimensional coordinate plane, there is an infinite tiling of 1 \times 1 squares.

Consider drawing a circle of radius R centered at the center of one of these squares. How many of these squares are completely contained inside the circle?

More precisely, find the number of integer pairs (i,j) such that all four points (i+0.5,j+0.5), (i+0.5,j-0.5), (i-0.5,j+0.5), and (i-0.5,j-0.5) are at a distance of at most R from the origin.

Constraints

  • 1 \leq R \leq 10^{6}
  • All input values are integers.

Input

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

R

Output

Print the answer.


Sample Input 1

2

Sample Output 1

5

There are a total of five squares completely contained in the circle: the square whose center matches the circle’s center, plus the four squares adjacent to it.


Sample Input 2

4

Sample Output 2

37

Sample Input 3

26

Sample Output 3

2025
E - Square Price

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 475

問題文

N 種類の商品がそれぞれ 10^{100} 個ずつあります。

あなたはこれらの商品を各種類について 0 個以上買うことが出来ます。i 番目の商品を k 個買うには k^2P_i 円かかります。

購入金額の合計を M 円以下にするとき、最大何個の商品を買うことができるか求めてください。

制約

  • 1\leq N\leq 2\times 10^{5}
  • 1\leq M\leq 10^{18}
  • 1\leq P_i\leq 2\times 10^{9}
  • 入力される数値は全て整数

入力

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

N M
P_1 \ldots P_N

出力

答えを出力せよ。


入力例 1

3 9
4 1 9

出力例 1

3

1 種類目の商品を 1 個、2 種類目の商品を 2 個買うとき、購入金額の合計は 1^2 \times 4+2^2\times 1=8 です。 購入金額の合計が 9 円以下で 4 個以上の商品を買うことはできないため、3 が答えとなります。


入力例 2

10 1000
2 15 6 5 12 1 7 9 17 2

出力例 2

53

Score : 475 points

Problem Statement

There are N types of products, each having 10^{100} units in stock.

You can buy any non-negative number of units of each product. To buy k units of the i-th product, it costs k^2 P_i yen.

If your total purchase cost is at most M yen, what is the maximum number of units you can buy in total?

Constraints

  • 1 \leq N \leq 2 \times 10^{5}
  • 1 \leq M \leq 10^{18}
  • 1 \leq P_i \leq 2 \times 10^{9}
  • All input values are integers.

Input

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

N M
P_1 \ldots P_N

Output

Print the answer.


Sample Input 1

3 9
4 1 9

Sample Output 1

3

If you buy one unit of the 1st product and two units of the 2nd product, the total purchase cost is 1^2 \times 4 + 2^2 \times 1 = 8. It is impossible to buy four or more units in total with a total cost of at most 9 yen, so the answer is 3.


Sample Input 2

10 1000
2 15 6 5 12 1 7 9 17 2

Sample Output 2

53
F - Rated Range

実行時間制限: 2.5 sec / メモリ制限: 1024 MiB

配点 : 525

問題文

高橋君はAtCoderのコンテストに N 回参加しようとしています。

i 回目 (1 \leq i \leq N) のコンテストでは、レーティングが L_i 以上 R_i 以下である場合、レーティングが 1 増加します。

以下の形式で与えられる Q 個のクエリに答えてください。

  • 整数 X が与えられる。高橋君の最初のレーティングが X であった場合、N 回のコンテストを終えた後のレーティングを求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq 5 \times 10^5 (1 \leq i \leq N)
  • 1 \leq Q \leq 3 \times 10^5
  • 各クエリについて 1 \leq X \leq 5 \times 10^5
  • 入力は全て整数

入力

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

ただし、\text{query}_ii 個目のクエリを表し、以下の形式である。

X

出力

Q 行出力せよ。i 行目には、i 個目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

6
6
8

1 個目のクエリでは以下のようにコンテストごとにレーティングが変化します。

  • 1 回目のコンテストではレーティングが 1 以上 5 以下のため、レーティングが 1 増加し 4 になります。
  • 2 回目のコンテストではレーティングが 1 以上 3 以下でないため、レーティングが増加せず 4 のままです。
  • 3 回目のコンテストではレーティングが 3 以上 6 以下のため、レーティングが 1 増加し 5 になります。
  • 4 回目のコンテストではレーティングが 2 以上 4 以下でないため、レーティングが増加せず 5 のままです。
  • 5 回目のコンテストではレーティングが 4 以上 7 以下のため、レーティングが 1 増加し 6 になります。

2 個目のクエリでは 1,2,3,5 回目のコンテストでレーティングが 1 増加するため、レーティングは 6 になります。

3 個目のクエリでは 1,3,5 回目のコンテストでレーティングが 1 増加するため、レーティングは 8 になります。


入力例 2

10
1 1999
1 1999
1200 2399
1 1999
1 1999
1 1999
2000 500000
1 1999
1 1999
1600 2799
7
1
1995
2000
2399
500000
2799
1000

出力例 2

8
2002
2003
2402
500001
2800
1007

入力例 3

15
260522 414575
436426 479445
148772 190081
190629 433447
47202 203497
394325 407775
304784 463982
302156 468417
131932 235902
78537 395728
223857 330739
286918 329211
39679 238506
63340 186568
160016 361868
10
287940
296263
224593
101449
336991
390310
323355
177068
11431
8580

出力例 3

287946
296269
224599
101453
336997
390315
323363
177075
11431
8580

Score : 525 points

Problem Statement

Takahashi plans to participate in N AtCoder contests.

In the i-th contest (1 \leq i \leq N), if his rating is between L_i and R_i (inclusive), his rating increases by 1.

You are given Q queries in the following format:

  • An integer X is given. Assuming that Takahashi's initial rating is X, determine his rating after participating in all N contests.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq 5 \times 10^5 (1 \leq i \leq N)
  • 1 \leq Q \leq 3 \times 10^5
  • For each query, 1 \leq X \leq 5 \times 10^5.
  • All input values are integers.

Input

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Here, \text{query}_i is the i-th query in the form:

X

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

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

Sample Output 1

6
6
8

For the 1st query, the rating changes as follows:

  • In the 1st contest, the rating is between 1 and 5, so it increases by 1, becoming 4.
  • In the 2nd contest, the rating is not between 1 and 3, so it remains 4.
  • In the 3rd contest, the rating is between 3 and 6, so it increases by 1, becoming 5.
  • In the 4th contest, the rating is not between 2 and 4, so it remains 5.
  • In the 5th contest, the rating is between 4 and 7, so it increases by 1, becoming 6.

For the 2nd query, the rating increases in the 1st, 2nd, 3rd, and 5th contests, ending at 6.

For the 3rd query, the rating increases in the 1st, 3rd, and 5th contests, ending at 8.


Sample Input 2

10
1 1999
1 1999
1200 2399
1 1999
1 1999
1 1999
2000 500000
1 1999
1 1999
1600 2799
7
1
1995
2000
2399
500000
2799
1000

Sample Output 2

8
2002
2003
2402
500001
2800
1007

Sample Input 3

15
260522 414575
436426 479445
148772 190081
190629 433447
47202 203497
394325 407775
304784 463982
302156 468417
131932 235902
78537 395728
223857 330739
286918 329211
39679 238506
63340 186568
160016 361868
10
287940
296263
224593
101449
336991
390310
323355
177068
11431
8580

Sample Output 3

287946
296269
224599
101453
336997
390315
323363
177075
11431
8580
G - Odd Even Graph

実行時間制限: 5 sec / メモリ制限: 1024 MiB

配点 : 600

問題文

正偶数 N 、素数 P が与えられます。

M=N-1,\ldots,\frac{N(N-1)}{2} について、以下の問題を解いてください。

頂点に 1 から N の番号が付いた N 頂点 M 辺の無向連結単純グラフであって、頂点 1 からの最短距離が偶数の頂点の個数と距離が奇数の頂点の個数が等しいものは何個あるでしょうか。個数を P で割ったあまりを求めてください。

制約

  • 2\leq N\leq 30
  • 10^8\leq P\leq 10^9
  • N は偶数
  • P は素数
  • 入力される数値は全て整数

入力

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

N P

出力

M=N-1,\ldots,\frac{N(N-1)}{2} に対する答えを順に空白区切りで一行に出力せよ。


入力例 1

4 998244353

出力例 1

12 9 3 0

4 頂点 3 辺の条件を満たす単純連結無向グラフは 12 個あります。

4 頂点 4 辺の条件を満たす単純連結無向グラフは 9 個あります。

4 頂点 5 辺の条件を満たす単純連結無向グラフは 3 個あります。

4 頂点 6 辺の条件を満たす単純連結無向グラフは 0 個あります。


入力例 2

6 924844033

出力例 2

810 2100 3060 3030 2230 1210 450 100 10 0 0

入力例 3

10 433416647

出力例 3

49218750 419111280 321937732 107111441 372416570 351559278 312484809 334285827 317777667 211471846 58741385 422156135 323887465 54923551 121645733 94354149 346849276 72744827 385773306 163421544 351691775 59915863 430096957 166653801 346330874 185052506 245426328 47501118 7422030 899640 79380 4536 126 0 0 0 0

個数を P で割ったあまりを求めることに注意してください。

Score : 600 points

Problem Statement

You are given a positive even integer N and a prime number P.

For M = N-1, \ldots, \frac{N(N-1)}{2}, solve the following problem.

How many undirected connected simple graphs with N vertices labeled from 1 to N and M edges satisfy this: the number of vertices whose shortest distance from vertex 1 is even is equal to the number of vertices whose shortest distance from vertex 1 is odd? Find this number modulo P.

Constraints

  • 2 \leq N \leq 30
  • 10^8 \leq P \leq 10^9
  • N is even.
  • P is prime.
  • All input values are integers.

Input

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

N P

Output

For M = N-1, \ldots, \frac{N(N-1)}{2}, output the answers in order, separated by spaces, on a single line.


Sample Input 1

4 998244353

Sample Output 1

12 9 3 0

With four vertices and three edges, there are 12 simple connected undirected graphs satisfying the condition.

With four vertices and four edges, there are 9 such graphs.

With four vertices and five edges, there are 3 such graphs.

With four vertices and six edges, there are 0 such graphs.


Sample Input 2

6 924844033

Sample Output 2

810 2100 3060 3030 2230 1210 450 100 10 0 0

Sample Input 3

10 433416647

Sample Output 3

49218750 419111280 321937732 107111441 372416570 351559278 312484809 334285827 317777667 211471846 58741385 422156135 323887465 54923551 121645733 94354149 346849276 72744827 385773306 163421544 351691775 59915863 430096957 166653801 346330874 185052506 245426328 47501118 7422030 899640 79380 4536 126 0 0 0 0

Remember to find the number of such graphs modulo P.