A - Three Threes

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

1 以上 9 以下の整数 N が入力として与えられます。

数字 NN 個繋げて得られる文字列を出力してください。

制約

  • N1 以上 9 以下の整数

入力

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

N

出力

答えを出力せよ。


入力例 1

3

出力例 1

333

33 個繋げて得られる文字列は 333 です。


入力例 2

9

出力例 2

999999999

Score : 100 points

Problem Statement

You are given an integer N between 1 and 9, inclusive, as input.

Concatenate N copies of the digit N and print the resulting string.

Constraints

  • N is an integer between 1 and 9, inclusive.

Input

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

N

Output

Print the answer.


Sample Input 1

3

Sample Output 1

333

Concatenate three copies of the digit 3 to yield the string 333.


Sample Input 2

9

Sample Output 2

999999999
B - Pentagon

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

以下の図で表される正五角形 P があります。

P の点 S_1 と点 S_2 を結ぶ線分と、点 T_1 と点 T_2 を結ぶ線分の長さが等しいか判定してください。

制約

  • S_1,S_2,T_1,T_2A, B, C, D, E のいずれかの文字
  • S_1 \neq S_2
  • T_1 \neq T_2

入力

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

S_1S_2
T_1T_2

出力

P の点 S_1 と点 S_2 を結ぶ線分と、点 T_1 と点 T_2 を結ぶ線分の長さが等しい場合 Yes を、等しくない場合 No を出力せよ。


入力例 1

AC
EC

出力例 1

Yes

P の点 A と点 C を結ぶ線分と、P の点 E と点 C を結ぶ線分の長さは等しいです。


入力例 2

DA
EA

出力例 2

No

P の点 D と点 A を結ぶ線分と、P の点 E と点 A を結ぶ線分の長さは等しくありません。


入力例 3

BD
BD

出力例 3

Yes

Score : 200 points

Problem Statement

A regular pentagon P is shown in the figure below.

Determine whether the length of the line segment connecting points S_1 and S_2 of P equals the length of the line segment connecting points T_1 and T_2.

Constraints

  • Each of S_1, S_2, T_1, and T_2 is one of the characters A, B, C, D, and E.
  • S_1 \neq S_2
  • T_1 \neq T_2

Input

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

S_1S_2
T_1T_2

Output

If the length of the line segment connecting points S_1 and S_2 of P equals the length of the line segment connecting points T_1 and T_2, print Yes; otherwise, print No.


Sample Input 1

AC
EC

Sample Output 1

Yes

The length of the line segment connecting point A and point C of P equals the length of the line segment connecting point E and point C.


Sample Input 2

DA
EA

Sample Output 2

No

The length of the line segment connecting point D and point A of P does not equal the length of the line segment connecting point E and point A.


Sample Input 3

BD
BD

Sample Output 3

Yes
C - Repunit Trio

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

十進法ですべての桁の数字が 1 である整数をレピュニットと呼びます。レピュニットを小さい順に並べると 1,11,111,\ldots です。

ちょうど 3 つのレピュニットの和として表せる整数のうち N 番目に小さいものを求めてください。

制約

  • N1 以上 333 以下の整数

入力

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

N

出力

答えを出力せよ。


入力例 1

5

出力例 1

113

ちょうど 3 つのレピュニットの和として表せる整数を小さい順に並べると 3,13,23,33,113,\ldots です。例えば 113113=1+1+111 と表せます。

3 つのレピュニットは相異ならなくてもよいことに注意してください。


入力例 2

19

出力例 2

2333

入力例 3

333

出力例 3

112222222233

Score : 300 points

Problem Statement

A repunit is an integer whose digits are all 1 in decimal representation. The repunits in ascending order are 1, 11, 111, \ldots.

Find the N-th smallest integer that can be expressed as the sum of exactly three repunits.

Constraints

  • N is an integer between 1 and 333, inclusive.

Input

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

N

Output

Print the answer.


Sample Input 1

5

Sample Output 1

113

The integers that can be expressed as the sum of exactly three repunits are 3, 13, 23, 33, 113, \ldots in ascending order. For example, 113 can be expressed as 113 = 1 + 1 + 111.

Note that the three repunits do not have to be distinct.


Sample Input 2

19

Sample Output 2

2333

Sample Input 3

333

Sample Output 3

