A - Security

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

配点: 100

問題文

すぬけ君の管理する研究室の扉にはロックがかかっており、解錠にはセキュリティコードを入力する必要があります。

セキュリティコードは 4 桁の数字列です。セキュリティコードが「入力しづらい」とは、同じ数字が連続する箇所が存在することを言います。

現在のセキュリティコード S が与えられます。S が「入力しづらい」なら Bad を、そうでなければ Good を出力してください。

制約

  • S は半角数字のみからなる長さ 4 の文字列

入力

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

S

出力

セキュリティコード S が「入力しづらい」なら Bad を、そうでなければ Good を出力してください。


入力例 1

3776

出力例 1

Bad

2 文字目と 3 文字目が同じなので、3776 は入力しづらいセキュリティコードです。


入力例 2

8080

出力例 2

Good

同じ数字が連続していないので、8080 は入力しづらいセキュリティコードではありません。


入力例 3

1333

出力例 3

Bad

入力例 4

0024

出力例 4

Bad

Score : 100 points

Problem Statement

The door of Snuke's laboratory is locked with a security code.

The security code is a 4-digit number. We say the security code is hard to enter when it contains two consecutive digits that are the same.

You are given the current security code S. If S is hard to enter, print Bad; otherwise, print Good.

Constraints

  • S is a 4-character string consisting of digits.

Input

Input is given from Standard Input in the following format:

S

Output

If S is hard to enter, print Bad; otherwise, print Good.


Sample Input 1

3776

Sample Output 1

Bad

The second and third digits are the same, so 3776 is hard to enter.


Sample Input 2

8080

Sample Output 2

Good

There are no two consecutive digits that are the same, so 8080 is not hard to enter.


Sample Input 3

1333

Sample Output 3

Bad

Sample Input 4

0024

Sample Output 4

Bad
B - Bite Eating

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

配点 : 200

問題文

N 個のリンゴがあります。これらはそれぞれリンゴ 1、リンゴ 2、リンゴ 3、...、リンゴ N と呼ばれており、リンゴ i の「味」は L+i-1 です。「味」は負になることもありえます。

また、1 個以上のリンゴを材料として、アップルパイをつくることができます。その「味」は、材料となったリンゴの「味」の総和となります。

あなたはこれらのリンゴを全て材料として、アップルパイをつくる予定でしたが、おなかがすいたので 1 個だけ食べることにしました。勿論、食べてしまったリンゴはアップルパイの材料にはできません。

つくる予定だったアップルパイとできるだけ同じものをつくりたいので、N 個のリンゴ全てを材料としてできるアップルパイの「味」と、食べていない N-1 個のリンゴを材料としてできるアップルパイの「味」の差の絶対値ができるだけ小さくなるように、食べるリンゴを選ぶことにしました。

このようにして選ばれたリンゴを食べた時、食べていない N-1 個のリンゴを材料としてできるアップルパイの「味」を求めてください。

なお、この値は一意に定まることが証明できます。

制約

  • 2 \leqq N \leqq 200
  • -100 \leqq L \leqq 100
  • 入力は全て整数である。

入力

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

N L

出力

最適に食べるリンゴを選んだ時の、食べていない N-1 個のリンゴを材料としてできるアップルパイの「味」を出力してください。


入力例 1

5 2

出力例 1

18

リンゴ 1,2,3,4,5 の「味」は、それぞれ 2,3,4,5,6 です。リンゴ 1 を食べるのが最適で、答えは 3+4+5+6=18 となります。


入力例 2

3 -1

出力例 2

0

リンゴ 1,2,3 の「味」は、それぞれ -1,0,1 です。リンゴ 2 を食べるのが最適で、答えは (-1)+1=0 となります。


入力例 3

30 -50

出力例 3

-1044

Score : 200 points

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

We can prove that this value is uniquely determined.

Constraints

  • 2 \leq N \leq 200
  • -100 \leq L \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.


Sample Input 1

5 2

Sample Output 1

18

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.


Sample Input 2

3 -1

Sample Output 2

0

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.


Sample Input 3

30 -50

Sample Output 3

-1044
C - Anti-Division

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

配点 : 300

