A - Div

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

N 個の互いに区別できないお菓子を、A君とB君で分け合います。 両者とも 1 個以上の整数個のお菓子を得るような分け方は何通りありますか?

制約

  • N は整数
  • 1 \leq N \leq 15

入力

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

N

出力

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


入力例 1

2

出力例 1

1

A君が 1 個、B君が 1 個取る方法のみ存在します。


入力例 2

1

出力例 2

0

入力例 3

3

出力例 3

2

Score : 100 points

Problem Statement

Two boys, A and B, will share N indistinguishable sweets. How many ways are there to do this so that each boy gets a positive integer number of sweets?

Constraints

  • N is an integer.
  • 1 \leq N \leq 15

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer as an integer.


Sample Input 1

2

Sample Output 1

1

There is only one way to share the sweets: A and B get one sweet each.


Sample Input 2

1

Sample Output 2

0

Sample Input 3

3

Sample Output 3

2
B - Palindrome with leading zeros

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

整数 N が与えられます。

N を十進法で表した文字列の先頭に 0 個以上の 0 をつけることで、回文にすることはできますか?

制約

  • 0 \leq N \leq 10^9

入力

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

N

出力

回文にできるなら Yes、できないなら No を出力せよ。


入力例 1

1210

出力例 1

Yes

1210 の先頭に 1 個の 0 をつけると 01210 となり回文になります。


入力例 2

777

出力例 2

Yes

777 はもともと回文です。


入力例 3

123456789

出力例 3

No

Score : 200 points

Problem Statement

Given is an integer N.

Is it possible to add zero or more 0s at the beginning of the string representing N in base ten to get a palindrome?

Constraints

  • 0 \leq N \leq 10^9

Input

Input is given from Standard Input in the following format:

N

Output

If a palindrome can be made, print Yes; otherwise, print No.


Sample Input 1

1210

Sample Output 1

Yes

Adding one 0 at the beginning of 1210 results in 01210, a palindrome.


Sample Input 2

777

Sample Output 2

Yes

777 is already a palindrome.


Sample Input 3

123456789

Sample Output 3

No
C - Compass Walking

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

2 次元平面上の原点に高橋君がいます。

高橋君が 1 歩歩くと、いまいる点からのユークリッド距離がちょうど R であるような点に移動することができます(移動先の座標が整数である必要はありません)。これ以外の方法で移動することはできません。

高橋君が点 (X,Y) に到達するまでに必要な歩数の最小値を求めてください。

なお、点 (x_1,y_1) と点 (x_2,y_2) のユークリッド距離は \sqrt{(x_1-x_2)^2+(y_1-y_2)^2} で与えられます。

制約

  • 1 \leq R \leq 10^5
  • 0 \leq X,Y \leq 10^5
  • (X,Y) \neq (0,0)
  • 入力は全て整数

入力

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

R X Y

出力

高橋君が (X,Y) に到達するまでに必要な歩数の最小値を出力せよ。


入力例 1

5 15 0

出力例 1

3

(0,0) \to (5,0) \to (10,0) \to (15,0)3 歩で到達できます。 2 歩以下で到達することはできないのでこれが最小です。

図1


入力例 2

5 11 0

出力例 2

3

例えば (0,0) \to (5,0) \to (8,4) \to (11,0) と移動すれば良いです。

図2


入力例 3

3 4 4

出力例 3

2

例えば (0,0) \to (2-\frac{\sqrt{2}}{2}, 2+\frac{\sqrt{2}}{2}) \to (4,4) と移動すれば良いです。

図3

Score : 300 points

Problem Statement

Takahashi is standing at the origin of a two-dimensional plane.

By taking one step, he can move to a point whose Euclidian distance from his current position is exactly R (the coordinates of the destination of a move do not have to be integers). There is no other way to move.

Find the minimum number of steps Takahashi has to take before reaching (X, Y).

We remind you that the Euclidian distance between points (x_1,y_1) and (x_2,y_2) is \sqrt{(x_1-x_2)^2+(y_1-y_2)^2}.

Constraints

  • 1 \leq R \leq 10^5
  • 0 \leq X,Y \leq 10^5
  • (X,Y) \neq (0,0)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

R X Y

Output

Print the minimum number of steps Takahashi has to take before reaching (X, Y).


Sample Input 1

