Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字のみからなる文字列 S が与えられます。
S の各文字を英大文字に変換して得られる文字列 T を出力してください。
制約
- S は英小文字のみからなる、長さが 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
T を出力せよ。
入力例 1
abc
出力例 1
ABC
abc の各文字を英大文字に変換すると ABC になります。
入力例 2
a
出力例 2
A
入力例 3
abcdefghjiklnmoqprstvuwxyz
出力例 3
ABCDEFGHJIKLNMOQPRSTVUWXYZ
Score : 100 points
Problem Statement
You are given a string S consisting of lowercase English letters.
Uppercase each character of S and print the resulting string T.
Constraints
- S is a string consisting of lowercase English letters whose length is between 1 and 100, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print T.
Sample Input 1
abc
Sample Output 1
ABC
Uppercase each character of abc, and you have ABC.
Sample Input 2
a
Sample Output 2
A
Sample Input 3
abcdefghjiklnmoqprstvuwxyz
Sample Output 3
ABCDEFGHJIKLNMOQPRSTVUWXYZ
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字と . のみからなる文字列 S が与えられます。
S を . で分割したときの末尾の文字列を出力してください。
すなわち、. を含まない S の接尾辞のうち最長のものを出力してください。
制約
- S は英小文字と
.からなる、長さ 2 以上 100 以下の文字列 - S には
.が 1 つ以上含まれる - S の末尾は
.ではない
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
atcoder.jp
出力例 1
jp
atcoder.jp の、 . を含まない最長の接尾辞は jp です。
入力例 2
translate.google.com
出力例 2
com
S に . が複数含まれることもあります。
入力例 3
.z
出力例 3
z
S が . から始まることもあります。
入力例 4
..........txt
出力例 4
txt
S 中で . が連続することもあります。
Score: 100 points
Problem Statement
You are given a string S consisting of lowercase English letters and the character ..
Print the last substring when S is split by .s.
In other words, print the longest suffix of S that does not contain ..
Constraints
- S is a string of length between 2 and 100, inclusive, consisting of lowercase English letters and
.. - S contains at least one
.. - S does not end with
..
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
atcoder.jp
Sample Output 1
jp
The longest suffix of atcoder.jp that does not contain . is jp.
Sample Input 2
translate.google.com
Sample Output 2
com
S may contain multiple .s.
Sample Input 3
.z
Sample Output 3
z
S may start with ..
Sample Input 4
..........txt
Sample Output 4
txt
S may contain consecutive .s.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
1,2,\dots,N の番号が付いた N 個の箱があります。最初は全ての箱が空です。
これから Q 個のボールが順番にやってきます。
高橋君は、数列 X=(X_1,X_2,\dots,X_Q) に従ってボールを箱に入れます。
具体的には、 i 番目にやってきたボールに次の処理を行います。
- X_i \ge 1 である場合 : このボールを、箱 X_i に入れる。
- X_i = 0 である場合 : このボールを、現在入っているボールが最も少ない箱のうち番号が最小である箱に入れる。
それぞれのボールをどの箱に入れたかを求めてください。
制約
- 入力は全て整数
- 1 \le N \le 100
- 1 \le Q \le 100
- 0 \le X_i \le N
入力
入力は以下の形式で標準入力から与えられる。
N Q X_1 X_2 \dots X_Q
出力
i 番目にやってきたボールを箱 B_i に入れたとき、次の形式に従って出力せよ。
B_1 B_2 \dots B_Q
入力例 1
4 5 2 0 3 0 0
出力例 1
2 1 3 4 1
箱が 4 つあり、ボールは 5 個やってきます。
- 最初、全ての箱は空です。
- 各箱に入っているボールの数は箱 1 から順に 0,0,0,0 個です。
- X_1=2 なので、 1 番目にやってきたボールを箱 2 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 0,1,0,0 個となります。
- X_2=0 なので、 2 番目にやってきたボールを現在入っているボールが最も少ない箱のうち番号が最小である箱である箱 1 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 1,1,0,0 個となります。
- X_3=3 なので、 3 番目にやってきたボールを箱 3 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 1,1,1,0 個となります。
- X_4=0 なので、 4 番目にやってきたボールを現在入っているボールが最も少ない箱のうち番号が最小である箱である箱 4 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 1,1,1,1 個となります。
- X_5=0 なので、 5 番目にやってきたボールを現在入っているボールが最も少ない箱のうち番号が最小である箱である箱 1 に入れます。
- 各箱に入っているボールの数は箱 1 から順に 2,1,1,1 個となります。
各ボールを、やってきた順に箱 2,1,3,4,1 に入れました。よって、 2 1 3 4 1 と出力します。
入力例 2
3 7 1 1 0 0 0 0 0
出力例 2
1 1 2 3 2 3 1
入力例 3
6 20 4 6 0 3 4 2 6 5 2 3 0 3 2 5 0 3 5 0 2 0
出力例 3
4 6 1 3 4 2 6 5 2 3 1 3 2 5 1 3 5 4 2 6
Score : 200 points
Problem Statement
There are N boxes numbered 1,2,\dots,N. Initially, all boxes are empty.
Q balls will come in order.
Takahashi will put the balls into the boxes according to the sequence X=(X_1,X_2,\dots,X_Q).
Specifically, he performs the following process for the i-th ball:
- If X_i \ge 1: Put this ball into box X_i.
- If X_i = 0: Put this ball into the box with the smallest number among those containing the fewest balls.
Find which box each ball was put into.
Constraints
- All input values are integers.
- 1 \le N \le 100
- 1 \le Q \le 100
- 0 \le X_i \le N
Input
The input is given from Standard Input in the following format:
N Q X_1 X_2 \dots X_Q
Output
If the i-th ball was put into box B_i, output in the following format:
B_1 B_2 \dots B_Q
Sample Input 1
4 5 2 0 3 0 0
Sample Output 1
2 1 3 4 1
There are 4 boxes, and 5 balls come.
- Initially, all boxes are empty.
- The numbers of balls in box 1,2,3,4 are 0,0,0,0, respectively.
- Since X_1=2, put the 1st ball into box 2.
- The numbers of balls in box 1,2,3,4 are 0,1,0,0, respectively.
- Since X_2=0, put the 2nd ball into box 1, which has the smallest number among those containing the fewest balls.
- The numbers of balls in box 1,2,3,4 are 1,1,0,0, respectively.
- Since X_3=3, put the 3rd ball into box 3.
- The numbers of balls in box 1,2,3,4 are 1,1,1,0, respectively.
- Since X_4=0, put the 4th ball into box 4, which has the smallest number among those containing the fewest balls.
- The numbers of balls in box 1,2,3,4 are 1,1,1,1, respectively.
- Since X_5=0, put the 5th ball into box 1, which has the smallest number among those containing the fewest balls.
- The numbers of balls in box 1,2,3,4 are 2,1,1,1, respectively.
The balls were put into boxes 2,1,3,4,1 in order. Thus, output 2 1 3 4 1.
Sample Input 2
3 7 1 1 0 0 0 0 0
Sample Output 2
1 1 2 3 2 3 1
Sample Input 3
6 20 4 6 0 3 4 2 6 5 2 3 0 3 2 5 0 3 5 0 2 0
Sample Output 3
4 6 1 3 4 2 6 5 2 3 1 3 2 5 1 3 5 4 2 6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は野球をモチーフにしたゲームを作ろうとしましたが、うまくコードが書けなくて困っています。
高橋君の代わりに次の問題を解くプログラムを作ってください。
マス 0, マス 1, マス 2, マス 3 の 4 つのマス目があります。はじめマスの上には何もありません。
また、整数 P があり、はじめ P = 0 です。
正の整数からなる数列 A = (A_1, A_2, \dots, A_N) が与えられるので、i = 1, 2, \dots, N について順番に次の操作を行います。
- マス 0 に駒を 1 個置く。
- マス上のすべての駒を番号が A_i 大きいマスに進める。言い換えると、駒がマス x にあればその駒をマス x + A_i に移動する。
ただし移動先のマスが存在しない (すなわち x + A_i が 4 以上になる) 駒たちに関しては、それらを取り除いて P に取り除いた個数を加算する。
すべての操作を行った後の P の値を出力してください。
制約
- 1 \leq N \leq 100
- 1 \leq A_i \leq 4
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
操作終了時点での P の値を出力せよ。
入力例 1
4 1 1 3 2
出力例 1
3
操作を説明すると次のようになり、操作終了時点での P の値は 3 になります。
- i=1 での操作
- マス 0 に駒を置く。この時点でマス 0 にコマが乗っている。
- すべての駒を 1 大きいマスに進める。移動を終えた時点でマス 1 に駒が乗っている。
- i=2 での操作
- マス 0 に駒を置く。この時点でマス 0, 1 にコマが乗っている。
- すべての駒を 1 大きいマスに進める。移動を終えた時点でマス 1, 2 に駒が乗っている。
- i=3 での操作
- マス 0 に駒を置く。この時点でマス 0, 1, 2 にコマが乗っている。
- すべての駒を 3 大きいマスに進める。
この時、マス 1,2 にある駒は移動先のマスが存在しないため (それぞれ 1+3=4,2+3=5 なので) 、盤上から取り除いて P に 2 を加算する。P の値は 2 になる。
移動を終えた時点でマス 3 に駒が乗っている。
- i=4 での操作
- マス 0 に駒を置く。この時点でマス 0, 3 にコマが乗っている。
- すべての駒を 2 大きいマスに進める。
この時、マス 3 にある駒は移動先のマスが存在しないため (3+2=5 なので) 、盤上から取り除いて P に 1 を加算する。P の値は 3 になる。
移動を終えた時点でマス 2 に駒が乗っている。
入力例 2
3 1 1 1
出力例 2
0
P の値が操作中に変化しない場合もあります。
入力例 3
10 2 2 4 1 1 1 4 2 2 1
出力例 3
8
Score : 200 points
Problem Statement
Takahashi is trying to create a game inspired by baseball, but he is having difficulty writing the code.
Write a program for Takahashi that solves the following problem.
There are 4 squares called Square 0, Square 1, Square 2, and Square 3. Initially, all squares are empty.
There is also an integer P; initially, P = 0.
Given a sequence of positive integers A = (A_1, A_2, \dots, A_N), perform the following operations for i = 1, 2, \dots, N in this order:
- Put a piece on Square 0.
- Advance every piece on the squares A_i squares ahead. In other words, if Square x has a piece, move the piece to Square (x + A_i).
If, however, the destination square does not exist (i.e. x + A_i is greater than or equal to 4) for a piece, remove it. Add to P the number of pieces that have been removed.
Print the value of P after all the operations have been performed.
Constraints
- 1 \leq N \leq 100
- 1 \leq A_i \leq 4
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the value of P after all the operations have been performed.
Sample Input 1
4 1 1 3 2
Sample Output 1
3
The operations are described below. After all the operations have been performed, P equals 3.
- The operations for i=1:
- Put a piece on Square 0. Now, Square 0 has a piece.
- Advance every piece on the squares 1 square ahead. After these moves, Square 1 has a piece.
- The operations for i=2:
- Put a piece on Square 0. Now, Squares 0 and 1 have a piece.
- Advance every piece on the squares 1 square ahead. After these moves, Squares 1 and 2 have a piece.
- The operations for i=3:
- Put a piece on Square 0. Now, Squares 0, 1, and 2 have a piece.
- Advance every piece on the squares 3 squares ahead.
Here, for the pieces on Squares 1 and 2, the destination squares do not exist (since 1+3=4 and 2+3=5), so remove these pieces and add 2 to P. P now equals 2. After these moves, Square 3 has a piece.
- The operations for i=4:
- Put a piece on Square 0. Now, Squares 0 and 3 have a piece.
- Advance every piece on the squares 2 squares ahead.
Here, for the piece on Square 3, the destination square does not exist (since 3+2=5), so remove this piece and add 1 to P. P now equals 3.
After these moves, Square 2 has a piece.
Sample Input 2
3 1 1 1
Sample Output 2
0
The value of P may not be updated by the operations.
Sample Input 3
10 2 2 4 1 1 1 4 2 2 1
Sample Output 3
8
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
左右一列に N 個のマスが並んでいます。 最初、すべてのマスは白く塗られています。
Q 個のクエリを順に処理してください。i 個目のクエリでは 1 以上 N 以下の整数 A_i が与えられ、次の操作を行います。
左から A_i 番目のマスの色を反転させる。 具体的には、左から A_i 番目のマスが、白く塗られていたならば黒く塗り、黒く塗られていたならば白く塗る。
その後、黒く塗られたマスが連続している区間の個数を求める。ここで、黒く塗られたマスが連続している区間とは次をすべてみたす整数の組 (l,r) (1\leq l\leq r\leq N) を指す。
- 左から l, l+1, \ldots, r 番目のマスはすべて黒く塗られている。
- l=1 であるか、または左から (l-1) 番目のマスは白く塗られている。
- r=N であるか、または左から (r+1) 番目のマスは白く塗られている。
制約
- 1\leq N,Q\leq 5\times 10^5
- 1\leq A_i\leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q A_1 A_2 \ldots A_Q
出力
Q 行出力せよ。i 行目 (1\leq i\leq Q) には i 個目のクエリの答えを出力せよ。
入力例 1
5 7 2 3 3 5 1 5 2
出力例 1
1 1 1 2 2 1 1
以下では、左から i 番目のマスを マス i と表します。
各クエリの後は次のような状態になっています。
- 1 個目のクエリの後、マス 2 のみが黒く塗られています。黒く塗られたマスが連続している区間は (l,r)=(2,2) の 1 つです。
- 2 個目のクエリの後、マス 2,3 が黒く塗られています。黒く塗られたマスが連続している区間は (l,r)=(2,3) の 1 つです。
- 3 個目のクエリの後、マス 2 のみが黒く塗られています。黒く塗られたマスが連続している区間は (l,r)=(2,2) の 1 つです。
- 4 個目のクエリの後、マス 2,5 が黒く塗られています。黒く塗られたマスが連続している区間は (l,r)=(2,2), (5,5) の 2 つです。
- 5 個目のクエリの後、マス 1,2,5 が黒く塗られています。黒く塗られたマスが連続している区間は (l,r)=(1,2), (5,5) の 2 つです。
- 6 個目のクエリの後、マス 1,2 のみが黒く塗られています。黒く塗られたマスが連続している区間は (l,r)=(1,2) の 1 つです。
- 7 個目のクエリの後、マス 1 のみが黒く塗られています。黒く塗られたマスが連続している区間は (l,r)=(1,1) の 1 つです。
よって、1,1,1,2,2,1,1 を改行区切りで出力します。
入力例 2
1 2 1 1
出力例 2
1 0
2 個目のクエリの後、すべてのマスは白く塗られているため、2 行目には 0 を出力します。
入力例 3
3 3 1 3 2
出力例 3
1 2 1
Score : 350 points
Problem Statement
There are N squares arranged in a row from left to right. Initially, all squares are painted white.
Process Q queries in order. The i-th query gives an integer A_i between 1 and N, inclusive, and performs the following operation:
Flip the color of the A_i-th square from the left. Specifically, if the A_i-th square from the left is painted white, paint it black; if it is painted black, paint it white.
Then, find the number of intervals of consecutively painted black squares.Here, an interval of consecutively painted black squares is a pair of integers (l,r) (1\leq l\leq r\leq N) that satisfy all of the following:
- The l-th, (l+1)-th, \ldots, r-th squares from the left are all painted black.
- Either l=1, or the (l-1)-th square from the left is painted white.
- Either r=N, or the (r+1)-th square from the left is painted white.
Constraints
- 1\leq N,Q\leq 5\times 10^5
- 1\leq A_i\leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q A_1 A_2 \ldots A_Q
Output
Output Q lines. On the i-th line (1\leq i\leq Q), output the answer to the i-th query.
Sample Input 1
5 7 2 3 3 5 1 5 2
Sample Output 1
1 1 1 2 2 1 1
Below, the i-th square from the left is referred to as square i.
After each query, the state is as follows:
- After the 1st query, only square 2 is painted black. There is 1 interval of consecutively painted black squares: (l,r)=(2,2).
- After the 2nd query, squares 2,3 are painted black. There is 1 interval of consecutively painted black squares: (l,r)=(2,3).
- After the 3rd query, only square 2 is painted black. There is 1 interval of consecutively painted black squares: (l,r)=(2,2).
- After the 4th query, squares 2,5 are painted black. There are 2 intervals of consecutively painted black squares: (l,r)=(2,2), (5,5).
- After the 5th query, squares 1,2,5 are painted black. There are 2 intervals of consecutively painted black squares: (l,r)=(1,2), (5,5).
- After the 6th query, only squares 1,2 are painted black. There is 1 interval of consecutively painted black squares: (l,r)=(1,2).
- After the 7th query, only square 1 is painted black. There is 1 interval of consecutively painted black squares: (l,r)=(1,1).
Thus, output 1,1,1,2,2,1,1 separated by newlines.
Sample Input 2
1 2 1 1
Sample Output 2
1 0
After the 2nd query, all squares are painted white, so output 0 on the 2nd line.
Sample Input 3
3 3 1 3 2
Sample Output 3
1 2 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
ボールがいくつか刺さった棒が N 本あり、各ボールには英小文字が 1 個書かれています。
i = 1, 2, \ldots, N について、i 番目の棒に刺さった各ボールの英小文字は、文字列 S_i によって表されます。 具体的には、i 番目の棒には文字列 S_i の長さ |S_i| に等しい個数のボールが刺さっており、 刺さっているボールの英小文字を、棒のある端から順に並べたものは文字列 S_i と等しいです。
2 つの棒は、一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列と、もう一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列が一致するとき、同じ棒とみなされます。 より形式的には、1 以上 N 以下の整数 i, j について、i 本目の棒と j 本目の棒は、S_i が S_j と一致するか、S_i が S_j を前後反転したものと一致するとき、かつそのときに限り、同じとみなされます。
N 本の棒の中に、何種類の異なる棒があるかを出力してください。
制約
- N は整数
- 2 \leq N \leq 2 \times 10^5
- S_i は英小文字のみからなる文字列
- |S_i| \geq 1
- \sum_{i = 1}^N |S_i| \leq 2 \times 10^5
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
6 a abc de cba de abc
出力例 1
3
- S_2 =
abcが S_4 =cbaを前後反転したものと一致するため、2 番目の棒と 4 番目の棒は同じとみなされます。 - S_2 =
abcが S_6 =abcと一致するため、2 番目の棒と 6 番目の棒は同じとみなされます。 - S_3 =
deが S_5 =deと一致するため、3 番目の棒と 5 番目の棒は同じとみなされます。
よって、6 本の棒の中に、1 本目の棒、2 本目の棒( 4, 6 本目の棒と同じ)、3 本目の棒( 5 本目の棒と同じ)の 3 種類の異なる棒があります。
Score : 300 points
Problem Statement
There are N sticks with several balls stuck onto them. Each ball has a lowercase English letter written on it.
For each i = 1, 2, \ldots, N, the letters written on the balls stuck onto the i-th stick are represented by a string S_i. Specifically, the number of balls stuck onto the i-th stick is the length |S_i| of the string S_i, and S_i is the sequence of letters on the balls starting from one end of the stick.
Two sticks are considered the same when the sequence of letters on the balls starting from one end of one stick is equal to the sequence of letters starting from one end of the other stick. More formally, for integers i and j between 1 and N, inclusive, the i-th and j-th sticks are considered the same if and only if S_i equals S_j or its reversal.
Print the number of different sticks among the N sticks.
Constraints
- N is an integer.
- 2 \leq N \leq 2 \times 10^5
- S_i is a string consisting of lowercase English letters.
- |S_i| \geq 1
- \sum_{i = 1}^N |S_i| \leq 2 \times 10^5
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
6 a abc de cba de abc
Sample Output 1
3
- S_2 =
abcequals the reversal of S_4 =cba, so the second and fourth sticks are considered the same. - S_2 =
abcequals S_6 =abc, so the second and sixth sticks are considered the same. - S_3 =
deequals S_5 =de, so the third and fifth sticks are considered the same.
Therefore, there are three different sticks among the six: the first, second (same as the fourth and sixth), and third (same as the fifth).
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
縦横 10^9 マスのグリッドがあります。上から i + 1 行目、左から j + 1 列目 (0 \leq i, j \lt 10^9) にあるマスを (i, j) と呼びます。(通常と異なる index の割り当て方に注意してください。)
各マスは黒マスか白マスのいずれかです。マス (i, j) の色は文字 P[i \bmod N][j \bmod N] によって表されて、B ならばマス (i, j) は黒マス、W ならば白マスです。ここで a \bmod b は a を b で割った余りを意味します。
Q 個のクエリが与えられるので順に処理してください。
各クエリでは 4 つの整数 A, B, C, D が与えられるので、(A, B) を左上隅、(C, D) を右下隅とする長方形領域に含まれる黒マスの個数を求めてください。
制約
- 1 \leq N \leq 1000
- P[i][j] は
WまたはB - 1 \leq Q \leq 2 \times 10^5
- 0 \leq A \leq C \lt 10^9
- 0 \leq B \leq D \lt 10^9
- N, Q, A, B, C, D は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで \text{query}_i は i 番目に処理するクエリである。
N Q
P[0][0]P[0][1]\dots P[0][N-1]
P[1][0]P[1][1]\dots P[1][N-1]
\vdots
P[N-1][0]P[N-1][1]\dots P[N-1][N-1]
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
各クエリは以下の形式で与えられる。
A B C D
出力
問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。
入力例 1
3 2 WWB BBW WBW 1 2 3 4 0 3 4 5
出力例 1
4 7
グリッドの左上部分を図示すると次の図のようになります。

