Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
0 と 1 からなる長さ 16 の文字列 S が与えられます。
2 以上 16 以下のすべての偶数 i について S の i 文字目が 0 ならば Yes を、
そうでないならば No を出力してください。
制約
- S は
0と1からなる長さ 16 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
2 以上 16 以下のすべての偶数 i について S の i 文字目が 0 ならば Yes を、
そうでないならば No を出力せよ。
入力例 1
1001000000001010
出力例 1
No
S= 1001000000001010 の 4 文字目が 1 であるため、No を出力します。
入力例 2
1010100000101000
出力例 2
Yes
S= 1010100000101000 の偶数番目の文字はすべて 0 であるため、Yes を出力します。
入力例 3
1111111111111111
出力例 3
No
S の偶数文字目はすべて 1 となっています。
特に「すべて 0 」ではないため、No を出力します。
Score : 100 points
Problem Statement
You are given a string S of length 16 consisting of 0 and 1.
If the i-th character of S is 0 for every even number i from 2 through 16, print Yes; otherwise, print No.
Constraints
- S is a string of length 16 consisting of
0and1.
Input
The input is given from Standard Input in the following format:
S
Output
If the i-th character of S is 0 for every even number i from 2 through 16, print Yes; otherwise, print No.
Sample Input 1
1001000000001010
Sample Output 1
No
The 4-th character of S= 1001000000001010 is 1, so you should print No.
Sample Input 2
1010100000101000
Sample Output 2
Yes
Every even-positioned character in S= 1010100000101000 is 0, so you should print Yes.
Sample Input 3
1111111111111111
Sample Output 3
No
Every even-positioned character in S is 1.
Particularly, they are not all 0, so you should print No.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
初項が A、末項が B、公差が D であるような等差数列を出力してください。
なお、そのような等差数列が存在する入力のみが与えられます。
制約
- 1 \leq A \leq B \leq 100
- 1\leq D \leq 100
- 初項が A、末項が B、公差が D であるような等差数列が存在する
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
A B D
出力
初項が A、末項が B、公差が D であるような等差数列の項を順に空白区切りで出力せよ。
入力例 1
3 9 2
出力例 1
3 5 7 9
初項が 3、末項が 9、公差が 2 であるような等差数列は (3,5,7,9) です。
入力例 2
10 10 1
出力例 2
10
初項が 10、末項が 10、公差が 1 であるような等差数列は (10) です。
Score: 100 points
Problem Statement
Print an arithmetic sequence with first term A, last term B, and common difference D.
You are only given inputs for which such an arithmetic sequence exists.
Constraints
- 1 \leq A \leq B \leq 100
- 1 \leq D \leq 100
- There is an arithmetic sequence with first term A, last term B, and common difference D.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A B D
Output
Print the terms of the arithmetic sequence with first term A, last term B, and common difference D, in order, separated by spaces.
Sample Input 1
3 9 2
Sample Output 1
3 5 7 9
The arithmetic sequence with first term 3, last term 9, and common difference 2 is (3,5,7,9).
Sample Input 2
10 10 1
Sample Output 2
10
The arithmetic sequence with first term 10, last term 10, and common difference 1 is (10).
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
プレイヤー 1 、プレイヤー 2 、\ldots 、プレイヤー N と番号がつけられた N 人のプレイヤーがカードゲームで対戦します。
各プレイヤーはカードを 1 枚場に出します。
各カードは色と値の 2 つの属性を持ち、どちらの属性も正整数で表されます。
i = 1, 2, \ldots, N について、プレイヤー i が場に出したカードの色は C_i であり、値は R_i です。
R_1, R_2, \ldots, R_N はすべて異なります。
N 人のプレイヤーの中から 1 人の勝者を下記の方法で決めます。
- 色が T であるカードが 1 枚以上場に出された場合、色が T であるカードのうち値が最大のものを出したプレイヤーが勝者である。
- 色が T であるカードが場に 1 枚も出されなかった場合、プレイヤー 1 が出したカードと同じ色のカードのうち値が最大のものを出したプレイヤーが勝者である。(プレイヤー 1 自身も勝者となり得ることに注意してください。)
勝者となるプレイヤーの番号を出力してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq T \leq 10^9
- 1 \leq C_i \leq 10^9
- 1 \leq R_i \leq 10^9
- i \neq j \implies R_i \neq R_j
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N T C_1 C_2 \ldots C_N R_1 R_2 \ldots R_N
出力
答えを出力せよ。
入力例 1
4 2 1 2 1 2 6 3 4 5
出力例 1
4
色が 2 であるカードが 1 枚以上場に出されています。 よって、色が 2 であるカードのうち値が最大の 5 のカードを出した、プレイヤー 4 が勝者です。
入力例 2
4 2 1 3 1 4 6 3 4 5
出力例 2
1
色が 2 であるカードが 1 枚も場に出されていません。 よって、プレイヤー 1 が出したカードの色と同じ色(すなわち色 1 )のカードのうち値が最大の 6 のカードを出した、プレイヤー 1 が勝者です。
入力例 3
2 1000000000 1000000000 1 1 1000000000
出力例 3
1
Score : 200 points
Problem Statement
N players with ID numbers 1, 2, \ldots, N are playing a card game.
Each player plays one card.
Each card has two parameters: color and rank, both of which are represented by positive integers.
For i = 1, 2, \ldots, N, the card played by player i has a color C_i and a rank R_i.
All of R_1, R_2, \ldots, R_N are different.
Among the N players, one winner is decided as follows.
- If one or more cards with the color T are played, the player who has played the card with the greatest rank among those cards is the winner.
- If no card with the color T is played, the player who has played the card with the greatest rank among the cards with the color of the card played by player 1 is the winner. (Note that player 1 may win.)
Print the ID number of the winner.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq T \leq 10^9
- 1 \leq C_i \leq 10^9
- 1 \leq R_i \leq 10^9
- i \neq j \implies R_i \neq R_j
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N T C_1 C_2 \ldots C_N R_1 R_2 \ldots R_N
Output
Print the answer.
Sample Input 1
4 2 1 2 1 2 6 3 4 5
Sample Output 1
4
Cards with the color 2 are played. Thus, the winner is player 4, who has played the card with the greatest rank, 5, among those cards.
Sample Input 2
4 2 1 3 1 4 6 3 4 5
Sample Output 2
1
No card with the color 2 is played. Thus, the winner is player 1, who has played the card with the greatest rank, 6, among the cards with the color of the card played by player 1 (color 1).
Sample Input 3
2 1000000000 1000000000 1 1 1000000000
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
N 人の AtCoder ユーザーが集まり、 AtCoderじゃんけん2 を行います。i 人目のユーザー名は S_i 、レートは C_i です。
AtCoderじゃんけん2 は以下の手順で行われます。
- それぞれのユーザーに、ユーザー名の辞書順に 0, 1, \dots ,N - 1 の番号を割り当てる。
- N 人のレートの総和を T とする。番号 T \bmod N を割り当てられたユーザーが勝者となる。
勝者のユーザー名を出力してください。
辞書順とは?
辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、英小文字からなる相異なる文字列 S, T の大小を判定するアルゴリズムを以下に説明します。
以下では「 S の i 文字目の文字」を S_i のように表します。また、 S が T より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。
- S, T のうち長さが大きくない方の文字列の長さを L とします。i=1,2,\dots,L に対して S_i と T_i が一致するか調べます。
- S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_j と T_j を比較して、S_j が T_j よりアルファベット順で小さい場合は S \lt T 、そうでない場合は S \gt T と決定して、アルゴリズムを終了します。
- S_i \neq T_i である i が存在しない場合、S と T の長さを比較して、S が T より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。
制約
- 1 \leq N \leq 100
- S_i は英小文字からなる長さ 3 以上 16 以下の文字列
- S_1, S_2, \dots ,S_N は全て異なる
- 1 \leq C_i \leq 4229
- C_i は整数
入力
入力は以下の形式で標準入力から与えられる。
N S_1 C_1 S_2 C_2 \vdots S_N C_N
出力
答えを一行に出力せよ。
入力例 1
3 takahashi 2 aoki 6 snuke 5
出力例 1
snuke
3 人のレートの総和は 13 です。また、それぞれの名前を辞書順に並び替えると、 aoki snuke takahashi の順になるので aoki に番号 0 が、 snuke に 番号 1 が、 takahashi に番号 2 が割り当てられます。
13 \bmod 3 = 1 なので、番号 1 を割り当てられた snuke を出力します。
入力例 2
3 takahashi 2813 takahashixx 1086 takahashix 4229
出力例 2
takahashix
Score : 200 points
Problem Statement
N AtCoder users have gathered to play AtCoder RPS 2. The i-th user's name is S_i and their rating is C_i.
AtCoder RPS 2 is played as follows:
- Assign the numbers 0, 1, \dots, N - 1 to the users in lexicographical order of their usernames.
- Let T be the sum of the ratings of the N users. The user assigned the number T \bmod N is the winner.
Print the winner's username.
What is lexicographical order?
Lexicographical order, simply put, means "the order in which words appear in a dictionary." More precisely, the algorithm to determine the order of two distinct strings S and T consisting of lowercase English letters is as follows:
Here, "the i-th character of S" is denoted as S_i. If S is lexicographically smaller than T, we write S \lt T, and if S is larger, we write S \gt T.
- Let L be the length of the shorter string among S and T. Check if S_i and T_i match for i=1,2,\dots,L.
- If there exists an i such that S_i \neq T_i, let j be the smallest such i. Compare S_j and T_j. If S_j is alphabetically smaller than T_j, then S \lt T. Otherwise, S \gt T. The algorithm ends here.
- If there is no i such that S_i \neq T_i, compare the lengths of S and T. If S is shorter than T, then S \lt T. If S is longer, then S \gt T. The algorithm ends here.
Constraints
- 1 \leq N \leq 100
- S_i is a string consisting of lowercase English letters with length between 3 and 16, inclusive.
- S_1, S_2, \dots, S_N are all distinct.
- 1 \leq C_i \leq 4229
- C_i is an integer.
Input
The input is given from Standard Input in the following format:
N S_1 C_1 S_2 C_2 \vdots S_N C_N
Output
Print the answer on a single line.
Sample Input 1
3 takahashi 2 aoki 6 snuke 5
Sample Output 1
snuke
The sum of the ratings of the three users is 13. Sorting their names in lexicographical order yields aoki, snuke, takahashi, so aoki is assigned number 0, snuke is 1, and takahashi is 2.
Since 13 \bmod 3 = 1, print snuke, who is assigned number 1.
Sample Input 2
3 takahashi 2813 takahashixx 1086 takahashix 4229
Sample Output 2
takahashix
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
上下左右に広がる N\times N のマス目があり、最初全てのマスは白く塗られています。このマス目の上から i 行目、左から j 列目のマスを (i,j) で表します。
高橋君は 1 以上 N 以下の整数 A, B を持っており、次のような操作を行います。
- \max(1-A,1-B)\leq k\leq \min(N-A,N-B) をみたす全ての整数 k について、(A+k,B+k) を黒く塗る。
- \max(1-A,B-N)\leq k\leq \min(N-A,B-1) をみたす全ての整数 k について、(A+k,B-k) を黒く塗る。
この操作を行った後のマス目について、P\leq i\leq Q かつ R\leq j\leq S をみたす各マス (i,j) がそれぞれ何色で塗られているか求めてください。
制約
- 1 \leq N \leq 10^{18}
- 1 \leq A \leq N
- 1 \leq B \leq N
- 1 \leq P \leq Q \leq N
- 1 \leq R \leq S \leq N
- (Q-P+1)\times(S-R+1)\leq 3\times 10^5
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N A B P Q R S
出力
Q-P+1 行出力せよ。
各行は # と . のみからなる長さ S-R+1 の文字列であり、
i 行目の文字列の j 番目の文字が
# であることは (P+i-1,R+j-1) が黒く塗られていることを、
. であることは (P+i-1,R+j-1) が白く塗られていることをさす。
入力例 1
5 3 2 1 5 1 5
出力例 1
...#. #.#.. .#... #.#.. ...#.
1 つめの操作で (2,1), (3,2), (4,3), (5,4) の 4 マスが、
2 つめの操作で (4,1), (3,2), (2,3), (1,4) の 4 マスが黒く塗られます。
よって、P=1, Q=5, R=1, S=5 より、上のように出力します。
入力例 2
5 3 3 4 5 2 5
出力例 2
#.#. ...#
操作によって、
(1,1), (1,5), (2,2), (2,4), (3,3), (4,2), (4,4), (5,1), (5,5) の 9 マスが
黒く塗られます。
P=4, Q=5, R=2, S=5 より、上のように出力します。
入力例 3
1000000000000000000 999999999999999999 999999999999999999 999999999999999998 1000000000000000000 999999999999999998 1000000000000000000
出力例 3
#.# .#. #.#
入力が 32 bit 整数型に収まらないことがあることに注意してください。
Score : 300 points
Problem Statement
There is an N\times N grid with horizontal rows and vertical columns, where all squares are initially painted white. Let (i,j) denote the square at the i-th row and j-th column.
Takahashi has integers A and B, which are between 1 and N (inclusive). He will do the following operations.
- For every integer k such that \max(1-A,1-B)\leq k\leq \min(N-A,N-B), paint (A+k,B+k) black.
- For every integer k such that \max(1-A,B-N)\leq k\leq \min(N-A,B-1), paint (A+k,B-k) black.
In the grid after these operations, find the color of each square (i,j) such that P\leq i\leq Q and R\leq j\leq S.
Constraints
- 1 \leq N \leq 10^{18}
- 1 \leq A \leq N
- 1 \leq B \leq N
- 1 \leq P \leq Q \leq N
- 1 \leq R \leq S \leq N
- (Q-P+1)\times(S-R+1)\leq 3\times 10^5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B P Q R S
Output
Print Q-P+1 lines.
Each line should contain a string of length S-R+1 consisting of # and ..
The j-th character of the string in the i-th line should be # to represent that (P+i-1, R+j-1) is painted black, and . to represent that (P+i-1, R+j-1) is white.
Sample Input 1
5 3 2 1 5 1 5
Sample Output 1
...#. #.#.. .#... #.#.. ...#.
The first operation paints the four squares (2,1), (3,2), (4,3), (5,4) black, and the second paints the four squares (4,1), (3,2), (2,3), (1,4) black.
Thus, the above output should be printed, since P=1, Q=5, R=1, S=5.
Sample Input 2
5 3 3 4 5 2 5
Sample Output 2
#.#. ...#
The operations paint the nine squares (1,1), (1,5), (2,2), (2,4), (3,3), (4,2), (4,4), (5,1), (5,5).
Thus, the above output should be printed, since P=4, Q=5, R=2, S=5.
Sample Input 3
1000000000000000000 999999999999999999 999999999999999999 999999999999999998 1000000000000000000 999999999999999998 1000000000000000000
Sample Output 3
#.# .#. #.#
Note that the input may not fit into a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
3\times3 のマス目に 1 から 9 までの数字が書き込まれており、上から i 行目、左から j 列目 (1\leq i\leq3,1\leq j\leq3) に書き込まれている数字は c _ {i,j} です。
異なるマスに同じ数字が書き込まれている場合もありますが、同じ数字が縦・横・斜めに 3 つ連続して書き込まれていることはありません。 より厳密には、c _ {i,j} について次の条件のすべてが成り立っていることが保証されます。
- どの 1\leq i\leq3 についても、c _ {i,1}=c _ {i,2}=c _ {i,3} ではない
- どの 1\leq j\leq3 についても、c _ {1,j}=c _ {2,j}=c _ {3,j} ではない
- c _ {1,1}=c _ {2,2}=c _ {3,3} ではない
- c _ {3,1}=c _ {2,2}=c _ {1,3} ではない
高橋くんは、それぞれのマスに書かれている数字をランダムな順番で知ります。 高橋くんは、縦・横・斜めの列のうちの 1 つでも次の条件を満たしたときがっかりします。
- はじめに知ったほうの 2 マスに書かれた数字が同じであり、最後に知ったマスに書かれた数字がそれと異なる。
高橋くんががっかりせずにすべてのマスに書かれた数字を知る確率を求めてください。
制約
- c _ {i,j}\in\lbrace1,2,3,4,5,6,7,8,9\rbrace\ (1\leq i\leq3,1\leq j\leq3)
- c _ {i,1}=c _ {i,2}=c _ {i,3} ではない (1\leq i\leq3)
- c _ {1,j}=c _ {2,j}=c _ {3,j} ではない (1\leq j\leq3)
- c _ {1,1}=c _ {2,2}=c _ {3,3} ではない
- c _ {1,3}=c _ {2,2}=c _ {3,1} ではない
入力
入力は以下の形式で標準入力から与えられる。
c _ {1,1} c _ {1,2} c _ {1,3}
c _ {2,1} c _ {2,2} c _ {2,3}
c _ {3,1} c _ {3,2} c _ {3,3}
出力
高橋くんががっかりせずにすべてのマスに書かれた数字を知る確率を 1 行で出力せよ。 真の値からの絶対誤差が 10 ^ {-8} 以下であるとき、正答と判定される。
入力例 1
3 1 9 2 5 6 2 7 1
出力例 1
0.666666666666666666666666666667
例えば、高橋くんが c _ {3,1}=2,c _ {2,1}=2,c _ {1,1}=3 の順に知った場合、高橋くんはがっかりしてしまいます。

