A - Digits Sum

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

高橋君は 2 つの正の整数 AB を持っています。

それらの和が N であると分かっているとき、 A の各位の和と B の各位の和の合計として考えられる最小の値を求めてください。

制約

  • 2 ≦ N ≦ 10^5
  • N は整数

入力

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

N

出力

A の各位の和と B の各位の和の合計として考えられる最小の値を出力せよ。


入力例 1

15

出力例 1

6

A=2,B=13 の場合、それぞれの各位の和は 2,4 となり、求める値が最小となります。


入力例 2

100000

出力例 2

10

Score : 200 points

Problem Statement

Takahashi has two positive integers A and B.

It is known that A plus B equals N. Find the minimum possible value of "the sum of the digits of A" plus "the sum of the digits of B" (in base 10).

Constraints

  • 2 ≤ N ≤ 10^5
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the minimum possible value of "the sum of the digits of A" plus "the sum of the digits of B".


Sample Input 1

15

Sample Output 1

6

When A=2 and B=13, the sums of their digits are 2 and 4, which minimizes the value in question.


Sample Input 2

100000

Sample Output 2

10
B - RGB Coloring

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

高橋君はタワーを 1 つ持っており、それは N 個のブロックが縦一列に重なって構成されています。 はじめすべてのブロックは無色ですが、高橋君はいくつかのブロックを赤色、緑色、青色のいずれかの色で塗ることで、 タワーを美しくしようとしています。そこで、高橋君は タワーの美しさ を以下のように定義することにしました。

  • 各ブロックの得点を、赤色に塗られていれば A 点、緑色に塗られていれば A+B 点、青色に塗られていれば B 点、無色ならば 0 点として、 N 個のブロックの得点の合計をタワーの美しさとする。

ただし、A,B はあらかじめ与えられる正整数の定数であり、各マスが 2 つ以上の色で同時に塗られることがないことにも注意してください。

高橋君はタワーの美しさがちょうど K になるようにブロックを塗ろうと考えています。 そのようにタワーを塗る方法は何通りあるでしょうか。 998244353 で割った余りを求めてください。 ただし、2 つのタワーを塗る方法が異なるとは、あるブロックが存在し、そのブロックに塗られている色が異なること、もしくは、そのブロックが一方では塗られているが、 他方では無色であることを指します。

制約

  • 1 ≦ N ≦ 3×10^5
  • 1 ≦ A,B ≦ 3×10^5
  • 0 ≦ K ≦ 18×10^{10}
  • 入力される値は全て整数である

入力

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

N A B K

出力

タワーを塗る方法の個数を 998244353 で割った余りを出力せよ。


入力例 1

4 1 2 5

出力例 1

40

この場合、赤色 1 つにつき 1 点、緑色 1 つにつき 3 点、青色 1 つにつき 2 点なので、美しさが 5 になるのは、

  • 緑色 1 つ、青色 1
  • 赤色 1 つ、青色 2
  • 赤色 2 つ、緑色 1
  • 赤色 3 つ、青色 1

のいずれかの場合だけです。よって、求める答えは 40 になります。


入力例 2

2 5 6 0

出力例 2

1

美しさが 0 であるタワーは、すべてのブロックが無色であるものだけです。よって、答えは 1 になります。


入力例 3

90081 33447 90629 6391049189

出力例 3

577742975

Score : 700 points

Problem Statement

Takahashi has a tower which is divided into N layers. Initially, all the layers are uncolored. Takahashi is going to paint some of the layers in red, green or blue to make a beautiful tower. He defines the beauty of the tower as follows:

  • The beauty of the tower is the sum of the scores of the N layers, where the score of a layer is A if the layer is painted red, A+B if the layer is painted green, B if the layer is painted blue, and 0 if the layer is uncolored.

Here, A and B are positive integer constants given beforehand. Also note that a layer may not be painted in two or more colors.

Takahashi is planning to paint the tower so that the beauty of the tower becomes exactly K. How many such ways are there to paint the tower? Find the count modulo 998244353. Two ways to paint the tower are considered different when there exists a layer that is painted in different colors, or a layer that is painted in some color in one of the ways and not in the other.

