実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
二次元平面があります。1 以上 9 以下の整数 r,c について、S_{r} の c 番目の文字が # であるとき座標 (r,c) にポーンが置いてあり、S_{r} の c 番目の文字が . であるとき座標 (r,c) に何も置かれていません。
この平面上の正方形であって、4 頂点全てにポーンが置いてあるものの個数を求めてください。
制約
- S_1,\ldots,S_9 はそれぞれ
#と.からなる長さ 9 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S_1 S_2 \vdots S_9
出力
答えを出力せよ。
入力例 1
##....... ##....... ......... .......#. .....#... ........# ......#.. ......... .........
出力例 1
2
座標 (1,1),(1,2),(2,2),(2,1) を頂点とする正方形は、4 頂点全てにポーンが置かれています。
座標 (4,8),(5,6),(7,7),(6,9) を頂点とする正方形も、4 頂点全てにポーンが置かれています。
よって答えは 2 です。
入力例 2
.#....... #.#...... .#....... ......... ....#.#.# ......... ....#.#.# ........# .........
出力例 2
3
Score : 300 points
Problem Statement
There is a two-dimensional plane. For integers r and c between 1 and 9, there is a pawn at the coordinates (r,c) if the c-th character of S_{r} is #, and nothing if the c-th character of S_{r} is ..
Find the number of squares in this plane with pawns placed at all four vertices.
Constraints
- Each of S_1,\ldots,S_9 is a string of length 9 consisting of
#and..
Input
The input is given from Standard Input in the following format:
S_1 S_2 \vdots S_9
Output
Print the answer.
Sample Input 1
##....... ##....... ......... .......#. .....#... ........# ......#.. ......... .........
Sample Output 1
2
The square with vertices (1,1), (1,2), (2,2), and (2,1) have pawns placed at all four vertices.
The square with vertices (4,8), (5,6), (7,7), and (6,9) also have pawns placed at all four vertices.
Thus, the answer is 2.
Sample Input 2
.#....... #.#...... .#....... ......... ....#.#.# ......... ....#.#.# ........# .........
Sample Output 2
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
9\times 9 のマス目 A があり、各マスには 1 以上 9 以下の整数が書き込まれています。
具体的には、 A の上から i 行目、左から j 列目のマスには A_{i,j} が書き込まれています。
A が次の条件をすべてみたしているならば Yes を、そうでないならば No を出力してください。
- A の各行について、その行に含まれる 9 マスには 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
- A の各列について、その列に含まれる 9 マスには 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
- A の行を上から 3 行ずつ 3 つに分け、同様に列も左から 3 列ずつ 3 つに分ける。 これによって A を 9 つの 3\times 3 のマス目に分けたとき、それぞれの 3\times 3 のマス目には 1 以上 9 以下の整数がちょうど 1 個ずつ書き込まれている。
制約
- 1\leq A_{i,j}\leq 9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
A_{1,1} A_{1,2} \ldots A_{1,9}
A_{2,1} A_{2,2} \ldots A_{2,9}
\vdots
A_{9,1} A_{9,2} \ldots A_{9,9}
出力
マス目 A が問題文の条件をすべてみたすならば Yes を、
そうでないならば No を出力せよ。
入力例 1
1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 2 3 4 5 6 7 8 9 1 5 6 7 8 9 1 2 3 4 8 9 1 2 3 4 5 6 7 3 4 5 6 7 8 9 1 2 6 7 8 9 1 2 3 4 5 9 1 2 3 4 5 6 7 8
出力例 1
Yes
マス目 A は次のようになっています。

マス目 A は 3 つの条件をすべてみたしているため、Yes を出力します。
入力例 2
1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 1 3 4 5 6 7 8 9 1 2 4 5 6 7 8 9 1 2 3 5 6 7 8 9 1 2 3 4 6 7 8 9 1 2 3 4 5 7 8 9 1 2 3 4 5 6 8 9 1 2 3 4 5 6 7 9 1 2 3 4 5 6 7 8
出力例 2
No
マス目 A は次のようになっています。

例えば左上の 3\times 3 のマス目に注目すると 3 つめの条件をみたしていないことが分かるため、No を出力します。
入力例 3
1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6
出力例 3
No
マス目 A は次のようになっています。