112222222233
D - Erase Leaves

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

頂点 1, 頂点 2,\ldots, 頂点 NN 個の頂点からなる木が与えられます。 i 番目 (1\leq i\lt N) の辺は頂点 u _ iv _ i を結んでいます。

次の操作を好きな回数繰り返すことを考えます。

  • 葉である頂点 v1 つ選び、頂点 v およびそれに接続する辺をすべて削除する。

頂点 1 を削除するまでに最小で操作を何回行う必要があるか求めてください。

木とは? 木とは、無向グラフのうち連結であって閉路がないものです。 詳しくはこちらをご覧ください: Wikipedia「木 (数学)」
葉とは? 木の葉とは、木の頂点のうち次数がたかだか 1 であるものです。

制約

  • 2\leq N\leq3\times10^5
  • 1\leq u _ i\lt v _ i\leq N\ (1\leq i\lt N)
  • 与えられるグラフは木
  • 入力はすべて整数

入力

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

N
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ {N-1} v _ {N-1}

出力

答えを 1 行で出力せよ。


入力例 1

9
1 2
2 3
2 4
2 5
1 6
6 7
7 8
7 9

出力例 1

5

与えられるグラフは次のようになります。

たとえば、頂点 9,8,7,6,1 の順に選んで操作を行うことで、5 回の操作で頂点 1 を削除することができます。

4 回以下の操作では頂点 1 を削除することはできないため、5 を出力してください。


入力例 2

6
1 2
2 3
2 4
3 5
3 6

出力例 2

1

与えられたグラフにおいて、頂点 1 は葉です。 よって、1 回目の操作で頂点 1 を選んで削除することができます。


入力例 3

24
3 6
7 17
7 20
7 11
14 18
17 21
6 19
5 22
9 24
11 14
6 23
8 17
9 12
4 17
2 15
1 17
3 9
10 16
7 13
2 16
1 16
5 7
1 3

出力例 3

12

Score : 400 points

Problem Statement

You are given a tree with N vertices: vertex 1, vertex 2, \ldots, vertex N. The i-th edge (1\leq i\lt N) connects vertex u _ i and vertex v _ i.

Consider repeating the following operation some number of times:

  • Choose one leaf vertex v and delete it along with all incident edges.

Find the minimum number of operations required to delete vertex 1.

What is a tree? A tree is an undirected graph that is connected and has no cycles. For more details, see: Wikipedia "Tree (graph theory)".
What is a leaf? A leaf in a tree is a vertex with a degree of at most 1.

Constraints

  • 2\leq N\leq3\times10^5
  • 1\leq u _ i\lt v _ i\leq N\ (1\leq i\lt N)
  • The given graph is a tree.
  • All input values are integers.

Input

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

N
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ {N-1} v _ {N-1}

Output

Print the answer in a single line.


Sample Input 1

9
1 2
2 3
2 4
2 5
1 6
6 7
7 8
7 9

Sample Output 1

5

The given graph looks like this:

For example, you can choose vertices 9,8,7,6,1 in this order to delete vertex 1 in five operations.

Vertex 1 cannot be deleted in four or fewer operations, so print 5.


Sample Input 2

6
1 2
2 3
2 4
3 5
3 6

Sample Output 2

1

In the given graph, vertex 1 is a leaf. Hence, you can choose and delete vertex 1 in the first operation.


Sample Input 3

24
3 6
7 17
7 20
7 11
14 18
17 21
6 19
5 22
9 24
11 14
6 23
8 17
9 12
4 17
2 15
1 17
3 9
10 16
7 13
2 16
1 16
5 7
1 3

Sample Output 3

12
E - Takahashi Quest

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

高橋くんは冒険に出ようとしています。

冒険では、N 個の出来事が起こります。 i 番目 (1\leq i\leq N) の出来事は整数の組 (t _ i,x _ i) (1\leq t _ i\leq 2,1\leq x _ i\leq N) で表され、次のような出来事です。

  • t _ i=1 のとき、タイプ x _ i のポーションを 1 つ発見する。高橋くんは、発見したポーションを拾うか捨てるかのどちらかを選択する。
  • t _ i=2 のとき、タイプ x _ i のモンスター 1 体と遭遇する。高橋くんがタイプ x _ i のポーションを持っている場合、それを 1 つ消費することでモンスターを撃退することができる。モンスターを撃退しなかった場合、高橋くんは敗北する。