Constraints

  • 1 ≤ N ≤ 3×10^5
  • 1 ≤ A,B ≤ 3×10^5
  • 0 ≤ K ≤ 18×10^{10}
  • All values in the input are integers.

Input

Input is given from Standard Input in the following format:

N A B K

Output

Print the number of the ways to paint tiles, modulo 998244353.


Sample Input 1

4 1 2 5

Sample Output 1

40

In this case, a red layer worth 1 points, a green layer worth 3 points and the blue layer worth 2 points. The beauty of the tower is 5 when we have one of the following sets of painted layers:

  • 1 green, 1 blue
  • 1 red, 2 blues
  • 2 reds, 1 green
  • 3 reds, 1 blue

The total number of the ways to produce them is 40.


Sample Input 2

2 5 6 0

Sample Output 2

1

The beauty of the tower is 0 only when all the layers are uncolored. Thus, the answer is 1.


Sample Input 3

90081 33447 90629 6391049189

Sample Output 3

577742975
C - Interval Game

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

高橋君と青木君は数直線と区間を使ってゲームをすることにしました。 高橋君は数直線上に立っており、最初は座標 0 にいます。 また、青木君は N 個の区間を持っており、i 個目の区間は [L_i,R_i]、つまり座標が L_i 以上 R_i 以下の点からなる区間となっています。

このゲームは N 回のステップからなります。i ステップ目では以下の手順を踏みます。

  • まず青木君は、N 個の区間の内、まだ選んでいない区間を一つ選び、その区間を高橋君に伝える。
  • 次に高橋君は、青木君が今回選んだ区間に入るように、数直線上を移動する。

N 回のステップを終えた後、高橋君が座標 0 まで戻ることでゲームは終了します。

高橋君がゲーム全体を通して移動する距離の合計を K としたとき、青木君は K ができるだけ大きくなるように区間を選び、 高橋君は K ができるだけ小さくなるように移動します。 このとき、最終的に高橋君の移動距離の合計 K はいくつになるでしょうか。

制約

  • 1 ≦ N ≦ 10^5
  • -10^5 ≦ L_i < R_i ≦ 10^5
  • L_iR_i は整数

入力

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

N
L_1 R_1
:
L_N R_N

出力

高橋君と青木君が上記の条件に従って行動するときの高橋君が動く距離の合計を、1 つの整数値として出力せよ。 ただし、L_i,R_i が整数であるとき、K が整数となることは保証されている。


入力例 1

3
-5 1
3 7
-4 -2

出力例 1

10

高橋君と青木君の行動の一例は以下のようになります。

  • 青木君が 1 番目の区間を選び、高橋君は座標 0 から座標 -4 まで距離 4 だけ移動する。
  • 青木君が 3 番目の区間を選び、高橋君は座標 -4 のまま動かない
  • 青木君が 2 番目の区間を選び、高橋君は座標 -4 から座標 3 まで距離 7 だけ移動する。
  • 高橋君は座標 3 から座標 0 まで距離 3 だけ移動する。

このとき高橋君の移動距離の合計は 14 になってしまうので、高橋君の行動は最適ではないですが、 動き方を変えることで、移動距離の合計を 10 にすることができます。


入力例 2

3
1 2
3 4
5 6

出力例 2

12

入力例 3

5
-2 0
-2 0
7 8
9 10
-2 -1

出力例 3

34

Score : 700 points

Problem Statement

Takahashi and Aoki will play a game with a number line and some segments. Takahashi is standing on the number line and he is initially at coordinate 0. Aoki has N segments. The i-th segment is [L_i,R_i], that is, a segment consisting of points with coordinates between L_i and R_i (inclusive).

The game has N steps. The i-th step proceeds as follows:

  • First, Aoki chooses a segment that is still not chosen yet from the N segments and tells it to Takahashi.
  • Then, Takahashi walks along the number line to some point within the segment chosen by Aoki this time.

After N steps are performed, Takahashi will return to coordinate 0 and the game ends.

Let K be the total distance traveled by Takahashi throughout the game. Aoki will choose segments so that K will be as large as possible, and Takahashi walks along the line so that K will be as small as possible. What will be the value of K in the end?

Constraints

  • 1 ≤ N ≤ 10^5
  • -10^5 ≤ L_i < R_i ≤ 10^5
  • L_i and R_i are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 R_1