例えば一番左の列に注目すると 2 つめの条件をみたしていないことが分かるため、No を出力します。
Score : 250 points
Problem Statement
There is a 9\times 9 grid A, where each cell contains an integer between 1 and 9, inclusive.
Specifically, the cell at the i-th row from the top and j-th column from the left contains A_{i,j}.
If A satisfies all of the following conditions, print Yes. Otherwise, print No.
- For each row of A, the nine cells in that row contain each integer from 1 to 9 exactly once.
- For each column of A, the nine cells in that column contain each integer from 1 to 9 exactly once.
- Divide the rows of A into three groups, each of three rows, from top to bottom, and similarly divide the columns into three groups, each of three columns, from left to right. Each 3\times 3 grid obtained from A in this way contains each integer from 1 to 9 exactly once.
Constraints
- 1\leq A_{i,j}\leq 9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A_{1,1} A_{1,2} \ldots A_{1,9}
A_{2,1} A_{2,2} \ldots A_{2,9}
\vdots
A_{9,1} A_{9,2} \ldots A_{9,9}
Output
If the grid A satisfies all the conditions in the problem statement, print Yes; otherwise, print No.
Sample Input 1
1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 2 3 4 5 6 7 8 9 1 5 6 7 8 9 1 2 3 4 8 9 1 2 3 4 5 6 7 3 4 5 6 7 8 9 1 2 6 7 8 9 1 2 3 4 5 9 1 2 3 4 5 6 7 8
Sample Output 1
Yes
The grid A is shown below.

The grid A satisfies all three conditions, so print Yes.
Sample Input 2
1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9 1 3 4 5 6 7 8 9 1 2 4 5 6 7 8 9 1 2 3 5 6 7 8 9 1 2 3 4 6 7 8 9 1 2 3 4 5 7 8 9 1 2 3 4 5 6 8 9 1 2 3 4 5 6 7 9 1 2 3 4 5 6 7 8
Sample Output 2
No
The grid A is shown below.

For example, if you look at the top left 3\times 3 grid, you can see that the third condition is unsatisfied, so print No.
Sample Input 3
1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 4 5 6 7 8 9 1 2 3 7 8 9 1 2 3 4 5 6
Sample Output 3
No
The grid A is shown below.