1 番目のクエリについて、(1, 2) を左上隅、(3, 4) を右下隅とする長方形領域は図の赤い枠線に囲まれた部分で、領域に含まれる黒マスの個数は 4 個です。
2 番目のクエリについて、(0, 3) を左上隅、(4, 5) を右下隅とする長方形領域は図の青い枠線に囲まれた部分で、領域に含まれる黒マスの個数は 7 個です。
入力例 2
10 5 BBBWWWBBBW WWWWWBBBWB BBBWBBWBBB BBBWWBWWWW WWWWBWBWBW WBBWBWBBBB WWBBBWWBWB WBWBWWBBBB WBWBWBBWWW WWWBWWBWWB 5 21 21 93 35 35 70 43 55 72 61 84 36 33 46 95 0 0 999999999 999999999
出力例 2
621 167 44 344 500000000000000000
Score : 450 points
Problem Statement
There is a grid with 10^9 by 10^9 squares. Let (i, j) denote the square at the (i + 1)-th row from the top and the (j + 1)-th column from the left (0 \leq i, j \lt 10^9). (Note the unusual index assignment.)
Each square is black or white. The color of the square (i, j) is represented by a character P[i \bmod N][j \bmod N], where B means black, and W means white. Here, a \bmod b denotes the remainder when a is divided by b.
Answer Q queries.
Each query gives you four integers A, B, C, D and asks you to find the number of black squares contained in the rectangular area with (A, B) as the top-left corner and (C, D) as the bottom-right corner.
Constraints
- 1 \leq N \leq 1000
- P[i][j] is
WorB. - 1 \leq Q \leq 2 \times 10^5
- 0 \leq A \leq C \lt 10^9
- 0 \leq B \leq D \lt 10^9
- N, Q, A, B, C, D are all integers.
Input
The input is given from Standard Input in the following format. Here, \text{query}_i is the i-th query to be processed.
N Q
P[0][0]P[0][1]\dots P[0][N-1]
P[1][0]P[1][1]\dots P[1][N-1]
\vdots
P[N-1][0]P[N-1][1]\dots P[N-1][N-1]
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Each query is given in the following format:
A B C D
Output
Follow the instructions in the problem statement and print the answers to the queries, separated by newlines.
Sample Input 1
3 2 WWB BBW WBW 1 2 3 4 0 3 4 5
Sample Output 1
4 7
The figure below illustrates the upper left part of the grid.

