Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は、レジ打ちの仕事をしています。
レジの機械には 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 の 11 個のボタンがあります。
レジの機械には、はじめ 0 が表示されています。
ボタン 00 を押すと、表示されている数が 100 倍されます。
それ以外のボタンを押すと、表示されている数が 10 倍されたあとに、押されたボタンに書かれている数が加算されます。
高橋君は、レジに整数 S を表示させたいです。 レジに S が表示されている状態にするためには、少なくとも何回ボタンを押す必要があるか求めてください。
制約
- 1\leq S\leq 10^{100000}
- S は整数
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを 1 行で出力せよ。
入力例 1
40004
出力例 1
4
例えば、次のように操作することでボタンを 4 回押して 40004 を表示させることができます。 はじめ、レジには 0 が表示されています。
- ボタン
4を押す。レジに表示されている数は 4 となる。 - ボタン
00を押す。レジに表示されている数は 400 となる。 - ボタン
0を押す。レジに表示されている数は 4000 となる。 - ボタン
4を押す。レジに表示されている数は 40004 となる。
3 回までボタンを押すことでレジに 40004 を表示させることはできないので、出力すべき値は 4 です。
入力例 2
1355506027
出力例 2
10
入力例 3
10888869450418352160768000001
出力例 3
27
S は 64\operatorname{bit} 整数に収まらない場合があることに注意してください。
Score : 300 points
Problem Statement
Takahashi is a cashier.
There is a cash register with 11 keys: 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
The cash register initially displays 0.
Whenever he types the key 00, the displayed number is multiplied by 100;
whenever he types one of the others, the displayed number is multiplied by 10, and then added by the number written on the key.
Takahashi wants the cash register to display an integer S. At least how many keystrokes are required to make it display S?
Constraints
- 1\leq S\leq 10^{100000}
- S is an integer.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer in a line.
Sample Input 1
40004
Sample Output 1
4
For example, the following four keystrokes make the cash register display 40004. Initially, the cash register displays 0.
- Type the key
4. It now displays 4. - Type the key
00. It now displays 400. - Type the key
0. It now displays 4000. - Type the key
4. It now displays 40004.
He cannot make it display 40004 with three or fewer keystrokes, so 4 should be printed.
Sample Input 2
1355506027
Sample Output 2
10
Sample Input 3
10888869450418352160768000001
Sample Output 3
27
Note that S may not fit into a 64-\operatorname{bit} integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 本の導火線を一直線に接着したものがあります。左から i 本目の導火線は長さが A_i cm で、 1 秒あたり B_i cm の一定の速さで燃えます。
この導火線の左端と右端から同時に火をつけるとき、 2 つの火がぶつかる場所が着火前の導火線の左端から何 cm の地点か求めてください。
制約
- 1 \leq N \leq 10^5
- 1 \leq A_i,B_i \leq 1000
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
2 つの火がぶつかる場所が着火前の導火線の左端から何 cm の地点か(単位を除いて)出力せよ。
想定解答との絶対誤差または相対誤差が 10^{-5} 以下であれば正解として扱われる。
入力例 1
3 1 1 2 1 3 1
出力例 1
3.000000000000000
着火前の導火線の左端から 3 cm の地点で 2 つの火がぶつかります。
入力例 2
3 1 3 2 2 3 1
出力例 2
3.833333333333333
入力例 3
5 3 9 1 2 4 6 1 5 5 3
出力例 3
8.916666666666668
Score : 300 points
Problem Statement
We have N fuses connected in series. The i-th fuse from the left has a length of A_i centimeters and burns at a constant speed of B_i centimeters per second.
Consider igniting this object from left and right ends simultaneously. Find the distance between the position where the two flames will meet and the left end of the object.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq A_i,B_i \leq 1000
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Print the distance between the position where the two flames will meet and the left end of the object, in centimeters (print just the number).
Your output will be considered correct when its absolute or relative error from our answer is at most 10^{-5}.
Sample Input 1
3 1 1 2 1 3 1
Sample Output 1
3.000000000000000
The two flames will meet at 3 centimeters from the left end of the object.
Sample Input 2
3 1 3 2 2 3 1
Sample Output 2
3.833333333333333
Sample Input 3
5 3 9 1 2 4 6 1 5 5 3
Sample Output 3
8.916666666666668
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
整数 N と A, B, C からなる長さ N の文字列 R,C が与えられるので、以下の問題を解いてください。
N \times N のマス目があり、最初全てのマスは空きマスです。
各マスに A, B, C のうち高々 1 文字を書き込みます。( 空きマスのままにすることも許されます )
以下の条件を全て満たすことが可能であるか判定し、可能であれば書き込み方を 1 つ出力して下さい。
- 各行 / 各列 に
A,B,Cがちょうど 1 個ずつ含まれる - i 行目に書かれた文字の中で最も左にある文字は R の i 文字目と一致する
- i 列目に書かれた文字の中で最も上にある文字は C の i 文字目と一致する
制約
- N は 3 以上 5 以下の整数
- R,C は
A,B,Cからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N R C
出力
問題文中の条件を満たす書き込み方が存在しない場合、 No と 1 行に出力してください。
そうでない場合、以下の形式に従い書き込み方を出力してください。
Yes A_1 A_2 \vdots A_N
まず、 1 行目に Yes と出力してください。
続く N 行のうちの i 行目に、長さ N の文字列である A_i を出力してください。
- A_i の j 文字目が
.であるとき、上から i 行目、左から j 列目のマスは空きマスであることを示します。 - A_i の j 文字目が
Aであるとき、上から i 行目、左から j 列目のマスにAが書き込まれたことを示します。 - A_i の j 文字目が
Bであるとき、上から i 行目、左から j 列目のマスにBが書き込まれたことを示します。 - A_i の j 文字目が
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, oneB, and oneC. - 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, andC.
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 thatAis 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 thatBis 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 thatCis 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, oneB, and oneC. - Each column contains exactly one
A, oneB, and oneC. - The leftmost characters written in the rows are
A,B,C,B,Cfrom top to bottom. - The topmost characters written in the columns are
A,C,A,A,Bfrom 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.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
この問題は問題 G と似た設定です。問題文の相違点を赤字で示します。
H 行 W 列のグリッドがあり、グリッドの各マスは赤色あるいは緑色に塗られています。
グリッドの上から i 行目、左から j 列目のマスをマス (i,j) と表記します。
マス (i,j) の色は文字 S_{i,j} で表され、S_{i,j} = . のときマス (i,j) は赤色、S_{i,j} = # のときマス (i,j) は緑色に塗られています。
グリッドにおいて、緑色に塗られたマスを頂点集合、隣り合った 2 つの緑色のマスを結ぶ辺全体を辺集合としたグラフにおける連結成分の個数を 緑の連結成分数 と呼びます。ただし、2 つのマス (x,y) と (x',y') が隣り合っているとは、|x-x'| + |y-y'| = 1 であることを指します。
赤色に塗られたマスを一様ランダムに 1 つ選び、緑色に塗り替えたとき、塗り替え後のグリッドの緑の連結成分数の期待値を \text{mod } 998244353 で出力してください。
「期待値を \text{mod } 998244353 で出力」とは
求める期待値は必ず有理数となることが証明できます。 またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、 R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ 1 つ存在することが証明できます。この R を出力してください。制約
- 1 \leq H,W \leq 1000
- S_{i,j} =
.または S_{i,j} =# - S_{i,j} =
.なる (i,j) が存在する。
入力
入力は以下の形式で標準入力から与えられる。
H W
S_{1,1}S_{1,2}\ldotsS_{1,W}
S_{2,1}S_{2,2}\ldotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\ldotsS_{H,W}
出力
答えを出力せよ。
入力例 1
3 3 ##. #.# #..
出力例 1
499122178
マス (1,3) を緑色に塗り替えたとき、緑の連結成分数は 1 となります。
マス (2,2) を緑色に塗り替えたとき、緑の連結成分数は 1 となります。
マス (3,2) を緑色に塗り替えたとき、緑の連結成分数は 2 となります。
マス (3,3) を緑色に塗り替えたとき、緑の連結成分数は 2 となります。
よって、赤色に塗られたマスを一様ランダムに 1 つ選び、緑色に塗り替えた後の緑の連結成分数の期待値は (1+1+2+2)/4 = 3/2 となります。
入力例 2
4 5 ..#.. .###. ##### ..#..
出力例 2
598946613
入力例 3
3 4 #... .#.# ..##
出力例 3
285212675
Score : 450 points
Problem Statement
This problem has a similar setting to Problem G. Differences in the problem statement are indicated in red.
There is a grid with H rows and W columns, where each cell is painted red or green.
Let (i,j) denote the cell in the i-th row from the top and the j-th column from the left.
The color of cell (i,j) is represented by the character S_{i,j}, where S_{i,j} = . means cell (i,j) is red, and S_{i,j} = # means cell (i,j) is green.
The number of green connected components in the grid is the number of connected components in the graph with the vertex set being the green cells and the edge set being the edges connecting two adjacent green cells. Here, two cells (x,y) and (x',y') are considered adjacent when |x-x'| + |y-y'| = 1.
Consider choosing one red cell uniformly at random and repainting it green. Print the expected value of the number of green connected components in the grid after repainting, modulo 998244353.
What does "print the expected value modulo 998244353" mean?
It can be proved that the sought expected value is always rational. Furthermore, the constraints of this problem guarantee that if that value is expressed as \frac{P}{Q} using two coprime integers P and Q, there is exactly one integer R such that R \times Q \equiv P \pmod{998244353} and 0 \leq R < 998244353. Print this R.Constraints
- 1 \leq H,W \leq 1000
- S_{i,j} =
.or S_{i,j} =#. - There is at least one (i,j) such that S_{i,j} =
..
Input
The input is given from Standard Input in the following format:
H W
S_{1,1}S_{1,2}\ldotsS_{1,W}
S_{2,1}S_{2,2}\ldotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\ldotsS_{H,W}
Output
Print the answer.
Sample Input 1
3 3 ##. #.# #..
Sample Output 1
499122178
If cell (1,3) is repainted green, the number of green connected components becomes 1.
If cell (2,2) is repainted green, the number of green connected components becomes 1.
If cell (3,2) is repainted green, the number of green connected components becomes 2.
If cell (3,3) is repainted green, the number of green connected components becomes 2.
Therefore, the expected value of the number of green connected components after choosing one red cell uniformly at random and repainting it green is (1+1+2+2)/4 = 3/2.
Sample Input 2
4 5 ..#.. .###. ##### ..#..
Sample Output 2
598946613
Sample Input 3
3 4 #... .#.# ..##
Sample Output 3
285212675
Time Limit: 2.5 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
非負整数列 A=(a_1,\ldots,a_N) が与えられます。
A に対して次の操作をちょうど 1 回行います。
- 非負整数 x を選ぶ。そして、i=1,\ldots,N すべてに対し、a_i の値を「a_i と x のビット単位 xor」に置き換える。
操作後の A に含まれる値の最大値を M とします。M の最小値を求めてください。
ビット単位 xor とは
非負整数 A, B のビット単位 xor 、A \oplus B は、以下のように定義されます。- A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
制約
- 1 \leq N \leq 1.5 \times 10^5
- 0 \leq a_i \lt 2^{30}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N a_1 \ldots a_N
出力
答えを出力せよ。
入力例 1
3 12 18 11
出力例 1
16
x=2 として操作をすると、操作後の数列は (12 \oplus 2,18 \oplus 2, 11 \oplus 2) = (14,16,9) となり、最大値 M は 16 となります。
M を 16 より小さくすることは出来ないため、この値が答えです。
入力例 2
10 0 0 0 0 0 0 0 0 0 0
出力例 2
0
入力例 3
5 324097321 555675086 304655177 991244276 9980291
出力例 3
805306368
Score : 500 points
Problem Statement
You are given a sequence of non-negative integers A=(a_1,\ldots,a_N).
Let us perform the following operation on A just once.
- Choose a non-negative integer x. Then, for every i=1, \ldots, N, replace the value of a_i with the bitwise XOR of a_i and x.
Let M be the maximum value in A after the operation. Find the minimum possible value of M.
What is bitwise XOR?
The bitwise XOR of non-negative integers A and B, A \oplus B, is defined as follows.- When A \oplus B is written in binary, the k-th lowest bit (k \geq 0) is 1 if exactly one of the k-th lowest bits of A and B is 1, and 0 otherwise.
Constraints
- 1 \leq N \leq 1.5 \times 10^5
- 0 \leq a_i \lt 2^{30}
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N a_1 \ldots a_N
Output
Print the answer.
Sample Input 1
3 12 18 11
Sample Output 1
16
If we do the operation with x=2, the sequence becomes (12 \oplus 2,18 \oplus 2, 11 \oplus 2) = (14,16,9), where the maximum value M is 16.
We cannot make M smaller than 16, so this value is the answer.
Sample Input 2
10 0 0 0 0 0 0 0 0 0 0
Sample Output 2
0
Sample Input 3
5 324097321 555675086 304655177 991244276 9980291
Sample Output 3
805306368