For example, if you look at the leftmost column, you can see that the second condition is unsatisfied, so print No.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
数列 (A_1,\ldots,A_K) を使って、高橋君と青木君が石取りゲームをします。
最初、山には N 個の石があります。高橋君から順に、二人が交互に次の操作を行います。
- 現在山にある石の個数以下であるような A_i を 1 つ選ぶ。山から A_i 個の石を取り除く。
山から石がなくなったとき、ゲームは終了します。
二人がともに、ゲーム終了までに自分が取り除いた石の個数を最大化しようとするとき、高橋君は何個の石を取り除くことができますか?
制約
- 1 \leq N \leq 10^4
- 1 \leq K \leq 100
- 1 = A_1 < A_2 < \ldots < A_K \leq N
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_K
出力
答えを出力せよ。
入力例 1
10 2 1 4
出力例 1
5
ゲームの進行の一例は以下の通りです。
- 高橋君が山から 4 個の石を取り除く。
- 青木君が山から 4 個の石を取り除く。
- 高橋君が山から 1 個の石を取り除く。
- 青木君が山から 1 個の石を取り除く。
この例では高橋君は 5 個の石を取り除くことができます。高橋君が 6 個以上の石を取り除くことはできないためこれが最大です。
高橋君は 5 個の石を取り除くことができるゲームの進行の例には、他にも次のようなものがあります。
- 高橋君が山から 1 個の石を取り除く。
- 青木君が山から 4 個の石を取り除く。
- 高橋君が山から 4 個の石を取り除く。
- 青木君が山から 1 個の石を取り除く。
入力例 2
11 4 1 2 3 6
出力例 2
8
ゲームの進行の一例は以下の通りです。
- 高橋君が山から 6 個の石を取り除く。
- 青木君が山から 3 個の石を取り除く。
- 高橋君が山から 2 個の石を取り除く。
この例では高橋君は 8 個の石を取り除くことができます。高橋君が 9 個以上の石を取り除くことはできないためこれが最大です。
入力例 3
10000 10 1 2 4 8 16 32 64 128 256 512
出力例 3
5136
Score : 400 points
Problem Statement
Takahashi and Aoki will play a game of taking stones using a sequence (A_1, \ldots, A_K).
There is a pile that initially contains N stones. The two players will alternately perform the following operation, with Takahashi going first.
- Choose an A_i that is at most the current number of stones in the pile. Remove A_i stones from the pile.
The game ends when the pile has no stones.
If both players attempt to maximize the total number of stones they remove before the end of the game, how many stones will Takahashi remove?
Constraints
- 1 \leq N \leq 10^4
- 1 \leq K \leq 100
- 1 = A_1 < A_2 < \ldots < A_K \leq N
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \ldots A_K
Output
Print the answer.
Sample Input 1
10 2 1 4
Sample Output 1
5
Below is one possible progression of the game.
- Takahashi removes 4 stones from the pile.
- Aoki removes 4 stones from the pile.
- Takahashi removes 1 stone from the pile.
- Aoki removes 1 stone from the pile.
In this case, Takahashi removes 5 stones. There is no way for him to remove 6 or more stones, so this is the maximum.
Below is another possible progression of the game where Takahashi removes 5 stones.
- Takahashi removes 1 stone from the pile.
- Aoki removes 4 stones from the pile.
- Takahashi removes 4 stones from the pile.
- Aoki removes 1 stone from the pile.
Sample Input 2
11 4 1 2 3 6
Sample Output 2
8
Below is one possible progression of the game.
- Takahashi removes 6 stones.
- Aoki removes 3 stones.
- Takahashi removes 2 stones.
In this case, Takahashi removes 8 stones. There is no way for him to remove 9 or more stones, so this is the maximum.
Sample Input 3
10000 10 1 2 4 8 16 32 64 128 256 512
Sample Output 3
5136
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
箱の中に N 個のボールが入っており、各ボールには 1 以上 M-1 以下の整数が書かれています。 i = 1, 2, \ldots, N について、i 番目のボールに書かれた整数は A_i です。
高橋君は、箱の中に 2 個以上のボールが残っている限り、下記の行動を繰り返します。
- まず、箱の中から 2 つのボールを任意に選んで取り出す。
- 次に、取り出した 2 つのボールに書かれた整数をそれぞれ x, y とおき、x^y + y^x を M で割ったあまりに等しい得点を獲得する。
- その後、取り出した 2 つのボールのうち、任意に選んだ一方を食べ、もう一方を箱の中に戻す。
高橋君が獲得する合計得点としてあり得る最大値を出力してください。
制約
- 2 \leq N \leq 500
- 2 \leq M \leq 10^9
- 1 \leq A_i \leq M-1
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
4 10 4 2 3 2
出力例 1
20
高橋君が下記の通りに行動する場合を考えます。以下、X \bmod Y で 非負整数 X を正整数 Y で割ったあまりを表します。
- 箱の中から 1 番目のボールと 3 番目のボールを取り出し、(4^3 + 3^4) \bmod 10 = 5 点を獲得します。その後、1 番目のボールを食べ、3 番目のボールを箱の中に戻します。その結果、箱の中には 2, 3, 4 番目のボールが残ります。
- 箱の中から 3 番目のボールと 4 番目のボールを取り出し、(3^2 + 2^3) \bmod 10 = 7 点を獲得します。その後、3 番目のボールを食べ、4 番目のボールを箱の中に戻します。その結果、箱の中には 2, 4 番目のボールが残ります。
- 箱の中から 2 番目のボールと 4 番目のボールを取り出し、(2^2 + 2^2) \bmod 10 = 8 点を獲得します。その後、2 番目のボールを食べ、4 番目のボールを箱の中に戻します。その結果、箱の中には 4 番目のボールのみが残ります。
このとき、高橋君が獲得する合計得点は 5 + 7 + 8 = 20 点であり、これがあり得る最大値です。
入力例 2
20 100 29 31 68 20 83 66 23 84 69 96 41 61 83 37 52 71 18 55 40 8
出力例 2
1733
Score : 500 points
Problem Statement
A box contains N balls, each with an integer between 1 and M-1 written on it. For i = 1, 2, \ldots, N, the integer written on the i-th ball is A_i.
While the box has two or more balls remaining, Takahashi will repeat the following.
- First, choose two balls arbitrarily.
- Then, get a score equal to the remainder when x^y + y^x is divided by M, where x and y are the integers written on the two balls.
- Finally, choose one of the two balls arbitrarily, eat it, and return the other to the box.
Print the maximum possible total score Takahashi will get.
Constraints
- 2 \leq N \leq 500
- 2 \leq M \leq 10^9
- 1 \leq A_i \leq M-1
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
4 10 4 2 3 2
Sample Output 1
20
Consider the following scenario. Below, X \bmod Y denotes the remainder when a non-negative integer X is divided by a positive integer Y.
- Take the first and third balls from the box to score (4^3 + 3^4) \bmod 10 = 5 points. Then, eat the first ball and return the third to the box. Now, the box has the second, third, and fourth balls.
- Take the third and fourth balls from the box to score (3^2 + 2^3) \bmod 10 = 7 points. Then, eat the third ball and return the fourth to the box. Now, the box has the second and fourth balls.
- Take the second and fourth balls from the box to score (2^2 + 2^2) \bmod 10 = 8 points. Then, eat the second ball and return the fourth to the box. Now, the box has just the fourth ball.
Here, Takahashi scores a total of 5 + 7 + 8 = 20 points, which is the maximum possible value.
Sample Input 2
20 100 29 31 68 20 83 66 23 84 69 96 41 61 83 37 52 71 18 55 40 8
Sample Output 2
1733
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 550 点
問題文
縦 N マス、横 N マスの N ^ 2 マスからなるマス目があります。 上から i 行目 (1\leq i\leq N) 、左から j 列目 (1\leq j\leq N) のマスをマス (i,j) と呼ぶことにします。
それぞれのマスは、空マスであるかコマが置かれているかのどちらかです。 マス目には合計で M 個のコマが置かれており、k 番目 (1\leq k\leq M) のコマはマス (a _ k,b _ k) に置かれています。
あなたは、すでに置かれているどのコマにも取られないように、いずれかの空マスに自分のコマを置きたいです。
マス (i,j) に置かれているコマは、次のどれかの条件を満たすコマを取ることができます。
- i 行目に置かれている
- j 列目に置かれている
- i+j=a+b となるようなマス (a,b)\ (1\leq a\leq N,1\leq b\leq N) に置かれている
- i-j=a-b となるようなマス (a,b)\ (1\leq a\leq N,1\leq b\leq N) に置かれている
たとえば、マス (4,4) に置かれているコマは、以下の図で青く示されたマスに置かれているコマを取ることができます。

