C - Glass and Mug

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

AtCoder 社はグラスマグカップを販売しています。

高橋君は容量が G ml のグラスと、容量が M ml のマグカップを 1 つずつ持っています。
ここで、G,MG<M をみたします。

最初、グラスとマグカップはいずれも空です。
以下の操作を K 回繰り返した後で、グラスとマグカップに水がそれぞれ何 ml ずつ入っているか求めてください。

  • グラスが水で満たされているとき、すなわちグラスにちょうど G ml 入っているとき、グラスの水をすべて捨てる。
  • そうでなく、マグカップが空であるとき、マグカップを水で満たす。
  • 上のいずれでもないとき、マグカップが空になるかグラスが水で満たされるまで、マグカップからグラスに水を移す。

制約

  • 1\leq K\leq 100
  • 1\leq G<M\leq 1000
  • G,M,K は整数

入力

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

K G M

出力

操作を K 回行った後で、グラスとマグカップに水がそれぞれ何 ml ずつ入っているか、この順に空白区切りで出力せよ。


入力例 1

5 300 500

出力例 1

200 500

操作は次の順で行われます。最初、グラスとマグカップはいずれも空です。

  • マグカップを水で満たす。グラスには 0 ml, マグカップには 500 ml 入った状態になる。
  • グラスが満たされるまでマグカップからグラスに水を移す。グラスには 300 ml, マグカップには 200 ml 入った状態になる。
  • グラスの水をすべて捨てる。グラスには 0 ml, マグカップには 200 ml 入った状態になる。
  • マグカップが空になるまでマグカップからグラスに水を移す。グラスには 200 ml, マグカップには 0 ml 入った状態になる。
  • マグカップを水で満たす。グラスには 200 ml, マグカップには 500 ml 入った状態になる。

よって、5 回の操作の後でグラスには 200 ml, マグカップには 500 ml 入った状態になります。 ゆえに、200, 500 を空白区切りでこの順に出力します。


入力例 2

5 100 200

出力例 2

0 0

Score : 200 points

Problem Statement

AtCoder Inc. sells glasses and mugs.

Takahashi has a glass with a capacity of G milliliters and a mug with a capacity of M milliliters.
Here, G<M.

Initially, both the glass and the mug are empty.
After performing the following operation K times, determine how many milliliters of water are in the glass and the mug, respectively.

  • When the glass is filled with water, that is, the glass contains exactly G milliliters of water, discard all the water from the glass.
  • Otherwise, if the mug is empty, fill the mug with water.
  • Otherwise, transfer water from the mug to the glass until the mug is empty or the glass is filled with water.

Constraints

  • 1\leq K\leq 100
  • 1\leq G<M\leq 1000
  • G, M, and K are integers.

Input

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

K G M

Output

Print the amounts, in milliliters, of water in the glass and the mug, in this order, separated by a space, after performing the operation K times.


Sample Input 1

5 300 500

Sample Output 1

200 500

The operation will be performed as follows. Initially, both the glass and the mug are empty.

  • Fill the mug with water. The glass has 0 milliliters, and the mug has 500 milliliters of water.
  • Transfer water from the mug to the glass until the glass is filled. The glass has 300 milliliters, and the mug has 200 milliliters of water.
  • Discard all the water from the glass. The glass has 0 milliliters, and the mug has 200 milliliters of water.
  • Transfer water from the mug to the glass until the mug is empty. The glass has 200 milliliters, and the mug has 0 milliliters of water.
  • Fill the mug with water. The glass has 200 milliliters, and the mug has 500 milliliters of water.

Thus, after five operations, the glass has 200 milliliters, and the mug has 500 milliliters of water. Hence, print 200 and 500 in this order, separated by a space.


Sample Input 2

5 100 200

Sample Output 2

0 0
D - Integer Division

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

-10^{18} 以上 10^{18} 以下の整数 X が与えられるので、\left\lfloor \dfrac{X}{10} \right\rfloor を出力してください。

注記

実数 x に対して、「x 以下の整数の中で最大の整数」を \left\lfloor x \right\rfloor と表します。たとえば \left\lfloor 4.7 \right\rfloor = 4, \left\lfloor -2.4 \right\rfloor = -3, \left\lfloor 5 \right\rfloor = 5 のようになります。(詳しくは入出力例にある説明を参考にしてください。)

制約

  • -10^{18} \leq X \leq 10^{18}
  • 入力はすべて整数である。

入力

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

X

出力

\left\lfloor \frac{X}{10} \right\rfloor を出力せよ。整数として出力する必要があることに注意せよ。