対して、高橋くんが c _ {1,1},c _ {1,2},c _ {1,3},c _ {2,1},c _ {2,2},c _ {2,3},c _ {3,1},c _ {3,2},c _ {3,3} の順に数字を知った場合、がっかりすることなくすべての数字を知ることができます。
高橋くんががっかりすることなくすべての数字を知ることができる確率は \dfrac 23 です。 絶対誤差が 10 ^ {-8} 以下であれば正答と判定されるため、0.666666657 や 0.666666676 のように出力しても正解になります。
入力例 2
7 7 6 8 6 8 7 7 6
出力例 2
0.004982363315696649029982363316
入力例 3
3 6 7 1 9 7 5 7 5
出力例 3
0.4
Score : 300 points
Problem Statement
There is a 3\times3 grid with numbers between 1 and 9, inclusive, written in each square. The square at the i-th row from the top and j-th column from the left (1\leq i\leq3,1\leq j\leq3) contains the number c _ {i,j}.
The same number may be written in different squares, but not in three consecutive cells vertically, horizontally, or diagonally. More precisely, it is guaranteed that c _ {i,j} satisfies all of the following conditions.
- c _ {i,1}=c _ {i,2}=c _ {i,3} does not hold for any 1\leq i\leq3.
- c _ {1,j}=c _ {2,j}=c _ {3,j} does not hold for any 1\leq j\leq3.
- c _ {1,1}=c _ {2,2}=c _ {3,3} does not hold.
- c _ {3,1}=c _ {2,2}=c _ {1,3} does not hold.
Takahashi will see the numbers written in each cell in random order. He will get disappointed when there is a line (vertical, horizontal, or diagonal) that satisfies the following condition.
- The first two squares he sees contain the same number, but the last square contains a different number.
Find the probability that Takahashi sees the numbers in all the squares without getting disappointed.
Constraints
- c _ {i,j}\in\lbrace1,2,3,4,5,6,7,8,9\rbrace\ (1\leq i\leq3,1\leq j\leq3)
- c _ {i,1}=c _ {i,2}=c _ {i,3} does not hold for any 1\leq i\leq3.
- c _ {1,j}=c _ {2,j}=c _ {3,j} does not hold for any 1\leq j\leq3.
- c _ {1,1}=c _ {2,2}=c _ {3,3} does not hold.
- c _ {3,1}=c _ {2,2}=c _ {1,3} does not hold.
Input
The input is given from Standard Input in the following format:
c _ {1,1} c _ {1,2} c _ {1,3}
c _ {2,1} c _ {2,2} c _ {2,3}
c _ {3,1} c _ {3,2} c _ {3,3}
Output
Print one line containing the probability that Takahashi sees the numbers in all the squares without getting disappointed. Your answer will be considered correct if the absolute error from the true value is at most 10 ^ {-8}.
Sample Input 1
3 1 9 2 5 6 2 7 1
Sample Output 1
0.666666666666666666666666666667
For example, if Takahashi sees c _ {3,1}=2,c _ {2,1}=2,c _ {1,1}=3 in this order, he will get disappointed.