あなたがコマを置くことができるマスがいくつあるか求めてください。
制約
- 1\leq N\leq10 ^ 9
- 1\leq M\leq10 ^ 3
- 1\leq a _ k\leq N,1\leq b _ k\leq N\ (1\leq k\leq M)
- (a _ k,b _ k)\neq(a _ l,b _ l)\ (1\leq k\lt l\leq M)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M a _ 1 b _ 1 a _ 2 b _ 2 \vdots a _ M b _ M
出力
すでに置かれているコマに取られずに自分のコマを置くことができる空マスの個数を出力せよ。
入力例 1
8 6 1 4 2 1 3 8 4 5 5 2 8 3
出力例 1
2
すでに置かれているコマは、以下の図で青く示されたマスに置かれたコマを取ることができます。

よって、あなたがすでに置かれているコマに取られないように自分のコマを置くことができるマスはマス (6,6) とマス (7,7) の 2 つです。
入力例 2
1000000000 1 1 1
出力例 2
999999997000000002
10 ^ {18} マスのうち、置くことができないマスは 1 行目のマス、1 列目のマス、およびマス (1,1), マス (2,2),\ldots, マス (10 ^ 9,10 ^ 9) の 3\times10 ^ 9-2 マスです。
答えが 2 ^ {32} 以上になる場合があることに注意してください。
入力例 3
20 10 1 4 7 11 7 15 8 10 11 6 12 5 13 1 15 2 20 10 20 15
出力例 3
77
Score : 550 points
Problem Statement
There is a grid of N^2 squares with N rows and N columns. Let (i,j) denote the square at the i-th row from the top (1\leq i\leq N) and j-th column from the left (1\leq j\leq N).
Each square is either empty or has a piece placed on it. There are M pieces placed on the grid, and the k-th (1\leq k\leq M) piece is placed on square (a_k,b_k).
You want to place your piece on an empty square in such a way that it cannot be captured by any of the existing pieces.
A piece placed on square (i,j) can capture pieces that satisfy any of the following conditions:
- Placed in row i
- Placed in column j
- Placed on any square (a,b)\ (1\leq a\leq N,1\leq b\leq N) where i+j=a+b
- Placed on any square (a,b)\ (1\leq a\leq N,1\leq b\leq N) where i-j=a-b
For example, a piece placed on square (4,4) can capture pieces placed on the squares shown in blue in the following figure:

How many squares can you place your piece on?
Constraints
- 1\leq N\leq10^9
- 1\leq M\leq10^3
- 1\leq a_k\leq N,1\leq b_k\leq N\ (1\leq k\leq M)
- (a_k,b_k)\neq(a_l,b_l)\ (1\leq k\lt l\leq M)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M a_1 b_1 a_2 b_2 \vdots a_M b_M
Output
Print the number of empty squares where you can place your piece without it being captured by any existing pieces.
Sample Input 1
8 6 1 4 2 1 3 8 4 5 5 2 8 3
Sample Output 1
2
The existing pieces can capture pieces placed on the squares shown in blue in the following figure:

Therefore, you can place your piece on only two squares: squares (6,6) and (7,7).
Sample Input 2
1000000000 1 1 1
Sample Output 2
999999997000000002
Out of 10^{18} squares, the squares that cannot be used are: squares in row 1, squares in column 1, and squares (1,1), (2,2), \ldots, (10^9,10^9), totaling 3\times10^9-2 squares.
Note that the answer may be 2^{32} or greater.
Sample Input 3
20 10 1 4 7 11 7 15 8 10 11 6 12 5 13 1 15 2 20 10 20 15
Sample Output 3
77