Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
縦 H 行、横 W 列のマス目があり、このうち上から i 個目、左から j 個目のマスを (i,j) と呼びます。
このとき、マス (R,C) に辺で隣接するマスの個数を求めてください。
ただし、ある 2 つのマス (a,b),(c,d) が辺で隣接するとは、 |a-c|+|b-d|=1 (|x| を x の絶対値とする) であることを言います。
制約
- 入力は全て整数
- 1 \le R \le H \le 10
- 1 \le C \le W \le 10
入力
入力は以下の形式で標準入力から与えられる。
H W R C
出力
答えを整数として出力せよ。
入力例 1
3 4 2 2
出力例 1
4
入出力例 1,2,3 に対する説明は、出力例 3 の下にまとめて示します。
入力例 2
3 4 1 3
出力例 2
3
入力例 3
3 4 3 4
出力例 3
2
H=3,W=4 のとき、マス目は以下のようになります。
- 入力例 1 について、マス (2,2) に隣接するマスは 4 つです。
- 入力例 2 について、マス (1,3) に隣接するマスは 3 つです。
- 入力例 3 について、マス (3,4) に隣接するマスは 2 つです。

入力例 4
1 10 1 5
出力例 4
2
入力例 5
8 1 8 1
出力例 5
1
入力例 6
1 1 1 1
出力例 6
0
Score : 100 points
Problem Statement
There is a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.
Find the number of squares that share a side with Square (R, C).
Here, two squares (a,b) and (c,d) are said to share a side if and only if |a-c|+|b-d|=1 (where |x| denotes the absolute value of x).
Constraints
- All values in input are integers.
- 1 \le R \le H \le 10
- 1 \le C \le W \le 10
Input
Input is given from Standard Input in the following format:
H W R C
Output
Print the answer as an integer.
Sample Input 1
3 4 2 2
Sample Output 1
4
We will describe Sample Inputs/Outputs 1,2, and 3 at once below Sample Output 3.
Sample Input 2
3 4 1 3
Sample Output 2
3
Sample Input 3
3 4 3 4
Sample Output 3
2
When H=3 and W=4, the grid looks as follows.
- For Sample Input 1, there are 4 squares adjacent to Square (2,2).
- For Sample Input 2, there are 3 squares adjacent to Square (1,3).
- For Sample Input 3, there are 2 squares adjacent to Square (3,4).