入力例 1

47

出力例 1

4

\frac{47}{10} = 4.7 以下の整数は、すべての負の整数および 0, 1, 2, 3, 4 です。この中で一番大きい整数は 4 なので、\left\lfloor \frac{47}{10} \right\rfloor = 4 となります。


入力例 2

-24

出力例 2

-3

\frac{-24}{10} = -2.4 以下の整数の中で一番大きい整数は -3 なので、 \left\lfloor \frac{-24}{10} \right\rfloor = -3 となります。
-2-2.4 よりも大きいので、条件を満たさないことに注意してください。


入力例 3

50

出力例 3

5

\frac{50}{10} = 5 以下の整数の中で一番大きい整数は 5 自身です。よって \left\lfloor \frac{50}{10} \right\rfloor = 5 となります。


入力例 4

-30

出力例 4

-3

上の例と同様に \left\lfloor \frac{-30}{10} \right\rfloor = -3 となります。


入力例 5

987654321987654321

出力例 5

98765432198765432

答えは 98765432198765432 となります。すべての桁が正しく合っているか確認しましょう。

なお、もしも自分で書いたプログラムが想定通りの挙動をしない場合は、使用しているプログラミング言語の仕様を調べてみることを推奨します。
また、自分の書いたコードがどのように動作するかを確認したい場合は問題文上部の「コードテスト」をご利用ください。

Score : 200 points

Problem Statement

Given an integer X between -10^{18} and 10^{18} (inclusive), print \left\lfloor \dfrac{X}{10} \right\rfloor.

Notes

For a real number x, \left\lfloor x \right\rfloor denotes "the maximum integer not exceeding x". For example, we have \left\lfloor 4.7 \right\rfloor = 4, \left\lfloor -2.4 \right\rfloor = -3, and \left\lfloor 5 \right\rfloor = 5. (For more details, please refer to the description in the Sample Input and Output.)

Constraints

  • -10^{18} \leq X \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print \left\lfloor \frac{X}{10} \right\rfloor. Note that it should be output as an integer.


Sample Input 1

47

Sample Output 1

4

The integers that do not exceed \frac{47}{10} = 4.7 are all the negative integers, 0, 1, 2, 3, and 4. The maximum integer among them is 4, so we have \left\lfloor \frac{47}{10} \right\rfloor = 4.


Sample Input 2

-24

Sample Output 2

-3

Since the maximum integer not exceeding \frac{-24}{10} = -2.4 is -3, we have \left\lfloor \frac{-24}{10} \right\rfloor = -3.
Note that -2 does not satisfy the condition, as -2 exceeds -2.4.


Sample Input 3

50

Sample Output 3

5

The maximum integer that does not exceed \frac{50}{10} = 5 is 5 itself. Thus, we have \left\lfloor \frac{50}{10} \right\rfloor = 5.


Sample Input 4

-30

Sample Output 4

-3

Just like the previous example, \left\lfloor \frac{-30}{10} \right\rfloor = -3.


Sample Input 5

987654321987654321

Sample Output 5

98765432198765432

The answer is 98765432198765432. Make sure that all the digits match.

If your program does not behave as intended, we recommend you checking the specification of the programming language you use.
If you want to check how your code works, you may use "Custom Test" above the Problem Statement.

E - Count xxx

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

英小文字からなる長さ N の文字列 S が与えられます。

S の空でない部分文字列であって、1 種類の文字のみからなるものの数を求めてください。 ただし、文字列として等しい部分文字列同士は、取り出し方が異なっても区別しません

なお、S の空でない部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のうち、長さが 1 以上であるもののことをいいます。 例えば、ababcabc の空でない部分文字列ですが、ac や空文字列は abc の空でない部分文字列ではありません。

制約

  • 1 \leq N \leq 2\times 10^5
  • S は英小文字からなる長さ N の文字列

入力

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

N
S

出力

S の空でない部分文字列であって、1 種類の文字のみからなるものの数を出力せよ。


入力例 1

6
aaabaa

出力例 1

4

S の空でない部分文字列であって、1 種類の文字のみからなるものは a, aa, aaa, b4 つです。 S から aaa を取り出す方法は 1 通りではありませんが、それぞれ 1 回ずつしか数えないことに注意してください。


入力例 2

1
x

出力例 2

1

入力例 3

12
ssskkyskkkky

出力例 3

8

Score : 300 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters.

Find the number of non-empty substrings of S that are repetitions of one character. Here, two substrings that are equal as strings are not distinguished even if they are obtained differently.