5 15 0

Sample Output 1

3

He can reach there in three steps: (0,0) \to (5,0) \to (10,0) \to (15,0). This is the minimum number needed: he cannot reach there in two or fewer steps.

Figure 1


Sample Input 2

5 11 0

Sample Output 2

3

One optimal route is (0,0) \to (5,0) \to (8,4) \to (11,0).

Figure 2


Sample Input 3

3 4 4

Sample Output 3

2

One optimal route is (0,0) \to (2-\frac{\sqrt{2}}{2}, 2+\frac{\sqrt{2}}{2}) \to (4,4).

Figure 3

D - Send More Money

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 400

問題文

英小文字からなる文字列 S_1,S_2,S_3 が与えられます。覆面算 S_1+S_2=S_3 を解いてください。

正確には、次の 3 つの条件をすべて満たすような正の整数の組 N_1,N_2,N_3 が存在するか判定し、存在するならばそのうち 1 つを求めてください。
ここで、N_1, N_2, N_3 を (先頭に余分な 0 をつけないで) 十進表記した文字列をそれぞれ N'_1, N'_2, N'_3 とします。

  • N'_i の文字数は、S_i の文字数に等しい
  • N_1+N_2=N_3 を満たす
  • S_ix 文字目と S_jy 文字目が等しいとき、またその時に限り、N'_ix 文字目と N'_jy 文字目が等しい

制約

  • S_1,S_2,S_3 は英小文字のみからなる長さ 1 以上 10 以下の文字列

入力

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

S_1
S_2
S_3

出力

条件を満たすような正整数の組 N_1,N_2,N_3 が存在するならば、そのような組の 1 つを改行区切りで出力せよ。 存在しないなら、代わりに UNSOLVABLE と出力せよ。


入力例 1

a
b
c

出力例 1

1
2
3

(N_1, N_2, N_3) = (4,5,9) などを出力しても正解となります。(1,1,2)3 つ目の条件を満たしていない (a,b がともに 1 に対応している) ため、不正解となります。


入力例 2

x
x
y

出力例 2

1
1
2

(N_1, N_2, N_3) = (3,3,6) などを出力しても正解となります。(1,2,3)3 つ目の条件を満たしていない (1,2 がともに x に対応している) ため、不正解となります。


入力例 3

p
q
p

出力例 3

UNSOLVABLE

入力例 4

abcd
efgh
ijkl

出力例 4

UNSOLVABLE

入力例 5

send
more
money

出力例 5

9567
1085
10652

Score : 400 points

Problem Statement

Given strings S_1,S_2,S_3 consisting of lowercase English letters, solve the alphametic S_1+S_2=S_3.

Formally, determine whether there is a triple of positive integers N_1, N_2, N_3 satisfying all of the three conditions below, and find one such triple if it exists.
Here, N'_1, N'_2, N'_3 are strings representing N_1, N_2, N_3 (without leading zeros) in base ten, respectively.

  • N'_i and S_i have the same number of characters.
  • N_1+N_2=N_3.
  • The x-th character of S_i and the y-th character of S_j is the same if and only if the x-th character of N'_i and the y-th character of N'_j are the same.

Constraints

  • Each of S_1, S_2, S_3 is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S_1
S_2
S_3

Output

If there is a triple of positive integers N_1, N_2, N_3 satisfying the conditions, print one such triple, using newline as a separator. Otherwise, print UNSOLVABLE instead.


Sample Input 1

a
b
c

Sample Output 1

1
2
3

Outputs such as (N_1, N_2, N_3) = (4,5,9) will also be accepted, but (1,1,2) will not since it violates the third condition (both a and b correspond to 1).


Sample Input 2

x
x
y

Sample Output 2

1
1
2

Outputs such as (N_1, N_2, N_3) = (3,3,6) will also be accepted, but (1,2,3) will not since it violates the third condition (both 1 and 2 correspond to x).


Sample Input 3

p
q
p

Sample Output 3

UNSOLVABLE

Sample Input 4

abcd
efgh
ijkl

Sample Output 4

UNSOLVABLE

Sample Input 5

send
more
money

Sample Output 5

9567
1085
10652
E - Unique Color

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点からなる木が与えられます。i 番目の辺は頂点 A_i と頂点 B_i をつないでいます。頂点 i は色 C_i で塗られています (この問題では、色は整数として表されます)。