高橋くんが敗北することなく全てのモンスターを撃退することができるか判定してください。

高橋くんが全てのモンスターを撃退することができない場合、-1 を出力してください。

高橋くんが全てのモンスターを撃退することができる場合、高橋君が冒険の途中で持っているポーションの個数の最大値を K とします。 高橋くんが敗北しないような戦略全体にわたる K の最小値を K _ {\min} とします。 K _ {\min} の値と、K _ {\min} を達成する高橋くんの行動を出力してください。

制約

  • 1\leq N\leq2\times10^5
  • 1\leq t _ i\leq2\ (1\leq i\leq N)
  • 1\leq x _ i\leq N\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N
t _ 1 x _ 1
t _ 2 x _ 2
\vdots
t _ N x _ N

出力

高橋くんが全てのモンスターを撃退することができない場合、-1 を出力せよ。 高橋くんが全てのモンスターを撃退することができる場合、1 行目には K _ {\min} の値を、2 行目には t _ i=1 であるような i すべてについて昇順に、i 番目の出来事で発見したポーションを拾うなら 1 を、拾わないなら 0 を空白区切りで出力せよ。 K _ {\min} を達成し、高橋くんが敗北せず冒険を終えられるような行動が複数ある場合、どれを出力しても構わない。


入力例 1

13
1 2
1 3
1 1
1 3
1 2
2 3
1 3
1 3
2 3
1 3
2 2
2 3
2 1

出力例 1

3
1 1 1 0 0 1 0 1

出力例は、次のような行動に対応しています。

  • タイプ 2,3,1 のポーションをこの順に発見する。これらのポーションをすべて拾う。
  • タイプ 3,2 のポーションをこの順に発見する。これらのポーションをいずれも拾わない。
  • タイプ 3 のモンスターと遭遇する。持っているタイプ 3 のポーションを 1 つ消費してモンスターを撃退する。
  • タイプ 3 のポーションを発見する。このポーションを拾う。
  • タイプ 3 のポーションを発見する。このポーションを拾わない。
  • タイプ 3 のモンスターと遭遇する。持っているタイプ 3 のポーションを 1 つ消費してモンスターを撃退する。
  • タイプ 3 のポーションを発見する。このポーションを拾う。
  • タイプ 2 のモンスターと遭遇する。持っているタイプ 2 のポーションを 1 つ消費してモンスターを撃退する。
  • タイプ 3 のモンスターと遭遇する。持っているタイプ 3 のポーションを 1 つ消費してモンスターを撃退する。
  • タイプ 1 のモンスターと遭遇する。持っているタイプ 1 のポーションを 1 つ消費してモンスターを撃退する。

この行動では、K の値は 3 となります。

K\leq 2 として敗北しない方法はないので、求める K _ {\min} の値は 3 です。 K=3 を満たして高橋くんが敗北しない行動は複数ありますが、どれを出力しても構いません。


入力例 2

4
2 3
1 4
2 1
1 2

出力例 2

-1

高橋くんはかならず最初に遭遇するモンスターに敗北してしまいます。


入力例 3

30
1 25
1 2
1 10
1 18
2 18
1 11
2 11
1 21
1 6
2 2
2 10
1 11
1 24
1 11
1 3
1 2
1 18
2 25
1 8
1 10
1 11
2 18
2 10
1 10
2 2
1 24
1 10
2 10
1 25
2 6

出力例 3

4
1 1 1 1 1 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0

Score : 450 points

Problem Statement

Takahashi will embark on an adventure.

During the adventure, N events will occur. The i-th event (1\leq i\leq N) is represented by a pair of integers (t _ i,x _ i) (1\leq t _ i\leq 2,1\leq x _ i\leq N) and is as follows:

  • If t _ i=1, he finds one potion of type x _ i. He can choose to pick it up or discard it.
  • If t _ i=2, he encounters one monster of type x _ i. If he has a potion of type x _ i, he can use one to defeat the monster. If he does not defeat it, he will be defeated.

Determine whether he can defeat all the monsters without being defeated.

If he cannot defeat all the monsters, print -1.

Otherwise, let K be the maximum number of potions he has at some point during the adventure. Let K _ {\min} be the minimum value of K across all strategies where he will not be defeated. Print the value of K _ {\min} and the actions of Takahashi that achieve K _ {\min}.