A non-empty substring of S is a string of length at least one obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S. For example, ab and abc are non-empty substrings of abc, while ac and the empty string are not.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • S is a string of length N consisting of lowercase English letters.

Input

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

N
S

Output

Print the number of non-empty substrings of S that are repetitions of one character.


Sample Input 1

6
aaabaa

Sample Output 1

4

The non-empty substrings of S that are repetitions of one character are a, aa, aaa, and b; there are four of them. Note that there are multiple ways to obtain a or aa from S, but each should only be counted once.


Sample Input 2

1
x

Sample Output 2

1

Sample Input 3

12
ssskkyskkkky

Sample Output 3

8
F - Make Isomorphic

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

頂点 1, 頂点 2,\ldots, 頂点 NN 個の頂点からなる単純無向グラフ G,H が与えられます。 G には M _ G 本の辺があり、i 本目 (1\leq i\leq M _ G) の辺は頂点 u _ i と頂点 v _ i を結んでいます。 H には M _ H 本の辺があり、i 本目 (1\leq i\leq M _ H) の辺は頂点 a _ i と頂点 b _ i を結んでいます。

あなたは、グラフ H に対して次の操作を 0 回以上の好きな回数繰り返すことができます。

  • 1\leq i\lt j\leq N を満たす整数の組 (i,j) をひとつ選ぶ。A _ {i,j} 円を支払って、頂点 i と頂点 j を結ぶ辺がなければ追加し、あれば取り除く。

GH を同型にするために少なくとも何円支払う必要があるか求めてください。

単純無向グラフとは?

単純無向グラフとは、自己ループや多重辺を含まず、辺に向きの無いグラフのことをいいます。

グラフの同型とは?

N 頂点のグラフ GH同型であるとは、ある (1,2,\ldots,N) の並べ替え (P _ 1,P _ 2,\ldots,P _ N) が存在して、どの 1\leq i\lt j\leq N に対しても

  • G に頂点 i, 頂点 j を結ぶ辺が存在するとき、かつそのときに限り H に頂点 P _ i, 頂点 P _ j を結ぶ辺が存在する
が成り立つことをいいます。

制約

  • 1\leq N\leq8
  • 0\leq M _ G\leq\dfrac{N(N-1)}2
  • 0\leq M _ H\leq\dfrac{N(N-1)}2
  • 1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M _ G)
  • (u _ i,v _ i)\neq(u _ j,v _ j)\ (1\leq i\lt j\leq M _ G)
  • 1\leq a _ i\lt b _ i\leq N\ (1\leq i\leq M _ H)
  • (a _ i,b _ i)\neq(a _ j,b _ j)\ (1\leq i\lt j\leq M _ H)
  • 1\leq A _ {i,j}\leq 10 ^ 6\ (1\leq i\lt j\leq N)
  • 入力はすべて整数

入力

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

N
M _ G
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ {M _ G} v _ {M _ G}
M _ H
a _ 1 b _ 1
a _ 2 b _ 2
\vdots
a _ {M _ H} b _ {M _ H}
A _ {1,2} A _ {1,3} \ldots A _ {1,N}
A _ {2,3} \ldots A _ {2,N}
\vdots
A _ {N-1,N}

出力

答えを出力せよ。


入力例 1

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

出力例 1

9

与えられたグラフはそれぞれ以下のようになります。

たとえば、H に対して次のような 4 つの操作を順に行うことで、9 円を支払ってGH を同型にすることができます。

  • (i,j)=(1,3) として操作を行う。H には頂点 1 と頂点 3 を結ぶ辺があるので、1 円を支払ってこれを取り除く。
  • (i,j)=(2,5) として操作を行う。H には頂点 2 と頂点 5 を結ぶ辺はないので、2 円を支払ってこれを追加する。
  • (i,j)=(1,5) として操作を行う。H には頂点 1 と頂点 5 を結ぶ辺があるので、1 円を支払ってこれを取り除く。
  • (i,j)=(3,5) として操作を行う。H には頂点 3 と頂点 5 を結ぶ辺はないので、5 円を支払ってこれを追加する。

操作の結果、H は以下のようになります。

支払う金額を 8 円以下にして GH を同型にすることはできないため、9 を出力してください。


入力例 2

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

出力例 2

3

たとえば、(i,j)=(2,3),(2,4),(3,4) とした 3 回の操作を行うことで GH を同型にすることができます。


入力例 3

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

出力例 3

5

たとえば、(i,j)=(4,5) とした 1 回の操作を行うことで GH を同型にすることができます。


入力例 4

2
0
0
371