:
L_N R_N

Output

Print the total distance traveled by Takahashi throughout the game when Takahashi and Aoki acts as above. It is guaranteed that K is always an integer when L_i,R_i are integers.


Sample Input 1

3
-5 1
3 7
-4 -2

Sample Output 1

10

One possible sequence of actions of Takahashi and Aoki is as follows:

  • Aoki chooses the first segment. Takahashi moves from coordinate 0 to -4, covering a distance of 4.
  • Aoki chooses the third segment. Takahashi stays at coordinate -4.
  • Aoki chooses the second segment. Takahashi moves from coordinate -4 to 3, covering a distance of 7.
  • Takahashi moves from coordinate 3 to 0, covering a distance of 3.

The distance covered by Takahashi here is 14 (because Takahashi didn't move optimally). It turns out that if both players move optimally, the distance covered by Takahashi will be 10.


Sample Input 2

3
1 2
3 4
5 6

Sample Output 2

12

Sample Input 3

5
-2 0
-2 0
7 8
9 10
-2 -1

Sample Output 3

34
D - Choosing Points

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

高橋君は平面上の点集合について研究しています。 高橋君にとって、座標平面上の点の集合 Sいい集合 であるとは、S が以下の条件をともに満たすことを指します。

  • S に属するどの 2 点間の距離も \sqrt{D_1} でない。
  • S に属するどの 2 点間の距離も \sqrt{D_2} でない。

ただし、D_1,D_2 は高橋君の定めた正整数の定数です。

ここで、X を座標平面上の格子点 (i,j) であって 0 ≦ i,j < 2N を満たす点 (i,j) 全体からなる集合としましょう。 研究者の高橋君は、D_1,D_2 をどのように選んでも、X からうまく N^2 個の点を選ぶことで、それらがいい集合をなすことを示しました。 しかし、実際にどのように選べばいい集合になるかは分かっていません。 そこで、高橋君の代わりに、X のサイズ N^2 の部分集合であって、いい集合となるものを見つけてください。

制約

  • 1 ≦ N ≦ 300
  • 1 ≦ D_1 ≦ 2×10^5
  • 1 ≦ D_2 ≦ 2×10^5
  • 入力される値は全て整数である

入力

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

N D_1 D_2

出力

条件を満たすように選んだ相異なる N^2 個の点を以下の形式で出力せよ。

x_1 y_1
x_2 y_2
:
x_{N^2} y_{N^2}

ただし、(x_i,y_i) は選んだ点の i 番目の点を表し、x_i,y_i は整数かつ 0 ≦ x_i,y_i < 2N を満たす必要がある。 また、点はどのような順番で出力しても構わず、解が複数ある場合は、いかなるものを出力しても構わないことに注意せよ。


入力例 1

2 1 2

出力例 1

0 0
0 2
2 0
2 2

この場合 2 点間の距離としてありうる値は 22\sqrt{2} のみであるから、確かに条件を満たします。


入力例 2

3 1 5

出力例 2

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

Score : 800 points

Problem Statement

Takahashi is doing a research on sets of points in a plane. Takahashi thinks a set S of points in a coordinate plane is a good set when S satisfies both of the following conditions:

  • The distance between any two points in S is not \sqrt{D_1}.
  • The distance between any two points in S is not \sqrt{D_2}.

Here, D_1 and D_2 are positive integer constants that Takahashi specified.

Let X be a set of points (i,j) on a coordinate plane where i and j are integers and satisfy 0 ≤ i,j < 2N.

Takahashi has proved that, for any choice of D_1 and D_2, there exists a way to choose N^2 points from X so that the chosen points form a good set. However, he does not know the specific way to choose such points to form a good set. Find a subset of X whose size is N^2 that forms a good set.

Constraints

  • 1 ≤ N ≤ 300
  • 1 ≤ D_1 ≤ 2×10^5
  • 1 ≤ D_2 ≤ 2×10^5
  • All values in the input are integers.

Input

Input is given from Standard Input in the following format:

N D_1 D_2

Output

Print N^2 distinct points that satisfy the condition in the following format:

x_1 y_1
x_2 y_2
:
x_{N^2} y_{N^2}

Here, (x_i,y_i) represents the i-th chosen point. 0 ≤ x_i,y_i < 2N must hold, and they must be integers. The chosen points may be printed in any order. In case there are multiple possible solutions, you can output any.


Sample Input 1

2 1 2

Sample Output 1

0 0
0 2
2 0
2 2

Among these points, the distance between 2 points is either 2 or 2\sqrt{2}, thus it satisfies the condition.


Sample Input 2

3 1 5

Sample Output 2

0 0
0 2
0 4
1 1
1 3
1 5
2 0
2 2
2 4
E - Walking on a Tree

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1500

問題文

高橋君は木上を散歩するのが大好きです。 高橋君が散歩をする木は N 頂点からなり、各頂点には 1 から N の番号が割り振られています。 また、N-1 本の辺のうち、i 本目の辺は頂点 a_i と頂点 b_i を結んでいます。

高橋君は M 回の散歩の予定を組みました。i 回目の散歩は以下のようにして行う予定です。

  • 2 つの頂点 u_i,v_i が決まっている。
  • 高橋君は頂点 u_i から頂点 v_i、または、頂点 v_i から頂点 u_i に向かって、同じ辺は一度しか通らないように移動する。

また、i 回目の散歩で得られる 楽しさ は以下のように定義されています。

  • i 回目の散歩で通った辺であって、以下のいずれかの条件を満たすものの個数
    • 今回の散歩で初めて通った辺
    • 今まで通ったことがあっても、今回とは逆向きでしか通っていなかった辺

高橋君は M 回の散歩の楽しさの合計が最大になるように、各散歩の向きを決めたいです。 そこで、高橋君が得られる楽しさの合計の最大値と、実際に楽しさの合計が最大となる各散歩の向きの定め方の一例を求めてください。

制約

  • 1 ≦ N,M ≦ 2000
  • 1 ≦ a_i , b_i ≦ N
  • 1 ≦ u_i , v_i ≦ N
  • a_i \neq b_i
  • u_i \neq v_i
  • 入力で与えられるグラフは木である。

入力

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

N M
a_1 b_1
:
a_{N-1} b_{N-1}
u_1 v_1
:
u_M v_M

出力

高橋君が得られる楽しさの合計の最大値 T と、実際に楽しさの合計が最大となる各散歩の向きの定め方の一例を、以下の形式に従って出力せよ。

T
u'_1 v'_1
:
u'_M v'_M

ただし、(u'_i,v'_i) は (u_i,v_i) または (v_i,u_i) であり、i 回目の散歩で u'_i から v'_i に向かって移動することを表している。


入力例 1

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

出力例 1

6
2 3
3 4
4 2

上のように散歩の向きを定めると、各散歩で 2 ずつ楽しさを得ることができ、合計の楽しさが 6 になります。


入力例 2

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

出力例 2

6
2 4
3 5
5 1

入力例 3

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

出力例 3

9
2 4
6 3
5 6
4 5

Score : 1500 points

Problem Statement

Takahashi loves walking on a tree. The tree where Takahashi walks has N vertices numbered 1 through N. The i-th of the N-1 edges connects Vertex a_i and Vertex b_i.

Takahashi has scheduled M walks. The i-th walk is done as follows:

  • The walk involves two vertices u_i and v_i that are fixed beforehand.
  • Takahashi will walk from u_i to v_i or from v_i to u_i along the shortest path.

The happiness gained from the i-th walk is defined as follows:

  • The happiness gained is the number of the edges traversed during the i-th walk that satisfies one of the following conditions:
    • In the previous walks, the edge has never been traversed.
    • In the previous walks, the edge has only been traversed in the direction opposite to the direction taken in the i-th walk.

Takahashi would like to decide the direction of each walk so that the total happiness gained from the M walks is maximized. Find the maximum total happiness that can be gained, and one specific way to choose the directions of the walks that maximizes the total happiness.

Constraints

  • 1 ≤ N,M ≤ 2000
  • 1 ≤ a_i , b_i ≤ N
  • 1 ≤ u_i , v_i ≤ N
  • a_i \neq b_i
  • u_i \neq v_i
  • The graph given as input is a tree.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_{N-1} b_{N-1}
u_1 v_1
:
u_M v_M

Output

Print the maximum total happiness T that can be gained, and one specific way to choose the directions of the walks that maximizes the total happiness, in the following format:

T
u^'_1 v^'_1
:
u^'_M v^'_M

where (u^'_i,v^'_i) is either (u_i,v_i) or (v_i,u_i), which means that the i-th walk is from vertex u^'_i to v^'_i.


Sample Input 1

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

Sample Output 1

6
2 3
3 4
4 2

If we decide the directions of the walks as above, he gains the happiness of 2 in each walk, and the total happiness will be 6.


Sample Input 2

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

Sample Output 2

6
2 4
3 5
5 1

Sample Input 3

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

Sample Output 3

9
2 4
6 3
5 6
4 5
F - Addition and Andition

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 2400

問題文

高橋君と青木君は計算をするのが大好きです。 そこで、二人は計算をして遊ぶことにしました。

まず二人は正整数を 1 つずつ用意しました。高橋君の用意した数は X、青木君の用意した数は Y です。 そして、以下の手順を K 回繰り返すことで、計算を楽しむことにしました。

  • 高橋君の持っている数と青木君の持っている数の bitwise AND を計算し、それを Z とする。
  • そして、高橋君と青木君の持っている数それぞれに Z を足す。

しかし、計算の大好きな二人にもこの計算は酷です。 そこで彼らの代わりに、高橋君が最終的に持つことになる数と、青木君が最終的に持つことになる数を求めてあげてください。

ただし、入出力は 2 進数表記で行われることに注意してください。 特に、X,Y はそれぞれ長さ N,M0,1 のみからなる文字列 S,T として入力され、S,T の先頭の文字は 1 であることが保証されています。

制約

  • 1 ≦ K ≦ 10^6
  • 1 ≦ N,M ≦ 10^6
  • S,T の先頭の文字は 1 である。

入力

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

N M K
S
T

出力

一行目には高橋君の最終的に持つことになる数を、二行目には青木君の最終的に持つことになる数を出力せよ。 ただし、それらを 2 進数表記で表し、先頭の文字が 1 であるような 0,1 のみからなる文字列として出力せよ。


入力例 1

2 3 3
11
101

出力例 1

10000
10010

各操作後の X,Y の値は以下のようになります。

  • 一回目の操作後は (X,Y)=(4,6) になる。
  • 二回目の操作後は (X,Y)=(8,10) になる。
  • 三回目の操作後は (X,Y)=(16,18) になる。

入力例 2

5 8 3
10101
10101001

出力例 2

100000
10110100

入力例 3

10 10 10
1100110011
1011001101

出力例 3

10000100000010001000
10000100000000100010

Score : 2400 points

Problem Statement

Takahashi and Aoki love calculating things, so they will play with numbers now.

First, they came up with one positive integer each. Takahashi came up with X, and Aoki came up with Y. Then, they will enjoy themselves by repeating the following operation K times:

  • Compute the bitwise AND of the number currently kept by Takahashi and the number currently kept by Aoki. Let Z be the result.
  • Then, add Z to both of the numbers kept by Takahashi and Aoki.

However, it turns out that even for the two math maniacs this is just too much work. Could you find the number that would be kept by Takahashi and the one that would be kept by Aoki eventually?

Note that input and output are done in binary. Especially, X and Y are given as strings S and T of length N and M consisting of 0 and 1, respectively, whose initial characters are guaranteed to be 1.

Constraints

  • 1 ≤ K ≤ 10^6
  • 1 ≤ N,M ≤ 10^6
  • The initial characters of S and T are 1.

Input

Input is given from Standard Input in the following format:

N M K
S
T

Output

In the first line, print the number that would be kept by Takahashi eventually; in the second line, print the number that would be kept by Aoki eventually. Those numbers should be represented in binary and printed as strings consisting of 0 and 1 that begin with 1.


Sample Input 1

2 3 3
11
101

Sample Output 1

10000
10010

The values of X and Y after each operation are as follows:

  • After the first operation: (X,Y)=(4,6).
  • After the second operation: (X,Y)=(8,10).
  • After the third operation: (X,Y)=(16,18).

Sample Input 2

5 8 3
10101
10101001

Sample Output 2

100000
10110100

Sample Input 3

10 10 10
1100110011
1011001101

Sample Output 3

10000100000010001000
10000100000000100010