問題文

整数 A,B,C,D が与えられます。A 以上 B 以下の整数のうち、C でも D でも割り切れないものの個数を求めてください。

制約

  • 1\leq A\leq B\leq 10^{18}
  • 1\leq C,D\leq 10^9
  • 入力はすべて整数である

入力

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

A B C D

出力

A 以上 B 以下の整数のうち、C でも D でも割り切れないものの個数を出力せよ。


入力例 1

4 9 2 3

出力例 1

2

5,7 が条件を満たします。


入力例 2

10 40 6 8

出力例 2

23

入力例 3

314159265358979323 846264338327950288 419716939 937510582

出力例 3

532105071133627368

Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

  • 1\leq A\leq B\leq 10^{18}
  • 1\leq C,D\leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.


Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.


Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368
D - Megalomania

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

配点: 400

問題文

AtCoder王国の王立問題工房でABC管理官の座に就いたキザハシ君は、浮かれるあまり仕事を引き受けすぎてしまいました。

現在の時刻は 0 です。キザハシ君は 1 から N までの番号が振られた N 件の仕事を持っています。

キザハシ君が仕事 i を終えるには A_i 単位時間かかります。また、仕事 i の〆切は時刻 B_i であり、これまでに仕事を終わらせる必要があります。時刻 B_i ちょうどに仕事 i を終わらせてもかまいません。

キザハシ君は 2 件以上の仕事を同時にすることはできませんが、ある仕事を終わらせた直後に別の仕事を始めることはできます。

キザハシ君はすべての仕事を〆切までに終わらせることができるでしょうか。可能ならば Yes、不可能ならば No を出力してください。

制約

  • 入力はすべて整数
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq 10^9 (1 \leq i \leq N)

入力

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

N
A_1 B_1
.
.
.
A_N B_N

出力

全ての仕事を〆切までに終わらせることが可能ならば Yes、不可能ならば No を出力してください。


入力例 1

5
2 4
1 9
1 8
4 9
3 12

出力例 1

Yes

たとえば以下の順番で仕事を行うことで、すべての仕事を達成できます。

  • 時刻 0 から 1 までの間、仕事 2 を行う。
  • 時刻 1 から 3 までの間、仕事 1 を行う。
  • 時刻 3 から 7 までの間、仕事 4 を行う。
  • 時刻 7 から 8 までの間、仕事 3 を行う。
  • 時刻 8 から 11 までの間、仕事 5 を行う。

仕事 3 は〆切である時刻 8 ちょうどに終えていますが、問題ないことに注意してください。


入力例 2

3
334 1000
334 1000
334 1000

出力例 2

No

どんな順番で仕事をしても、全ての仕事を間に合わせることはできません。


入力例 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

出力例 3

Yes

Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

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

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.


Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

  • Do Job 2 from time 0 to 1.
  • Do Job 1 from time 1 to 3.
  • Do Job 4 from time 3 to 7.
  • Do Job 3 from time 7 to 8.
  • Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.


Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.


Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes
E - Friendships

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

配点 : 500

問題文

以下の条件を満たす N 頂点の無向グラフは存在するでしょうか?

  • グラフは単純かつ連結である。
  • 各頂点には 1, 2, ..., N の番号が付けられている。
  • グラフの辺数を M としたとき、各辺には 1, 2, ..., M の番号が付けられていて、辺 i は頂点 u_i と頂点 v_i をつなぐ長さ 1 の辺である。
  • 最短距離が 2 であるような頂点対 (i,\ j)\ (i < j) が、ちょうど K 個存在する。

条件を満たすグラフが存在するならば 1 つ構築してください。

制約

  • 入力は全て整数である。
  • 2 \leq N \leq 100
  • 0 \leq K \leq \frac{N(N - 1)}{2}

入力

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

N K

出力

条件を満たすグラフが存在しなければ -1 を出力せよ。

存在するならば、そのようなグラフを 1 つ、以下の形式で出力せよ (記号の意味は問題文を参照せよ)。

M
u_1 v_1
:
u_M v_M

条件を満たすグラフが複数存在する場合、どれを出力してもよい。


入力例 1

5 3

出力例 1