Constraints

  • 1\leq N\leq2\times10^5
  • 1\leq t _ i\leq2\ (1\leq i\leq N)
  • 1\leq x _ i\leq N\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N
t _ 1 x _ 1
t _ 2 x _ 2
\vdots
t _ N x _ N

Output

If Takahashi cannot defeat all the monsters, print -1. If he can, print the value of K _ {\min} in the first line, and in the second line, for each i such that t _ i=1 in ascending order, print 1 if he picks up the potion found at the i-th event, and 0 otherwise, separated by spaces. If multiple sequences of actions achieve K _ {\min} and allow him to finish the adventure without being defeated, you may print any of them.


Sample Input 1

13
1 2
1 3
1 1
1 3
1 2
2 3
1 3
1 3
2 3
1 3
2 2
2 3
2 1

Sample Output 1

3
1 1 1 0 0 1 0 1

The sample output corresponds to the following actions:

  • Find potions of types 2,3,1 in this order. Pick up all of them.
  • Find potions of types 3,2 in this order. Do not pick up any of them.
  • Encounter a type-3 monster. Use one type-3 potion to defeat it.
  • Find a type-3 potion. Pick it up.
  • Find a type-3 potion. Do not pick it up.
  • Encounter a type-3 monster. Use one type-3 potion to defeat it.
  • Find a type-3 potion. Pick it up.
  • Encounter a type-2 monster. Use one type-2 potion to defeat it.
  • Encounter a type-3 monster. Use one type-3 potion to defeat it.
  • Encounter a type-1 monster. Use one type-1 potion to defeat it.

In this sequence of actions, the value of K is 3.

There is no way to avoid defeat with K\leq 2, so the sought value of K _ {\min} is 3. There are multiple sequences of actions that satisfy K=3 and allow him to avoid defeat; you may print any of them.


Sample Input 2

4
2 3
1 4
2 1
1 2

Sample Output 2

-1

He will inevitably be defeated by the first monster he encounters.


Sample Input 3

30
1 25
1 2
1 10
1 18
2 18
1 11
2 11
1 21
1 6
2 2
2 10
1 11
1 24
1 11
1 3
1 2
1 18
2 25
1 8
1 10
1 11
2 18
2 10
1 10
2 2
1 24
1 10
2 10
1 25
2 6

Sample Output 3

4
1 1 1 1 1 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0
F - Bomb Game 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550

問題文

N 人の人が一列に並んでおり、人 i は先頭から i 番目に並んでいます。

以下の操作を、列に並んでいる人が 1 人になるまで繰り返します。

  • 先頭に並んでいる人を \frac{1}{2} の確率で列から取り除き、そうでない場合は列の末尾に移す。

i=1,2,\ldots,N それぞれについて、人 i が最後まで列に並んでいる 1 人になる確率を \text{mod }998244353 で求めて下さい。(取り除くかどうかの選択はすべてランダムかつ独立です。)

確率 \text{mod } 998244353 の定義

この問題で求める確率は必ず有理数になることが証明できます。 また、この問題の制約下では、求める確率を既約分数 \frac{y}{x} で表したときに x998244353 で割り切れないことが保証されます。

このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。

制約

  • 2\leq N\leq 3000
  • 入力は全て整数

入力

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

N 

出力

i=1,2,\ldots,N に対する答えを空白区切りで出力せよ。


入力例 1

2

出力例 1

332748118 665496236

1 が最後まで列に並んでいる 1 人になる確率は \frac{1}{3} です。

2 が最後まで列に並んでいる 1 人になる確率は \frac{2}{3} です。


入力例 2

5

出力例 2

235530465 792768557 258531487 238597268 471060930

Score : 550 points

Problem Statement

There are N people standing in a line, with person i standing at the i-th position from the front.

Repeat the following operation until there is only one person left in the line:

  • Remove the person at the front of the line with a probability of \frac{1}{2}, otherwise move them to the end of the line.

For each person i=1,2,\ldots,N, find the probability that person i is the last person remaining in the line, modulo 998244353. (All choices to remove or not are random and independent.)

How to find probabilities modulo 998244353