On the other hand, if Takahashi sees c _ {1,1},c _ {1,2},c _ {1,3},c _ {2,1},c _ {2,2},c _ {2,3},c _ {3,1},c _ {3,2},c _ {3,3} in this order, he will see all numbers without getting disappointed.
The probability that Takahashi sees all the numbers without getting disappointed is \dfrac 23. Your answer will be considered correct if the absolute error from the true value is at most 10 ^ {-8}, so outputs such as 0.666666657 and 0.666666676 would also be accepted.
Sample Input 2
7 7 6 8 6 8 7 7 6
Sample Output 2
0.004982363315696649029982363316
Sample Input 3
3 6 7 1 9 7 5 7 5
Sample Output 3
0.4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋くんはレストランで、 N 品からなる奇妙なフルコースを楽しむことにしました。
このコースのうち i 番目の料理は以下の通りです。
- X_i=0 の場合、美味しさが Y_i の 解毒剤入り の料理
- X_i=1 の場合、美味しさが Y_i の 毒入り の料理
高橋くんが料理を食べると、高橋くんの状態は以下のように変化します。
- 最初、高橋くんはお腹を壊していない。
- 高橋くんが お腹を壊していない 時、
- 解毒剤入り の料理を食べても、高橋くんは お腹を壊していないまま である。
- 毒入り の料理を食べると、高橋くんは お腹を壊す 。
- 高橋くんが お腹を壊している 時、
- 解毒剤入り の料理を食べると、高橋くんは お腹を壊していない状態になる 。
- 毒入り の料理を食べると、高橋くんは 死ぬ 。
コースは以下の流れで進行します。
- i = 1, \ldots, N についてこの順に、以下の処理を繰り返す。
- まず、 i 番目の料理が高橋くんに提供される。
- 次に、 高橋くんはこの料理に対し「食べる」か「下げてもらう」かを選択する。
- 「食べる」を選択した場合、高橋くんは i 番目の料理を食べる。食べた料理に応じて高橋くんの状態も変化する。
- 「下げてもらう」を選択した場合、高橋くんは i 番目の料理を食べない。この料理を後で提供してもらったり何らかの手段で保存したりすることはできない。
- 最後に、 (状態が変化するなら変化後の時点で) 高橋くんが死んでいない場合、
- i \neq N なら次の料理に進む。
- i = N なら高橋くんは生きて退店する。
高橋くんはこのあと重要な仕事があるため、高橋くんは生きて退店しなければなりません。
この条件の下で高橋くんが各料理に対し「食べる」「下げてもらう」を選択したとき、高橋くんが 食べた料理の美味しさの総和として考えられる最大値 ( 但し、何も食べなかった場合は 0 ) を出力してください。
制約
- 入力は全て整数
- 1 \le N \le 3 \times 10^5
- X_i \in \{0,1\}
- つまり、 X_i は 0,1 のどちらかである。
- -10^9 \le Y_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
答えを整数として出力せよ。
入力例 1
5 1 100 1 300 0 -200 1 500 1 300
出力例 1
600
以下のように選択することで食べた料理の美味しさの総和を 600 にでき、これが考えられる最大値です。
- 1 番目の料理を下げてもらう。高橋くんはお腹を壊していません。
- 2 番目の料理を食べる。高橋くんはお腹を壊し、食べた料理の美味しさの総和は 300 となります。
- 3 番目の料理を食べる。高橋くんはお腹を壊していない状態に戻り、食べた料理の美味しさの総和は 100 となります。
- 4 番目の料理を食べる。高橋くんはお腹を壊し、食べた料理の美味しさの総和は 600 となります。
- 5 番目の料理を下げてもらう。高橋くんはお腹を壊しています。
- 最終的に高橋くんは死んでいないので、高橋くんは生きて退店する。
入力例 2
4 0 -1 1 -2 0 -3 1 -4
出力例 2
0
この入力の場合何も食べないことが最善ですが、この場合答えは 0 となります。
入力例 3
15 1 900000000 0 600000000 1 -300000000 0 -700000000 1 200000000 1 300000000 0 -600000000 1 -900000000 1 600000000 1 -100000000 1 -400000000 0 900000000 0 200000000 1 -500000000 1 900000000
出力例 3
4100000000
答えが 32 bit 符号付き整数に収まらない可能性があります。
Score : 400 points
Problem Statement
Takahashi has decided to enjoy a wired full-course meal consisting of N courses in a restaurant.
The i-th course is:
- if X_i=0, an antidotal course with a tastiness of Y_i;
- if X_i=1, a poisonous course with a tastiness of Y_i.
When Takahashi eats a course, his state changes as follows:
- Initially, Takahashi has a healthy stomach.
- When he has a healthy stomach,
- if he eats an antidotal course, his stomach remains healthy;
- if he eats a poisonous course, he gets an upset stomach.
- When he has an upset stomach,
- if he eats an antidotal course, his stomach becomes healthy;
- if he eats a poisonous course, he dies.
The meal progresses as follows.
- Repeat the following process for i = 1, \ldots, N in this order.
- First, the i-th course is served to Takahashi.
- Next, he chooses whether to "eat" or "skip" the course.
- If he chooses to "eat" it, he eats the i-th course. His state also changes depending on the course he eats.
- If he chooses to "skip" it, he does not eat the i-th course. This course cannot be served later or kept somehow.
- Finally, (if his state changes, after the change) if he is not dead,
- if i \neq N, he proceeds to the next course.
- if i = N, he makes it out of the restaurant alive.
An important meeting awaits him, so he must make it out of there alive.
Find the maximum possible sum of tastiness of the courses that he eats (or 0 if he eats nothing) when he decides whether to "eat" or "skip" the courses under that condition.
Constraints
- All input values are integers.
- 1 \le N \le 3 \times 10^5
- X_i \in \{0,1\}
- In other words, X_i is either 0 or 1.
- -10^9 \le Y_i \le 10^9
Input
The input is given from Standard Input in the following format:
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
Output
Print the answer as an integer.
Sample Input 1
5 1 100 1 300 0 -200 1 500 1 300
Sample Output 1
600
The following choices result in a total tastiness of the courses that he eats amounting to 600, which is the maximum possible.
- He skips the 1-st course. He now has a healthy stomach.
- He eats the 2-nd course. He now has an upset stomach, and the total tastiness of the courses that he eats amounts to 300.
- He eats the 3-rd course. He now has a healthy stomach again, and the total tastiness of the courses that he eats amounts to 100.
- He eats the 4-th course. He now has an upset stomach, and the total tastiness of the courses that he eats amounts to 600.
- He skips the 5-th course. He now has an upset stomach.
- In the end, he is not dead, so he makes it out of the restaurant alive.
Sample Input 2
4 0 -1 1 -2 0 -3 1 -4
Sample Output 2
0
For this input, it is optimal to eat nothing, in which case the answer is 0.
Sample Input 3
15 1 900000000 0 600000000 1 -300000000 0 -700000000 1 200000000 1 300000000 0 -600000000 1 -900000000 1 600000000 1 -100000000 1 -400000000 0 900000000 0 200000000 1 -500000000 1 900000000
Sample Output 3
4100000000
The answer may not fit into a 32-bit integer type.
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 点
問題文
英大文字・英小文字からなる長さ 4 の文字列で、以下の 2 条件をともに満たすものを DDoS 型文字列と呼ぶことにします。
- 1,2,4 文字目が英大文字で、3 文字目が英小文字である
- 1,2 文字目が等しい
例えば DDoS, AAaA は DDoS 型文字列であり、ddos, IPoE は DDoS 型文字列ではありません。
英大文字・英小文字および ? からなる文字列 S が与えられます。
S に含まれる ? を独立に英大文字・英小文字に置き換えてできる文字列は、S に含まれる ? の個数を q として 52^q 通りあります。
このうち DDoS 型文字列を部分列に含まないものの個数を 998244353 で割ったあまりを求めてください。
注記
文字列の部分列とは、文字列から 0 個以上の文字を取り除いた後、残りの文字を元の順序で連結して得られる文字列のことをいいます。
例えば、AC は ABC の部分列であり、RE は ECR の部分列ではありません。
制約
- S は英大文字・英小文字および
?からなる - S の長さは 4 以上 3\times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
DD??S
出力例 1
676
? の少なくとも一方が英小文字のとき、DDoS 型文字列を部分列に含みます。
入力例 2
????????????????????????????????????????
出力例 2
858572093
998244353 で割ったあまりを求めてください。
入力例 3
?D??S
出力例 3
136604
Score : 500 points
Problem Statement
A DDoS-type string is a string of length 4 consisting of uppercase and lowercase English letters satisfying both of the following conditions.
- The first, second, and fourth characters are uppercase English letters, and the third character is a lowercase English letter.
- The first and second characters are equal.
For instance, DDoS and AAaA are DDoS-type strings, while neither ddos nor IPoE is.
You are given a string S consisting of uppercase and lowercase English letters and ?.
Let q be the number of occurrences of ? in S. There are 52^q strings that can be obtained by independently replacing each ? in S with an uppercase or lowercase English letter.
Among these strings, find the number of ones that do not contain a DDoS-type string as a subsequence, modulo 998244353.
Notes
A subsequence of a string is a string obtained by removing zero or more characters from the string and concatenating the remaining characters without changing the order.
For instance, AC is a subsequence of ABC, while RE is not a subsequence of ECR.
Constraints
- S consists of uppercase English letters, lowercase English letters, and
?. - The length of S is between 4 and 3\times 10^5, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
DD??S
Sample Output 1
676
When at least one of the ?s is replaced with a lowercase English letter, the resulting string will contain a DDoS-type string as a subsequence.
Sample Input 2
????????????????????????????????????????
Sample Output 2
858572093
Find the count modulo 998244353.
Sample Input 3
?D??S
Sample Output 3
136604