出力例 4

0

GH には辺が含まれていないこともあります。 また、一度も操作をする必要がないこともあります。


入力例 5

8
13
1 8
5 7
4 6
1 5
7 8
1 6
1 2
5 8
2 6
5 6
6 7
3 7
4 8
15
3 5
1 7
4 6
3 8
7 8
1 2
5 6
1 6
1 5
1 4
2 8
2 6
2 4
4 7
1 3
7483 1694 5868 3296 9723 5299 4326
5195 4088 5871 1384 2491 6562
1149 6326 2996 9845 7557
4041 7720 1554 5060
8329 8541 3530
4652 3874
3748

出力例 5

21214

Score : 300 points

Problem Statement

You are given simple undirected graphs G and H, each with N vertices: vertices 1, 2, \ldots, N. Graph G has M_G edges, and its i-th edge (1\leq i\leq M_G) connects vertices u_i and v_i. Graph H has M_H edges, and its i-th edge (1\leq i\leq M_H) connects vertices a_i and b_i.

You can perform the following operation on graph H any number of times, possibly zero.

  • Choose a pair of integers (i,j) satisfying 1\leq i<j\leq N. Pay A_{i,j} yen, and if there is no edge between vertices i and j in H, add one; if there is, remove it.

Find the minimum total cost required to make G and H isomorphic.

What is a simple undirected graph?

A simple undirected graph is a graph without self-loops or multi-edges, where edges have no direction.

What does it mean for graphs to be isomorphic?

Two graphs G and H with N vertices are isomorphic if and only if there exists a permutation (P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) such that for all 1\leq i\lt j\leq N:

  • an edge exists between vertices i and j in G if and only if an edge exists between vertices P_i and P_j in H.

Constraints

  • 1\leq N\leq8
  • 0\leq M _ G\leq\dfrac{N(N-1)}2
  • 0\leq M _ H\leq\dfrac{N(N-1)}2
  • 1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M _ G)
  • (u _ i,v _ i)\neq(u _ j,v _ j)\ (1\leq i\lt j\leq M _ G)
  • 1\leq a _ i\lt b _ i\leq N\ (1\leq i\leq M _ H)
  • (a _ i,b _ i)\neq(a _ j,b _ j)\ (1\leq i\lt j\leq M _ H)
  • 1\leq A _ {i,j}\leq 10 ^ 6\ (1\leq i\lt j\leq N)
  • All input values are integers.

Input

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

N
M _ G
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ {M _ G} v _ {M _ G}
M _ H
a _ 1 b _ 1
a _ 2 b _ 2
\vdots
a _ {M _ H} b _ {M _ H}
A _ {1,2} A _ {1,3} \ldots A _ {1,N}
A _ {2,3} \ldots A _ {2,N}
\vdots
A _ {N-1,N}

Output

Print the answer.


Sample Input 1

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

Sample Output 1

9

The given graphs are as follows:

For example, you can perform the following four operations on H to make it isomorphic to G at a cost of 9 yen.

  • Choose (i,j)=(1,3). There is an edge between vertices 1 and 3 in H, so pay 1 yen to remove it.
  • Choose (i,j)=(2,5). There is no edge between vertices 2 and 5 in H, so pay 2 yen to add it.
  • Choose (i,j)=(1,5). There is an edge between vertices 1 and 5 in H, so pay 1 yen to remove it.
  • Choose (i,j)=(3,5). There is no edge between vertices 3 and 5 in H, so pay 5 yen to add it.

After these operations, H becomes:

You cannot make G and H isomorphic at a cost less than 9 yen, so print 9.


Sample Input 2

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

Sample Output 2

3

For example, performing the operations (i,j)=(2,3),(2,4),(3,4) on H will make it isomorphic to G.


Sample Input 3

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

Sample Output 3

5

For example, performing the operation (i,j)=(4,5) once will make G and H isomorphic.


Sample Input 4

2
0
0
371

Sample Output 4

0

Note that G and H may have no edges.

Also, it is possible that no operations are needed.


Sample Input 5

8
13
1 8
5 7
4 6
1 5
7 8
1 6
1 2
5 8
2 6
5 6
6 7
3 7
4 8
15
3 5
1 7
4 6
3 8
7 8
1 2
5 6
1 6
1 5
1 4
2 8
2 6
2 4
4 7
1 3
7483 1694 5868 3296 9723 5299 4326
5195 4088 5871 1384 2491 6562
1149 6326 2996 9845 7557
4041 7720 1554 5060
8329 8541 3530
4652 3874
3748