The probabilities sought in this problem can be proven to always be a rational number. Furthermore, the constraints of this problem guarantee that if the sought probability is expressed as an irreducible fraction \frac{y}{x}, then x is not divisible by 998244353.

Now, there is a unique integer z between 0 and 998244352, inclusive, such that xz \equiv y \pmod{998244353}. Report this z.

Constraints

  • 2\leq N\leq 3000
  • All input values are integers.

Input

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

N 

Output

Print the answer for people i=1,2,\ldots,N, separated by spaces.


Sample Input 1

2

Sample Output 1

332748118 665496236

Person 1 will be the last person remaining in the line with probability \frac{1}{3}.

Person 2 will be the last person remaining in the line with probability \frac{2}{3}.


Sample Input 2

5

Sample Output 2

235530465 792768557 258531487 238597268 471060930
G - Nearest Fraction

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 625

問題文

1 未満の正実数 r と正整数 N が与えられます。

0\leq p\leq q\leq N かつ \gcd(p,q)=1 を満たす整数の組 (p,q) のうち、\left\vert r-\dfrac pq\right\vert の値を最小にするものを求めてください。

ただし、そのような (p,q) が複数存在する場合、\dfrac pq の値が最も小さいものを出力してください。

制約

  • 0\lt r\lt 1
  • r は十進法で小数点以下たかだか 18 桁の実数として与えられる。
  • 1\leq N\leq 10^{10}
  • N は整数

入力

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

r
N

出力

問題文の条件を満たす (p,q) について pq をこの順に空白区切りで一行に出力せよ。


入力例 1

0.333
33

出力例 1

1 3

\left\vert0.333-\dfrac13\right\vert=\dfrac1{3000} です。 \left\vert0.333-\dfrac pq\right\vert\lt\dfrac1{3000} となる 0\leq p\leq q\leq33,\gcd(p,q)=1 は存在しないので、1 3 を出力してください。


入力例 2

0.45
5

出力例 2

2 5

\left\vert0.45-\dfrac12\right\vert=\left\vert0.45-\dfrac25\right\vert=\dfrac1{20} です。 \left\vert0.45-\dfrac pq\right\vert\lt\dfrac1{20} となる 0\leq p\leq q\leq5,\gcd(p,q)=1 は存在せず、\dfrac12\gt\dfrac25 が成り立つので、2 5 を出力してください。


入力例 3

0.314159265358979323
10000

出力例 3

71 226

\left\vert0.314159265358979323-\dfrac{71}{226}\right\vert=\dfrac{3014435336501}{113000000000000000000} です。


入力例 4

0.007735339533561113
7203576162

出力例 4

34928144 4515398949

Score : 625 points

Problem Statement

You are given a positive real number r less than 1, and a positive integer N.

Among the pair of integers (p,q) such that 0\leq p\leq q\leq N and \gcd(p,q)=1, find the one that minimizes \left\vert r-\dfrac pq\right\vert.

If multiple such pairs (p,q) exist, print the one with the smallest value of \dfrac pq.

Constraints

  • 0\lt r\lt 1
  • r is given as a real number with at most 18 decimal places.
  • 1\leq N\leq 10^{10}
  • N is an integer.

Input

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

r
N

Output

For the pair (p,q) that satisfies the conditions in the problem statement, print p and q in this order, separated by a space, in a single line.


Sample Input 1

0.333
33

Sample Output 1

1 3

\left\vert0.333-\dfrac13\right\vert=\dfrac1{3000}. There is no 0\leq p\leq q\leq33,\gcd(p,q)=1 such that \left\vert0.333-\dfrac pq\right\vert\lt\dfrac1{3000}, so print 1 3.


Sample Input 2

0.45
5

Sample Output 2

2 5

\left\vert0.45-\dfrac12\right\vert=\left\vert0.45-\dfrac25\right\vert=\dfrac1{20}. There is no 0\leq p\leq q\leq5,\gcd(p,q)=1 such that \left\vert0.45-\dfrac pq\right\vert\lt\dfrac1{20}, and we have \dfrac12\gt\dfrac25, so print 2 5.


Sample Input 3

0.314159265358979323
10000

Sample Output 3

71 226

\left\vert0.314159265358979323-\dfrac{71}{226}\right\vert=\dfrac{3014435336501}{113000000000000000000}.


Sample Input 4

0.007735339533561113
7203576162

Sample Output 4

34928144 4515398949