Sample Input 4
1 10 1 5
Sample Output 4
2
Sample Input 5
8 1 8 1
Sample Output 5
1
Sample Input 6
1 1 1 1
Sample Output 6
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
野球少年の高橋君は今年とても良い子にしていたので、サンタさんはバットかグローブのうち値段が高い方を高橋君にプレゼントすることにしました。
バットの値段が B 円、グローブの値段が G 円 (B\neq G) のとき、サンタさんが高橋君にプレゼントするのはどちらですか?
制約
- B,G は 1 以上 1000 以下の相異なる整数
入力
入力は以下の形式で標準入力から与えられる。
B G
出力
サンタさんが高橋君にプレゼントするのがバットであるとき Bat を、グローブであるとき Glove を出力せよ。
入力例 1
300 100
出力例 1
Bat
バットの方がグローブより値段が高いので、サンタさんは高橋君にバットをプレゼントします。
入力例 2
334 343
出力例 2
Glove
グローブの方がバットより値段が高いので、サンタさんは高橋君にグローブをプレゼントします。
Score : 100 points
Problem Statement
Takahashi, a young baseball enthusiast, has been a very good boy this year, so Santa has decided to give him a bat or a glove, whichever is more expensive.
If a bat costs B yen and a glove costs G yen (B\neq G), which one will Santa give to Takahashi?
Constraints
- B and G are different integers between 1 and 1000, inclusive.
Input
The input is given from Standard Input in the following format:
B G
Output
If Santa gives Takahashi a bat, print Bat; if Santa gives him a glove, print Glove.
Sample Input 1
300 100
Sample Output 1
Bat
The bat is more expensive than the glove, so Santa will give Takahashi the bat.
Sample Input 2
334 343
Sample Output 2
Glove
The glove is more expensive than the bat, so Santa will give Takahashi the glove.
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.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
H 行 W 列のブロックからなる長方形状のチョコレートがあります。
Q 個のクエリが与えられるので、順に処理したときの各クエリの答えを求めてください。各クエリは、以下のいずれかの形式です。
-
タイプ 1 : 整数 R が与えられる。下 R 行のチョコレートのブロックの個数を求め、それらを食べる。
-
タイプ 2 : 整数 C が与えられる。右 C 列のチョコレートのブロックの個数を求め、それらを食べる。
なお、クエリを順に処理したとき、各クエリを処理した後もチョコレートは長方形状であり、タイプ 1 のクエリを処理する直前の時点でチョコレートは R + 1 行以上存在し、タイプ 2 のクエリを処理する直前の時点でチョコレートは C + 1 列以上存在します。
制約
- 2 \leq H, W \leq 100
- 1 \leq Q \leq 100
- タイプ 1 のクエリについて、1 \leq R
- クエリを順に処理したとき、タイプ 1 のクエリを処理する直前にチョコレートは R + 1 行以上存在する
- タイプ 2 のクエリについて、1 \leq C
- クエリを順に処理したとき、タイプ 2 のクエリを処理する直前にチョコレートは C + 1 列以上存在する
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
ただし、\text{query}_i は i 番目のクエリであり、以下のいずれかの形式で与えられる。
1 R
2 C
出力
Q 行出力せよ。 i (1 \leq i \leq Q) 行目には i 番目のクエリに対する答えを出力せよ。
入力例 1
7 9 5 2 4 1 3 2 1 2 1 1 3
出力例 1
28 15 4 4 9
はじめ、チョコレートは 7 行 9 列の長方形状です。
1 番目のクエリでは、右 4 列のチョコレートの個数は 28 個であるため 28 を出力します。チョコレートは 7 行 5 列となります。
2 番目のクエリでは、下 3 行のチョコレートの個数は 15 個であるため 15 を出力します。チョコレートは 4 行 5 列となります。
3 番目のクエリでは、右 1 列のチョコレートの個数は 4 個であるため 4 を出力します。チョコレートは 4 行 4 列となります。
4 番目のクエリでは、右 1 列のチョコレートの個数は 4 個であるため 4 を出力します。チョコレートは 4 行 3 列となります。
5 番目のクエリでは、下 3 行のチョコレートの個数は 9 個であるため 9 を出力します。チョコレートは 1 行 3 列となります。
Score : 200 points
Problem Statement
There is a rectangular chocolate consisting of H rows and W columns of blocks.
You are given Q queries; process them in order and find the answer to each query. Each query is in one of the following formats:
-
Type 1: An integer R is given. Find the number of chocolate blocks in the bottom R rows, then eat them.
-
Type 2: An integer C is given. Find the number of chocolate blocks in the rightmost C columns, then eat them.
When the queries are processed in order, the chocolate remains rectangular after each query is processed, and it has at least R + 1 rows immediately before processing a type 1 query and has at least C + 1 columns immediately before processing a type 2 query.
Constraints
- 2 \leq H, W \leq 100
- 1 \leq Q \leq 100
- For type 1 queries, 1 \leq R.
- When the queries are processed in order, the chocolate has at least R + 1 rows immediately before processing a type 1 query.
- For type 2 queries, 1 \leq C.
- When the queries are processed in order, the chocolate has at least C + 1 columns immediately before processing a type 2 query.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Here, \text{query}_i is the i-th query, given in one of the following formats:
1 R
2 C
Output
Output Q lines. The i-th line (1 \leq i \leq Q) should contain the answer to the i-th query.
Sample Input 1
7 9 5 2 4 1 3 2 1 2 1 1 3
Sample Output 1
28 15 4 4 9
Initially, the chocolate is a rectangle with 7 rows and 9 columns.
For the first query, the number of chocolate blocks in the rightmost 4 columns is 28, so output 28. The chocolate becomes 7 rows and 5 columns.
For the second query, the number of chocolate blocks in the bottom 3 rows is 15, so output 15. The chocolate becomes 4 rows and 5 columns.
For the third query, the number of chocolate blocks in the rightmost 1 column is 4, so output 4. The chocolate becomes 4 rows and 4 columns.
For the fourth query, the number of chocolate blocks in the rightmost 1 column is 4, so output 4. The chocolate becomes 4 rows and 3 columns.
For the fifth query, the number of chocolate blocks in the bottom 3 rows is 9, so output 9. The chocolate becomes 1 row and 3 columns.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
非負整数 n が次の条件を満たすとき、n を 良い整数 と呼びます。
- n を 10 進法で表したときに、偶数の数字 (0, 2, 4, 6, 8) のみが登場する。
例えば 0、68 および 2024 は良い整数です。
整数 N が与えられます。良い整数のうち小さい方から N 番目の整数を求めてください。
制約
- 1 \leq N \leq 10^{12}
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
小さい方から N 番目の良い整数を出力せよ。
入力例 1
8
出力例 1
24
良い整数を小さい方から順に並べると 0, 2, 4, 6, 8, 20, 22, 24, 26, 28, \dots となります。
小さい方から 8 番目の良い整数は 24 なので、これを出力します。
入力例 2
133
出力例 2
2024
入力例 3
31415926535
出力例 3
2006628868244228
Score: 300 points
Problem Statement
A non-negative integer n is called a good integer when it satisfies the following condition:
- All digits in the decimal notation of n are even numbers (0, 2, 4, 6, and 8).
For example, 0, 68, and 2024 are good integers.
You are given an integer N. Find the N-th smallest good integer.
Constraints
- 1 \leq N \leq 10^{12}
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the N-th smallest good integer.
Sample Input 1
8
Sample Output 1
24
The good integers in ascending order are 0, 2, 4, 6, 8, 20, 22, 24, 26, 28, \dots.
The eighth smallest is 24, which should be printed.
Sample Input 2
133
Sample Output 2
2024
Sample Input 3
31415926535
Sample Output 3
2006628868244228
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の整数列 A=(A _ 1,A _ 2,\ldots,A _ N) が与えられます。
整数の 2 つ組 (i,j)\ (1\le i\lt j\le N) のうち、j-i=A _ i+A _ j を満たすものがいくつあるか求めてください。
制約
- 1\le N\le2\times10 ^ 5
- 1\le A _ i\le2\times10 ^ 5\ (1\le i\le N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N
出力
答えを出力せよ。
入力例 1
9 3 1 4 1 5 9 2 6 5
出力例 1
3
例えば、(i,j)=(4,7) とすると、j-i=7-4=3 かつ A _ i+A _ j=1+2=3 が成り立つので、j-i=A _ i+A _ j です。
一方で、(i,j)=(3,8) とすると、j-i=8-3=5 かつ A _ i+A _ j=4+6=10 となるので、j-i\ne A _ i+A _ j です。
(i,j)=(1,9),(2,4),(4,7) の 3 組だけが条件を満たすので、3 を出力してください。
入力例 2
3 123456 123456 123456
出力例 2
0
条件を満たす組が存在しない場合もあります。
入力例 3
30 8 3 6 4 9 6 5 6 5 6 3 4 7 3 7 4 9 8 5 8 3 6 8 8 4 5 5 5 6 5
出力例 3
17
Score : 300 points
Problem Statement
You are given an integer sequence A=(A _ 1,A _ 2,\ldots,A _ N) of length N.
Find how many pairs of integers (i,j)\ (1\le i\lt j\le N) satisfy j-i=A _ i+A _ j.
Constraints
- 1\le N\le2\times10 ^ 5
- 1\le A _ i\le2\times10 ^ 5\ (1\le i\le N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A _ 1 A _ 2 \ldots A _ N
Output
Output the answer.
Sample Input 1
9 3 1 4 1 5 9 2 6 5
Sample Output 1
3
For example, when (i,j)=(4,7), we have j-i=7-4=3 and A _ i+A _ j=1+2=3, so j-i=A _ i+A _ j.
In contrast, when (i,j)=(3,8), we have j-i=8-3=5 and A _ i+A _ j=4+6=10, so j-i\ne A _ i+A _ j.
Only the three pairs (i,j)=(1,9),(2,4),(4,7) satisfy the condition, so output 3.
Sample Input 2
3 123456 123456 123456
Sample Output 2
0
There may be no pairs that satisfy the condition.
Sample Input 3
30 8 3 6 4 9 6 5 6 5 6 3 4 7 3 7 4 9 8 5 8 3 6 8 8 4 5 5 5 6 5
Sample Output 3
17
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
1, 2, \ldots, 2N と番号づけられた 2N 人の人が舞踏会に参加します。 彼らは N 個の 2 人組にわかれてダンスを踊ります。
2 人組を構成する人のうち、番号の小さい方の人が人 i 、番号の大きい方の人が人 j のとき、
その 2 人組の「相性」は A_{i, j} です。
N 個の 2 人組の相性がそれぞれ B_1, B_2, \ldots, B_N であるとき、
「舞踏会全体の楽しさ」はそれらのビットごとの排他的論理和である B_1 \oplus B_2 \oplus \cdots \oplus B_N です。
「 2N 人の参加者が N 個の 2 人組に分かれる方法」を自由に選べるとき、「舞踏会全体の楽しさ」としてあり得る最大値を出力してください。
制約
- 1 \leq N \leq 8
- 0 \leq A_{i, j} < 2^{30}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_{1, 2} A_{1, 3} A_{1, 4} \cdots A_{1, 2N}
A_{2, 3} A_{2, 4} \cdots A_{2, 2N}
A_{3, 4} \cdots A_{3, 2N}
\vdots
A_{2N-1, 2N}
出力
舞踏会全体の楽しさとしてあり得る最大値を出力せよ。
入力例 1
2 4 0 1 5 3 2
出力例 1
6
人 i と人 j からなる 2 人組を \lbrace i, j\rbrace で表します。 4 人が 2 個の 2 人組にわかれる方法は下記の 3 通りです。
- \lbrace 1, 2\rbrace, \lbrace 3, 4\rbrace という 2 組にわかれる。 このとき、舞踏会全体の楽しさは A_{1, 2} \oplus A_{3, 4} = 4 \oplus 2 = 6 です。
- \lbrace 1, 3\rbrace, \lbrace 2, 4\rbrace という 2 組にわかれる。 このとき、舞踏会全体の楽しさは A_{1, 3} \oplus A_{2, 4} = 0 \oplus 3 = 3 です。
- \lbrace 1, 4\rbrace, \lbrace 2, 3\rbrace という 2 組にわかれる。 このとき、舞踏会全体の楽しさは A_{1, 4} \oplus A_{2, 3} = 1 \oplus 5 = 4 です。
よって、舞踏会全体の楽しさとしてあり得る最大値は 6 です。
入力例 2
1 5
出力例 2
5
人 1 と人 2 からなる 2 人組のみが作られ、このときの舞踏会全体の楽しさは 5 です。
入力例 3
5 900606388 317329110 665451442 1045743214 260775845 726039763 57365372 741277060 944347467 369646735 642395945 599952146 86221147 523579390 591944369 911198494 695097136 138172503 571268336 111747377 595746631 934427285 840101927 757856472 655483844 580613112 445614713 607825444 252585196 725229185 827291247 105489451 58628521 1032791417 152042357 919691140 703307785 100772330 370415195 666350287 691977663 987658020 1039679956 218233643 70938785
出力例 3
1073289207
Score : 400 points
Problem Statement
2N people numbered 1, 2, \ldots, 2N attend a ball. They will group into N pairs and have a dance.
If Person i and Person j pair up, where i is smaller than j, the affinity of that pair is A_{i, j}.
If the N pairs have the affinity of B_1, B_2, \ldots, B_N, the total fun of the ball is the bitwise XOR of them: B_1 \oplus B_2 \oplus \cdots \oplus B_N.
Print the maximum possible total fun of the ball when the 2N people can freely group into N pairs.
Constraints
- 1 \leq N \leq 8
- 0 \leq A_{i, j} < 2^{30}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_{1, 2} A_{1, 3} A_{1, 4} \cdots A_{1, 2N}
A_{2, 3} A_{2, 4} \cdots A_{2, 2N}
A_{3, 4} \cdots A_{3, 2N}
\vdots
A_{2N-1, 2N}
Output
Print the maximum possible total fun of the ball.
Sample Input 1
2 4 0 1 5 3 2
Sample Output 1
6
Let \lbrace i, j\rbrace denote a pair of Person i and Person j. There are three ways for the four people to group into two pairs, as follows.
- Group into \lbrace 1, 2\rbrace, \lbrace 3, 4\rbrace. The total fun of the ball here is A_{1, 2} \oplus A_{3, 4} = 4 \oplus 2 = 6.
- Group into \lbrace 1, 3\rbrace, \lbrace 2, 4\rbrace. The total fun of the ball here is A_{1, 3} \oplus A_{2, 4} = 0 \oplus 3 = 3.
- Group into \lbrace 1, 4\rbrace, \lbrace 2, 3\rbrace. The total fun of the ball here is A_{1, 4} \oplus A_{2, 3} = 1 \oplus 5 = 4.
Therefore, the maximum possible total fun of the ball is 6.
Sample Input 2
1 5
Sample Output 2
5
There will be just a pair of Person 1 and Person 2, where the total fun of the ball is 5.
Sample Input 3
5 900606388 317329110 665451442 1045743214 260775845 726039763 57365372 741277060 944347467 369646735 642395945 599952146 86221147 523579390 591944369 911198494 695097136 138172503 571268336 111747377 595746631 934427285 840101927 757856472 655483844 580613112 445614713 607825444 252585196 725229185 827291247 105489451 58628521 1032791417 152042357 919691140 703307785 100772330 370415195 666350287 691977663 987658020 1039679956 218233643 70938785
Sample Output 3
1073289207
Time Limit: 3 sec / Memory Limit: 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
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 個の文字列 S _ 1,S _ 2,\ldots,S _ N が与えられます。 S _ i\ (1\leq i\leq N) は英小文字からなる長さ 10 以下の空でない文字列で、互いに異なります。
先手太郎君と後手次郎君がしりとりをします。 このしりとりでは、先手太郎君と後手次郎君の手番が交互に訪れます。 はじめの手番は先手太郎君の手番です。 それぞれのプレイヤーは自分の手番において整数 i\ (1\leq i\leq N) を 1 つ選びます。 このとき、i は次の 2 つの条件を満たしていなければなりません。
- i は、しりとりが開始してからこれまでの 2 人の手番で選ばれたどの整数とも異なる
- この手番がしりとりの最初の手番であるか、直前に選ばれた整数を j として、S _ j の最後の文字と S _ i の最初の文字が等しい
条件を満たす i を選べなくなったプレイヤーの負けで、負けなかったプレイヤーの勝ちです。
2 人が最適に行動したときに勝つのはどちらかを判定してください。
制約
- 1 \leq N \leq 16
- N は整数
- S _ i\ (1\leq i\leq N) は英小文字からなる長さ 10 以下の空でない文字列
- S _ i\neq S _ j\ (1\leq i\lt j\leq N)
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
2 人が最適に行動したとき、先手太郎君が勝つなら First、後手次郎君が勝つなら Second と出力せよ。
入力例 1
6 enum float if modint takahashi template
出力例 1
First
例えば、ゲームは以下のように進行します。 この進行例では 2 人の行動が必ずしも最適とは限らないことに注意してください。
- 先手太郎君が i=3 を選ぶ。S _ i=
ifである。 - 後手次郎君が i=2 を選ぶ。S _ i=
floatであり、ifの最後の文字とfloatの最初の文字は等しい。 - 先手太郎君が i=5 を選ぶ。S _ i=
takahashiであり、floatの最後の文字とtakahashiの最初の文字は等しい。 - 後手次郎君は i\neq2,3,5 であって S _ i の最初の文字が
iと等しいものを選べないため、負ける。
このとき、先手太郎君が勝ちます。
入力例 2
10 catch chokudai class continue copy exec havoc intrinsic static yucatec
出力例 2
Second
入力例 3
16 mnofcmzsdx lgeowlxuqm ouimgdjxlo jhwttcycwl jbcuioqbsj mdjfikdwix jhvdpuxfil peekycgxco sbvxszools xuuqebcrzp jsciwvdqzl obblxzjhco ptobhnpfpo muizaqtpgx jtgjnbtzcl sivwidaszs
出力例 3
First
Score : 500 points
Problem Statement
You are given N strings S _ 1,S _ 2,\ldots,S _ N. S _ i\ (1\leq i\leq N) is a non-empty string of length at most 10 consisting of lowercase English letters, and the strings are pairwise distinct.
Taro the First and Jiro the Second play a word-chain game. In this game, the two players take alternating turns, with Taro the First going first. In each player's turn, the player chooses an integer i\ (1\leq i\leq N), which should satisfy the following two conditions:
- i is different from any integer chosen by the two players so far since the game started;
- the current turn is the first turn of the game, or the last character of S_j equals the first character of S_i, where j is the last integer chosen.
The player who is unable to choose a conforming i loses; the other player wins.
Determine which player will win if the two players play optimally.
Constraints
- 1 \leq N \leq 16
- N is an integer.
- S _ i\ (1\leq i\leq N) is a non-empty string of length at most 10 consisting of lowercase English letters.
- S _ i\neq S _ j\ (1\leq i\lt j\leq N)
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print First if Taro the First wins when the two players play optimally; print Second if Jiro the Second wins.
Sample Input 1
6 enum float if modint takahashi template
Sample Output 1
First
For example, the game progresses as follows. Note that the two players may not be playing optimally in this example.
- Taro the First chooses i=3. S _ i=
if. - Jiro the Second chooses i=2. S _ i=
float, and the last character ofifequals the first character offloat. - Taro the First chooses i=5. S _ i=
takahashi, and the last character offloatequals the first character oftakahashi. - Jiro the Second is unable to choose i\neq2,3,5 such that S _ i starts with
i, so he loses.
In this case, Taro the First wins.
Sample Input 2
10 catch chokudai class continue copy exec havoc intrinsic static yucatec
Sample Output 2
Second
Sample Input 3
16 mnofcmzsdx lgeowlxuqm ouimgdjxlo jhwttcycwl jbcuioqbsj mdjfikdwix jhvdpuxfil peekycgxco sbvxszools xuuqebcrzp jsciwvdqzl obblxzjhco ptobhnpfpo muizaqtpgx jtgjnbtzcl sivwidaszs
Sample Output 3
First