実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
AtCoder社のオフィスは H 行 W 列のマス目で表されます。上から i 行目、左から j 列目のマスを (i, j) と表します。
各マスの状態は文字 S_{i,j} で表され、 S_{i,j} が # のときそのマスは壁、. のときそのマスは床、H のときそのマスは加湿器が置かれた床です。
あるマスは、ある加湿器から壁を通らず上下左右に D 回以下の移動で辿り着ける場合加湿されます。ここで、加湿器が置かれた床のマスは必ず加湿されていることに注意してください。
加湿されている床のマスの個数を求めてください。
制約
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- 0 \leq D \leq H\times W
- S_{i,j} は
#または.またはHである (1 \leq i \leq H, 1 \leq j \leq W) - 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
H W D
S_{1,1}S_{1,2}\cdotsS_{1,W}
S_{2,1}S_{2,2}\cdotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\cdotsS_{H,W}
出力
答えを出力せよ。
入力例 1
3 4 1 H... #..H .#.#
出力例 1
5
マス (1,1),(1,2),(1,4),(2,3),(2,4) の 5 つのマスが加湿されています。
入力例 2
5 6 2 ##...H H..... ..H.#. .HH... .###..
出力例 2
21
入力例 3
1 6 3 ...#..
出力例 3
0
加湿されるマスが存在しない場合もあります。
Score : 350 points
Problem Statement
The AtCoder company office is represented as a grid of H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left.
The state of each cell is represented by a character S_{i,j}. If S_{i,j} is #, that cell has a wall; if S_{i,j} is ., that cell is a floor; if S_{i,j} is H, that cell has a humidifier placed on a floor cell.
A certain cell is considered humidified if it can be reached from at least one humidifier cell by at most D moves up, down, left, or right without passing through a wall. Note that any cell with a humidifier is always humidified.
Find the number of humidified floor cells.
Constraints
- 1 \leq H \leq 1000
- 1 \leq W \leq 1000
- 0 \leq D \leq H\times W
- S_{i,j} is
#,., orH. (1 \leq i \leq H, 1 \leq j \leq W) - All input numbers are integers.
Input
The input is given from Standard Input in the following format:
H W D
S_{1,1}S_{1,2}\cdotsS_{1,W}
S_{2,1}S_{2,2}\cdotsS_{2,W}
\vdots
S_{H,1}S_{H,2}\cdotsS_{H,W}
Output
Print the answer.
Sample Input 1
3 4 1 H... #..H .#.#
Sample Output 1
5
Five cells (1,1), (1,2), (1,4), (2,3), (2,4) are humidified.
Sample Input 2
5 6 2 ##...H H..... ..H.#. .HH... .###..
Sample Output 2
21
Sample Input 3
1 6 3 ...#..
Sample Output 3
0
It is possible that no cells are humidified.
実行時間制限: 2 sec / メモリ制限: 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.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
長さ N の数列 A=(A_1,A_2,\ldots,A_N) と、整数 K が与えられます。
A の連続部分列のうち、要素の和が K になるものはいくつありますか?
すなわち、以下の条件を全て満たす整数の組 (l,r) はいくつありますか?
- 1\leq l\leq r\leq N
- \displaystyle\sum_{i=l}^{r}A_i = K
制約
- 1\leq N \leq 2\times 10^5
- |A_i| \leq 10^9
- |K| \leq 10^{15}
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
6 5 8 -3 5 7 0 -4
出力例 1
3
(l,r)=(1,2),(3,3),(2,6) の 3 組が条件を満たします。
入力例 2
2 -1000000000000000 1000000000 -1000000000
出力例 2
0
条件を満たす (l,r) の組が 1 つも存在しないこともあります。
Score : 400 points
Problem Statement
Given is a sequence of length N: A=(A_1,A_2,\ldots,A_N), and an integer K.
How many of the contiguous subsequences of A have the sum of K?
In other words, how many pairs of integers (l,r) satisfy all of the conditions below?
- 1\leq l\leq r\leq N
- \displaystyle\sum_{i=l}^{r}A_i = K
Constraints
- 1\leq N \leq 2\times 10^5
- |A_i| \leq 10^9
- |K| \leq 10^{15}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
6 5 8 -3 5 7 0 -4
Sample Output 1
3
(l,r)=(1,2),(3,3),(2,6) are the three pairs that satisfy the conditions.
Sample Input 2
2 -1000000000000000 1000000000 -1000000000
Sample Output 2
0
There may be no pair that satisfies the conditions.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
高橋くんは秘密の整数列 a を持っており、現時点で、a の長さが N であることは分かっています。
a の中身を当てたいあなたに対し、高橋くんは以下の Q 個の情報を追加で与えてくれることを約束しました。
- i\ (1 \leq i \leq Q) 個目の情報: a_{l_i}+a_{l_i+1}+\cdots+a_{r_i} の値
高橋くんが約束を守り、Q 個の情報すべてが与えられた場合、a に含まれる全要素の総和 a_1+a_2+\cdots+a_N を特定することは可能ですか?
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq \min(2 \times 10^5,\frac{N(N+1)}{2})
- 1 \leq l_i \leq r_i \leq N
- (l_i,r_i) \neq (l_j,r_j)\ (i \neq j)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q
l_1 r_1
l_2 r_2
\hspace{0.4cm}\vdots
l_Q r_Q
出力
a に含まれる全要素の総和を特定することが可能なら Yes を、そうでないなら No を出力せよ。
入力例 1
3 3 1 2 2 3 2 2
出力例 1
Yes
1 個目の情報と 2 個目の情報から、a_1+a_2+a_2+a_3 の値が分かります。そこから 3 個目の情報によって得られる a_2 の値を引くと、a_1+a_2+a_3 の値を特定可能です。
入力例 2
4 3 1 3 1 2 2 3
出力例 2
No
a の先頭 3 項の総和を特定することは可能ですが、全要素の総和を特定することは不可能です。
入力例 3
4 4 1 1 2 2 3 3 1 4
出力例 3
Yes
4 個目の情報によって全要素の総和が直接与えられています。
Score : 500 points
Problem Statement
Takahashi has a secret integer sequence a. You know that the length of a is N.
You want to guess the contents of a. He has promised to give you the following Q additional pieces of information.
- The i-th information: the value a_{l_i}+a_{l_i+1}+\cdots+a_{r_i}.
Is it possible to determine the sum of all elements in a, a_1+a_2+\cdots+a_N, if the Q pieces of promised information are given?
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq Q \leq \min(2 \times 10^5,\frac{N(N+1)}{2})
- 1 \leq l_i \leq r_i \leq N
- (l_i,r_i) \neq (l_j,r_j)\ (i \neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
l_1 r_1
l_2 r_2
\hspace{0.4cm}\vdots
l_Q r_Q
Output
If it is possible to determine the sum of all elements in a, print Yes; otherwise, print No.
Sample Input 1
3 3 1 2 2 3 2 2
Sample Output 1
Yes
From the first and second information, we can find the value a_1+a_2+a_2+a_3. By subtracting the value of a_2 from it, we can determine the value a_1+a_2+a_3.
Sample Input 2
4 3 1 3 1 2 2 3
Sample Output 2
No
We can determine the sum of the first 3 elements of a, but not the sum of all elements.
Sample Input 3
4 4 1 1 2 2 3 3 1 4
Sample Output 3
Yes
The fourth information directly gives us the sum of all elements.
実行時間制限: 2.5 sec / メモリ制限: 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