頂点 1 から頂点 x への最短パスに、頂点 x と同じ色で塗られた頂点が頂点 x 以外に存在しないとき、頂点 xよい頂点 であるといいます。

よい頂点を全て求めてください。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq C_i \leq 10^5
  • 1 \leq A_i,B_i \leq N
  • 与えられるグラフは木である
  • 入力は全て整数

入力

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

N
C_1 \ldots C_N
A_1 B_1
\vdots
A_{N-1} B_{N-1}

出力

全てのよい頂点の番号を、昇順に改行区切りで出力せよ。


入力例 1

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

出力例 1

1
2
3
4
6

例えば頂点 1 から頂点 6 への最短パスには頂点 1,2,3,6 が含まれます。これらの中に、頂点 6 と同じ色の頂点は頂点 6 以外存在しないので、頂点 6 はよい頂点です。
一方で、頂点 1 から頂点 5 への最短パスには頂点 1, 2, 5 が含まれ、頂点 1 と頂点 5 の色は同じであるため、頂点 5 はよい頂点ではありません。


入力例 2

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

出力例 2

1
2
3
5
6
7
8

Score : 500 points

Problem Statement

Given is a tree with N vertices numbered 1 through N. The i-th edge connects Vertex A_i and Vertex B_i. Vertex i is painted in color C_i (in this problem, colors are represented as integers).

Vertex x is said to be good when the shortest path from Vertex 1 to Vertex x does not contain a vertex painted in the same color as Vertex x, except Vertex x itself.

Find all good vertices.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq C_i \leq 10^5
  • 1 \leq A_i,B_i \leq N
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 \ldots C_N
A_1 B_1
\vdots
A_{N-1} B_{N-1}

Output

Print all good vertices as integers, in ascending order, using newline as a separator.


Sample Input 1

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

Sample Output 1

1
2
3
4
6

For example, the shortest path from Vertex 1 to Vertex 6 contains Vertices 1,2,3,6. Among them, only Vertex 6 itself is painted in the same color as Vertex 6, so it is a good vertex.
On the other hand, the shortest path from Vertex 1 to Vertex 5 contains Vertices 1,2,5, and Vertex 1 is painted in the same color as Vertex 5, so Vertex 5 is not a good vertex.


Sample Input 2

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

Sample Output 2

1
2
3
5
6
7
8
F - Cube

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

立方体の各面に 1 つずつ正の整数を書きます。書かれた 6 つの数の和が S になるような書き込み方は何通りありますか?

ただし、立方体を回転した時に一致するような書き込み方は区別しないものとします(数に向きはありません)。

答えは非常に大きくなる可能性があるので、998244353 で割ったあまりを求めてください。

制約

  • 6 \leq S \leq 10^{18}
  • S は整数

入力

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

S

出力

答えを 998244353 で割った余りを出力せよ。


入力例 1

8

出力例 1

3

書かれた 6 つの数が (1,1,1,1,1,3) であるような書き込み方が 1 通り、(1,1,1,1,2,2) であるような書き込み方が 2 通り (2 が書かれた面が隣り合うものと反対側に配置されるもの) の、計 3 通りの書き込み方があります。


入力例 2

9

出力例 2

5

入力例 3

50

出力例 3

80132

入力例 4

10000000000

出力例 4

2239716

答えを 998244353 で割ったあまりを求めてください。

Score : 600 points

Problem Statement

Let us write a positive integer on each face of a cube. How many ways are there to do this so that the sum of the six numbers written is S?

Here, two ways to write numbers are not distinguished when they only differ by rotation. (Numbers have no direction.)

The count can be enormous, so find it modulo 998244353.

Constraints

  • 6 \leq S \leq 10^{18}
  • S is an integer.

Input

Input is given from Standard Input in the following format:

S

Output

Print the count modulo 998244353.


Sample Input 1

8

Sample Output 1

3

We have one way to write 1,1,1,1,1,3 on the cube and two ways to write 1,1,1,1,2,2 (one where we write 2 on adjacent faces and another where we write 2 on opposite faces), for a total of three ways.


Sample Input 2

9

Sample Output 2

5

Sample Input 3

50

Sample Output 3

80132

Sample Input 4

10000000000

Sample Output 4

2239716

Find the count modulo 998244353.