For the first query, the rectangular area with (1, 2) as the top-left corner and (3, 4) as the bottom-right corner, surrounded by the red frame in the figure, contains four black squares.
For the second query, the rectangular area with (0, 3) as the top-left corner and (4, 5) as the bottom-right corner, surrounded by the blue frame in the figure, contains seven black squares.
Sample Input 2
10 5 BBBWWWBBBW WWWWWBBBWB BBBWBBWBBB BBBWWBWWWW WWWWBWBWBW WBBWBWBBBB WWBBBWWBWB WBWBWWBBBB WBWBWBBWWW WWWBWWBWWB 5 21 21 93 35 35 70 43 55 72 61 84 36 33 46 95 0 0 999999999 999999999
Sample Output 2
621 167 44 344 500000000000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
2 次元平面上に N 個の街と M 個の宝箱があります。街 i は座標 (X_i,Y_i) に、宝箱 i は座標 (P_i,Q_i) にあります。
高橋君は原点を出発し、N 個の街全てを訪れたのち原点に戻る旅行をしようと考えています。
宝箱を訪れる必要はありませんが、宝箱の中にはそれぞれブースターが 1 つあり、ブースターを拾うごとに移動速度が 2 倍になります。
高橋君の最初の移動速度が単位時間あたり 1 であるとき、旅行にかかる時間の最小値を求めてください。
制約
- 1 \leq N \leq 12
- 0 \leq M \leq 5
- -10^9 \leq X_i,Y_i,P_i,Q_i \leq 10^9
- (0,0),(X_i,Y_i),(P_i,Q_i) は相異なる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M X_1 Y_1 \vdots X_N Y_N P_1 Q_1 \vdots P_M Q_M
出力
答えを出力せよ。なお、想定解答との絶対誤差または相対誤差が 10^{-6} 以下であれば正解として扱われる。
入力例 1
2 1 1 1 0 1 1 0
出力例 1
2.5000000000
以下のように移動するのが最適解の一つです。
- 原点から宝箱 1 までの距離 1 を速さ 1 で移動する。時間が 1 かかる
- 宝箱 1 から街 1 までの距離 1 を速さ 2 で移動する。時間が 0.5 かかる
- 街 1 から街 2までの距離 1 を速さ 2 で移動する。時間が 0.5 かかる
- 街 2から原点までの距離 1 を速さ 2 で移動する。時間が 0.5 かかる
入力例 2
2 1 1 1 0 1 100 0
出力例 2
3.4142135624
以下のように移動するのが最適解の一つです。
- 原点 から街 1 までの距離 1.41\ldots を速さ 1 で移動する。時間が 1.41\ldots かかる
- 街 1 から街 2 までの距離 1 を速さ 1 で移動する。時間が 1 かかる
- 街 2 から原点までの距離 1 を速さ 1 で移動する。時間が 1 かかる
入力例 3
1 2 4 4 1 0 0 1
出力例 3
4.3713203436
以下のように移動するのが最適解の一つです。
- 原点から宝箱 1 までの距離 1 を速さ 1 で移動する。時間が 1 かかる
- 宝箱 1 から宝箱 2 までの距離 1.41\ldots を速さ 2 で移動する。時間が 0.707\ldots かかる
- 宝箱 2 から街 1 までの距離 5 を速さ 4 で移動する。時間が 1.25 かかる
- 街 1 から原点までの距離 5.65\ldots を速さ 4 で移動する。時間が 1.41\ldots かかる
Score : 500 points
Problem Statement
In a two-dimensional plane, there are N towns and M chests. Town i is at the coordinates (X_i,Y_i), and chest i is at the coordinates (P_i,Q_i).
Takahashi will go on a trip where he starts at the origin, visits all N towns, and then returns to the origin.
It is not mandatory to visit chests, but each chest contains an accelerator. Each time he picks up an accelerator, his moving speed gets multiplied by 2.
Takahashi's initial moving speed is 1. Find the shortest time needed to complete the trip.
Constraints
- 1 \leq N \leq 12
- 0 \leq M \leq 5
- -10^9 \leq X_i,Y_i,P_i,Q_i \leq 10^9
- (0,0), (X_i,Y_i), and (P_i,Q_i) are distinct.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M X_1 Y_1 \vdots X_N Y_N P_1 Q_1 \vdots P_M Q_M
Output
Print the answer. Your output will be considered correct if the absolute or relative error from the judge's answer is at most 10^{-6}.
Sample Input 1
2 1 1 1 0 1 1 0
Sample Output 1
2.5000000000
Here is one optimal way to complete the trip.
- Go the distance 1 from the origin to chest 1 at a speed of 1, taking a time of 1.
- Go the distance 1 from chest 1 to town 1 at a speed of 2, taking a time of 0.5.
- Go the distance 1 from town 1 to town 2 at a speed of 2, taking a time of 0.5.
- Go the distance 1 from town 2 to the origin at a speed of 2, taking a time of 0.5.
Sample Input 2
2 1 1 1 0 1 100 0
Sample Output 2
3.4142135624
Here is one optimal way to complete the trip.
- Go the distance 1.41\ldots from the origin to town 1 at a speed of 1, taking a time of 1.41\ldots.
- Go the distance 1 from town 1 to town 2 at a speed of 1, taking a time of 1.
- Go the distance 1 from town 2 to the origin at a speed of 1, taking a time of 1.
Sample Input 3
1 2 4 4 1 0 0 1
Sample Output 3
4.3713203436
Here is one optimal way to complete the trip.
- Go the distance 1 from the origin to chest 1 at a speed of 1, taking a time of 1.
- Go the distance 1.41\ldots from chest 1 to chest 2 at a speed of 2, taking a time of 0.707\ldots.
- Go the distance 5 from chest 2 to town 1 at a speed of 4, taking a time of 1.25.
- Go the distance 5.65\ldots from town 1 to the origin at a speed of 4, taking a time of 1.41\ldots.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
長さ N の 2 つの整数列 A = (A_1, A_2, \ldots, A_N) および B = (B_1, B_2 \ldots, B_N) が与えられます。
整数列 A に対して、「下記の 2 つの操作のうちどちらかを行う」ということを好きな回数( 0 回でもよい)繰り返すことができます。
- 1 \leq i \leq N を満たす整数 i を選び、A_i の値を 1 増やすか 1 減らす。この操作には X 円の費用がかかる。
- 1 \leq i \leq N-1 を満たす整数 i を選び、A_i の値と A_{i+1} の値を入れ替える。この操作には Y 円の費用がかかる。
上記の繰り返しによって整数列 A を整数列 B に一致させるためにかかる合計費用としてあり得る最小値を出力してください。
制約
- 2 \leq N \leq 18
- 1 \leq X \leq 10^8
- 1 \leq Y \leq 10^{16}
- 1 \leq A_i, B_i \leq 10^8
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X Y A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
出力
A を B に一致させるためにかかる合計費用としてあり得る最小値を求めよ。
入力例 1
4 3 5 4 2 5 2 6 4 2 1
出力例 1
16
はじめ、A = (4, 2, 5, 2) です。
下記の通りに操作を行うと、A を B に一致させることができます。
- X = 3 円払い、A_3 の値を 1 増やす。その結果 A = (4, 2, 6, 2) となる。
- Y = 5 円払い、A_2 の値と A_3 の値を入れ替える。その結果 A = (4, 6, 2, 2) となる。
- Y = 5 円払い、A_1 の値と A_2 の値を入れ替える。その結果 A = (6, 4, 2, 2) となる。
- X = 3 円払い、A_4 の値を 1 減らす。その結果 A = (6, 4, 2, 1) = B となる。
上記の操作にかかる費用の合計は 3+5+5+3 = 16 円であり、これが最小となります。
入力例 2
5 12345 6789 1 2 3 4 5 1 2 3 4 5
出力例 2
0
A と B は初めから一致しているため、一度も操作を行う必要がありません。
入力例 3
18 20719114 5117250357733867 10511029 36397527 63027379 44706927 47672230 79861204 57882493 42931589 51053644 52300688 43971370 26515475 62139996 41282303 34022578 12523039 6696497 64922712 14720753 4621362 25269832 91410838 86751784 32741849 6602693 60719353 28911226 88280613 18745325 80675202 34289776 37849132 99280042 73760634 43897718 40659077
出力例 3
13104119429316474
入力や出力が 32 bit 整数型に収まらないことがあることに注意してください。
Score : 500 points
Problem Statement
Given are two sequences of N integers each: A = (A_1, A_2, \ldots, A_N) and B = (B_1, B_2 \ldots, B_N).
On the sequence A, you can do the two operations below any number of times (possibly zero) in any order.
- Choose an integer i satisfying 1 \leq i \leq N, and increase or decrease A_i by 1, for the cost of X yen (Japanese currency).
- Choose an integer i satisfying 1 \leq i \leq N-1, and swap the values of A_i and A_{i+1}, for the cost of Y yen.
Print the minimum total cost needed to make the sequence A equal the sequence B by repeating the above.
Constraints
- 2 \leq N \leq 18
- 1 \leq X \leq 10^8
- 1 \leq Y \leq 10^{16}
- 1 \leq A_i, B_i \leq 10^8
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X Y A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
Output
Print the minimum total cost needed to make A equal B.
Sample Input 1
4 3 5 4 2 5 2 6 4 2 1
Sample Output 1
16
Initialy, we have A = (4, 2, 5, 2).
The following sequence of operations makes A equal B.
- Pay X = 3 yen to increase the value of A_3 by 1, making A = (4, 2, 6, 2).
- Pay Y = 5 yen to swap the values of A_2 and A_3, making A = (4, 6, 2, 2).
- Pay Y = 5 yen to swap the values of A_1 and A_2, making A = (6, 4, 2, 2).
- Pay X = 3 yen to decrease the value of A_4 by 1, making A = (6, 4, 2, 1).
The total cost of these operations is 3+5+5+3 = 16 yen, which is the minimum possible.
Sample Input 2
5 12345 6789 1 2 3 4 5 1 2 3 4 5
Sample Output 2
0
A and B are equal from the beginning, so no operation is needed.
Sample Input 3
18 20719114 5117250357733867 10511029 36397527 63027379 44706927 47672230 79861204 57882493 42931589 51053644 52300688 43971370 26515475 62139996 41282303 34022578 12523039 6696497 64922712 14720753 4621362 25269832 91410838 86751784 32741849 6602693 60719353 28911226 88280613 18745325 80675202 34289776 37849132 99280042 73760634 43897718 40659077
Sample Output 3
13104119429316474
Note that values in input or output may not fit into 32-bit integer types.