5
4 3
1 2
3 1
4 5
2 3

このグラフには最短距離が 2 であるような頂点対が (1,\ 4),\ (2,\ 4),\ (3,\ 5)3 個存在します。よって条件を満たしています。


入力例 2

5 8

出力例 2

-1

条件を満たすグラフは存在しません。

Score: 500 points

Problem Statement

Does there exist an undirected graph with N vertices satisfying the following conditions?

  • The graph is simple and connected.
  • The vertices are numbered 1, 2, ..., N.
  • Let M be the number of edges in the graph. The edges are numbered 1, 2, ..., M, the length of each edge is 1, and Edge i connects Vertex u_i and Vertex v_i.
  • There are exactly K pairs of vertices (i,\ j)\ (i < j) such that the shortest distance between them is 2.

If there exists such a graph, construct an example.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 100
  • 0 \leq K \leq \frac{N(N - 1)}{2}

Input

Input is given from Standard Input in the following format:

N K

Output

If there does not exist an undirected graph with N vertices satisfying the conditions, print -1.

If there exists such a graph, print an example in the following format (refer to Problem Statement for what the symbols stand for):

M
u_1 v_1
:
u_M v_M

If there exist multiple graphs satisfying the conditions, any of them will be accepted.


Sample Input 1

5 3

Sample Output 1

5
4 3
1 2
3 1
4 5
2 3

This graph has three pairs of vertices such that the shortest distance between them is 2: (1,\ 4), (2,\ 4), and (3,\ 5). Thus, the condition is satisfied.


Sample Input 2

5 8

Sample Output 2

-1

There is no graph satisfying the conditions.

F - Must Be Rectangular!

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

配点 : 600

問題文

2 次元平面上の N 個の点があり、i 番目の点の座標は (x_i, y_i) です。

以下の操作を行える限り繰り返します。

  • 座標 (a, b), (a, d), (c, b), (c, d) のうちちょうど 3 箇所に点が存在するような整数 a, b, c, d (a \neq c, b \neq d) を選び、残りの 1 箇所に点を追加する。

この操作は有限回しか行なうことができないことが証明できます。操作回数の最大値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq x_i, y_i \leq 10^5
  • x_i \neq x_j または y_i \neq y_j (i \neq j)
  • 入力は全て整数である

入力

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

N
x_1 y_1
:
x_N y_N

出力

操作回数の最大値を出力せよ。


入力例 1

3
1 1
5 1
5 5

出力例 1

1

a = 1, b = 1, c = 5, d = 5 とすると (1, 5) に点を追加することができます。これ以上操作を行うことはできないので、操作回数の最大値は 1 回です。


入力例 2

2
10 10
20 20

出力例 2

0

2 点しか点がないので操作を 1 回も行うことができません。


入力例 3

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

出力例 3

16

a = 1, b = 1, c = i, d = j (2 \leq i,j \leq 5) の全てに対して操作を行うことができ、それ以上操作を行うことはできないので、操作回数の最大値は 16 回です。

Score : 600 points

Problem Statement

There are N dots in a two-dimensional plane. The coordinates of the i-th dot are (x_i, y_i).

We will repeat the following operation as long as possible:

  • Choose four integers a, b, c, d (a \neq c, b \neq d) such that there are dots at exactly three of the positions (a, b), (a, d), (c, b) and (c, d), and add a dot at the remaining position.

We can prove that we can only do this operation a finite number of times. Find the maximum number of times we can do the operation.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq x_i, y_i \leq 10^5
  • If i \neq j, x_i \neq x_j or y_i \neq y_j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the maximum number of times we can do the operation.


Sample Input 1

3
1 1
5 1
5 5

Sample Output 1

1

By choosing a = 1, b = 1, c = 5, d = 5, we can add a dot at (1, 5). We cannot do the operation any more, so the maximum number of operations is 1.


Sample Input 2

2
10 10
20 20

Sample Output 2

0

There are only two dots, so we cannot do the operation at all.


Sample Input 3

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

Sample Output 3

16

We can do the operation for all choices of the form a = 1, b = 1, c = i, d = j (2 \leq i,j \leq 5), and no more. Thus, the maximum number of operations is 16.