Sample Output 5

21214
G - ABC Puzzle

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

整数 NA, B, C からなる長さ N の文字列 R,C が与えられるので、以下の問題を解いてください。

N \times N のマス目があり、最初全てのマスは空きマスです。
各マスに A, B, C のうち高々 1 文字を書き込みます。( 空きマスのままにすることも許されます )

以下の条件を全て満たすことが可能であるか判定し、可能であれば書き込み方を 1 つ出力して下さい。

  • 各行 / 各列 に A, B, C がちょうど 1 個ずつ含まれる
  • i 行目に書かれた文字の中で最も左にある文字は Ri 文字目と一致する
  • i 列目に書かれた文字の中で最も上にある文字は Ci 文字目と一致する

制約

  • N3 以上 5 以下の整数
  • R,CA, B, C からなる長さ N の文字列

入力

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

N
R
C

出力

問題文中の条件を満たす書き込み方が存在しない場合、 No1 行に出力してください。
そうでない場合、以下の形式に従い書き込み方を出力してください。

Yes
A_1
A_2
\vdots
A_N

まず、 1 行目に Yes と出力してください。 続く N 行のうちの i 行目に、長さ N の文字列である A_i を出力してください。

  • A_ij 文字目が . であるとき、上から i 行目、左から j 列目のマスは空きマスであることを示します。
  • A_ij 文字目が A であるとき、上から i 行目、左から j 列目のマスに A が書き込まれたことを示します。
  • A_ij 文字目が B であるとき、上から i 行目、左から j 列目のマスに B が書き込まれたことを示します。
  • A_ij 文字目が C であるとき、上から i 行目、左から j 列目のマスに C が書き込まれたことを示します。

正しい書き込み方が複数存在する場合、どれを出力しても構いません。


入力例 1

5
ABCBC
ACAAB

出力例 1

Yes
AC..B
.BA.C
C.BA.
BA.C.
..CBA

出力例のマス目は以下の条件を全て満たすため、正解として扱われます。

  • 全ての行に A, B, C がちょうど 1 個ずつ含まれる
  • 全ての列に A, B, C がちょうど 1 個ずつ含まれる
  • 各行に書かれた最も左の文字は上から順に A, B, C, B, C である
  • 各列に書かれた最も上の文字は左から順に A, C, A, A, B である

入力例 2

3
AAA
BBB

出力例 2

No

この入力では、条件を満たす書き込み方は存在しません。

Score : 450 points

Problem Statement

You are given an integer N and strings R and C of length N consisting of A, B, and C. Solve the following problem.

There is a N \times N grid. All cells are initially empty.
You can write at most one character from A, B, and C in each cell. (You can also leave the cell empty.)

Determine if it is possible to satisfy all of the following conditions, and if it is possible, print one way to do so.

  • Each row and each column contain exactly one A, one B, and one C.
  • The leftmost character written in the i-th row matches the i-th character of R.
  • The topmost character written in the i-th column matches the i-th character of C.

Constraints

  • N is an integer between 3 and 5, inclusive.
  • R and C are strings of length N consisting of A, B, and C.

Input

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

N
R
C

Output

If there is no way to fill the grid to satisfy the conditions in the problem statement, print No in one line.
Otherwise, print one such way to fill the grid in the following format:

Yes
A_1
A_2
\vdots
A_N

The first line should contain Yes. The i-th of the subsequent N lines should contain a string A_i of length N.

  • If the j-th character of A_i is ., it indicates that the cell in the i-th row from the top and the j-th column from the left is empty.
  • If the j-th character of A_i is A, it indicates that A is written in the cell in the i-th row from the top and the j-th column from the left.
  • If the j-th character of A_i is B, it indicates that B is written in the cell in the i-th row from the top and the j-th column from the left.
  • If the j-th character of A_i is C, it indicates that C is written in the cell in the i-th row from the top and the j-th column from the left.

If there are multiple correct ways to fill the grid, you may print any of them.


Sample Input 1

5
ABCBC
ACAAB

Sample Output 1

Yes
AC..B
.BA.C
C.BA.
BA.C.
..CBA

The grid in the output example satisfies all the following conditions, so it will be treated as correct.

  • Each row contains exactly one A, one B, and one C.
  • Each column contains exactly one A, one B, and one C.
  • The leftmost characters written in the rows are A, B, C, B, C from top to bottom.
  • The topmost characters written in the columns are A, C, A, A, B from left to right.

Sample Input 2

3
AAA
BBB

Sample Output 2

No

For this input, there is no way to fill the grid to satisfy the conditions.