実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
高橋君は N 日間のタスク目標を設定しました。
高橋君は i (1 \leq i \leq N) 日目に A_i 個のタスクを完了することを目標としており、実際には B_i 個のタスクを完了しました。
高橋君が目標より多くのタスクを完了した日数を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq A_i, B_i \leq 100
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
答えを出力せよ。
入力例 1
4 2 8 5 5 5 4 6 7
出力例 1
2
高橋君は 1 日目と 4 日目に目標より多くのタスクを完了しました。
入力例 2
5 1 1 1 1 1 1 1 1 1 1
出力例 2
0
入力例 3
6 1 6 2 5 3 4 4 3 5 2 6 1
出力例 3
3
Score : 100 points
Problem Statement
Takahashi set task goals for N days.
He aimed to complete A_i tasks on day i (1 \leq i \leq N), and actually completed B_i tasks.
Find the number of days when he completed more tasks than his goal.
Constraints
- 1 \leq N \leq 100
- 1 \leq A_i, B_i \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Output the answer.
Sample Input 1
4 2 8 5 5 5 4 6 7
Sample Output 1
2
Takahashi completed more tasks than his goal on the 1st and 4th days.
Sample Input 2
5 1 1 1 1 1 1 1 1 1 1
Sample Output 2
0
Sample Input 3
6 1 6 2 5 3 4 4 3 5 2 6 1
Sample Output 3
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
ある人物の名前 S が与えられます。S の先頭の文字は英大文字であり、先頭以外の文字は英小文字です。
この人物の侍女の名前は、S の頭文字を英小文字に直し、先頭に Of をつけて得られる文字列です。この侍女の名前を答えてください。
制約
- S は長さ 1 以上 10 以下の文字列
- S の先頭の文字は英大文字
- S の先頭以外の文字は英小文字
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを 1 行に出力せよ。
入力例 1
Glen
出力例 1
Ofglen
Glen の頭文字を英小文字に直すと glen となり、さらに先頭に Of をつけると Ofglen となります。
入力例 2
I
出力例 2
Ofi
入力例 3
Fred
出力例 3
Offred
Score : 100 points
Problem Statement
You are given the name S of a certain person. The first character of S is an uppercase English letter, and the other characters are lowercase English letters.
The name of this person's handmaid is the string obtained by converting the first letter of S to lowercase and adding Of to the beginning. Find the name of this handmaid.
Constraints
- S is a string of length between 1 and 10, inclusive.
- The first character of S is an uppercase English letter.
- The characters of S other than the first are lowercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Output the answer on one line.
Sample Input 1
Glen
Sample Output 1
Ofglen
Converting the first letter of Glen to lowercase gives glen, and adding Of to the beginning gives Ofglen.
Sample Input 2
I
Sample Output 2
Ofi
Sample Input 3
Fred
Sample Output 3
Offred
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
英小文字からなる文字列 S が与えられます。S の空でない部分文字列は何種類ありますか?
ただし、部分文字列とは連続する部分列のことを指します。例えば、xxx は yxxxy の部分文字列ですが、xxyxx の部分文字列ではありません。
制約
- S は英小文字からなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
yay
出力例 1
5
S の空でない部分文字列は以下の 5 種類です。
ayayyayay
入力例 2
aababc
出力例 2
17
入力例 3
abracadabra
出力例 3
54
Score: 200 points
Problem Statement
You are given a string S consisting of lowercase English letters. How many different non-empty substrings does S have?
A substring is a contiguous subsequence. For example, xxx is a substring of yxxxy but not of xxyxx.
Constraints
- S is a string of length between 1 and 100, inclusive, consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
yay
Sample Output 1
5
S has the following five different non-empty substrings:
ayayyayay
Sample Input 2
aababc
Sample Output 2
17
Sample Input 3
abracadabra
Sample Output 3
54
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 人が 1 列に並んでおり、前から i 番目に並んでいる人は人 P_i です。
Q 個のクエリを処理して下さい。i 番目のクエリは以下のものです。
- 整数 A_i,B_i が与えられる。人 A_i と人 B_i のうち、より前に並んでいる人の番号を出力せよ。
制約
- 入力は全て整数
- 1\leq N\leq 100
- 1\leq P_i\leq N
- P_i \neq P_j\ (i\neq j)
- 1\leq Q \leq 100
- 1\leq A_i<B_i\leq N
入力
入力は以下の形式で標準入力から与えられる。
N P_1 \ldots P_N Q A_1 B_1 \vdots A_Q B_Q
出力
Q 行出力せよ。i 行目には、i 番目のクエリの答えを出力せよ。
入力例 1
3 2 1 3 3 2 3 1 2 1 3
出力例 1
2 2 1
1 番目のクエリでは、人 2 は前から 1 番目、人 3 は前から 3 番目なので、人 2 がより前にいます。
2 番目のクエリでは、人 1 は前から 2 番目、人 2 は前から 1 番目なので、人 2 がより前にいます。
3 番目のクエリでは、人 1 は前から 2 番目、人 3 は前から 3 番目なので、人 1 がより前にいます。
入力例 2
7 3 7 2 1 6 5 4 13 2 3 1 2 1 3 3 6 3 7 2 4 3 7 1 3 4 7 1 6 2 4 1 3 1 3
出力例 2
3 2 3 3 3 2 3 3 7 1 2 3 3
Score: 200 points
Problem Statement
There are N people standing in a line. The person standing at the i-th position from the front is person P_i.
Process Q queries. The i-th query is as follows:
- You are given integers A_i and B_i. Between person A_i and person B_i, print the person number of the person standing further to the front.
Constraints
- All inputs are integers.
- 1 \leq N \leq 100
- 1 \leq P_i \leq N
- P_i \neq P_j\ (i \neq j)
- 1 \leq Q \leq 100
- 1 \leq A_i < B_i \leq N
Input
The input is given from Standard Input in the following format:
N P_1 \ldots P_N Q A_1 B_1 \vdots A_Q B_Q
Output
Print Q lines. The i-th line should contain the response for the i-th query.
Sample Input 1
3 2 1 3 3 2 3 1 2 1 3
Sample Output 1
2 2 1
In the first query, person 2 is at the first position from the front, and person 3 is at the third position, so person 2 is further to the front.
In the second query, person 1 is at the second position from the front, and person 2 is at the first position, so person 2 is further to the front.
In the third query, person 1 is at the second position from the front, and person 3 is at the third position, so person 1 is further to the front.
Sample Input 2
7 3 7 2 1 6 5 4 13 2 3 1 2 1 3 3 6 3 7 2 4 3 7 1 3 4 7 1 6 2 4 1 3 1 3
Sample Output 2
3 2 3 3 3 2 3 3 7 1 2 3 3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
長さ N の整数列 A,B が与えられます。1 以上 N 以下の整数 i,j を選んで、 A_i + B_j の値を最大化してください。
制約
- 1 \leq N \leq 5 \times 10^5
- |A_i| \leq 10^9\,(i=1,2,\dots,N)
- |B_j| \leq 10^9\,(j=1,2,\dots,N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
出力
A_i + B_j の最大値を出力せよ。
入力例 1
2 -1 5 3 -7
出力例 1
8
(i,j)=(1,1),(1,2),(2,1),(2,2) に対する A_i+B_j の値はそれぞれ 2,-8,8,-2 であり、(i,j)=(2,1) が最大値 8 を達成します。
入力例 2
6 15 12 3 -13 -1 -19 7 17 -13 -10 18 4
出力例 2
33
Score : 250 points
Problem Statement
You are given two integer sequences A and B, each of length N. Choose integers i, j (1 \leq i, j \leq N) to maximize the value of A_i + B_j.
Constraints
- 1 \leq N \leq 5 \times 10^5
- |A_i| \leq 10^9 (i=1,2,\dots,N)
- |B_j| \leq 10^9 (j=1,2,\dots,N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
Output
Print the maximum possible value of A_i + B_j.
Sample Input 1
2 -1 5 3 -7
Sample Output 1
8
For (i,j) = (1,1), (1,2), (2,1), (2,2), the values of A_i + B_j are 2, -8, 8, -2 respectively, and (i,j) = (2,1) achieves the maximum value 8.
Sample Input 2
6 15 12 3 -13 -1 -19 7 17 -13 -10 18 4
Sample Output 2
33
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
数直線上の座標 0.5 に高橋君がいます。
高橋君はこれから N 回の移動を行います。
i 回目の移動では、「正の方向」「負の方向」のいずれかを選び、その方向に L_i 進みます。
高橋君は座標 0 を最大で何回通り過ぎることが出来るでしょうか?
なお、この問題の制約上、座標 0 で完了する移動が生じることはありません。
制約
- 1 \le N \le 20
- 1 \le L_i \le 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N L_1 L_2 \dots L_N
出力
答えを出力せよ。
入力例 1
5 2 5 2 2 1
出力例 1
4
例えば以下のように移動の方向を選択することで座標 0 を 4 回通り過ぎることができ、これが最大です。
- 1 回目の移動で負の方向を選び、 2 進む。高橋君は座標 0.5 から -1.5 へと移動し、座標 0 を通り過ぎる。
- 2 回目の移動で正の方向を選び、 5 進む。高橋君は座標 -1.5 から 3.5 へと移動し、座標 0 を通り過ぎる。
- 3 回目の移動で負の方向を選び、 2 進む。高橋君は座標 3.5 から 1.5 へと移動する。
- 4 回目の移動で負の方向を選び、 2 進む。高橋君は座標 1.5 から -0.5 へと移動し、座標 0 を通り過ぎる。
- 5 回目の移動で正の方向を選び、 1 進む。高橋君は座標 -0.5 から 0.5 へと移動し、座標 0 を通り過ぎる。
入力例 2
5 100 1 2 3 4
出力例 2
1
入力例 3
20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
出力例 3
20
Score : 300 points
Problem Statement
Takahashi is at coordinate 0.5 on a number line.
He will make N moves.
In the i-th move, he chooses either the positive direction or the negative direction, and moves L_i in that direction.
What is the maximum number of times he can pass through coordinate 0?
Under the constraints of this problem, no move will end at coordinate 0.
Constraints
- 1 \le N \le 20
- 1 \le L_i \le 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N L_1 L_2 \dots L_N
Output
Output the answer.
Sample Input 1
5 2 5 2 2 1
Sample Output 1
4
For example, by choosing the directions of movement as follows, he can pass through coordinate 0 four times, which is the maximum.
- In the first move, choose the negative direction and move 2. He moves from coordinate 0.5 to -1.5, passing through coordinate 0.
- In the second move, choose the positive direction and move 5. He moves from coordinate -1.5 to 3.5, passing through coordinate 0.
- In the third move, choose the negative direction and move 2. He moves from coordinate 3.5 to 1.5.
- In the fourth move, choose the negative direction and move 2. He moves from coordinate 1.5 to -0.5, passing through coordinate 0.
- In the fifth move, choose the positive direction and move 1. He moves from coordinate -0.5 to 0.5, passing through coordinate 0.
Sample Input 2
5 100 1 2 3 4
Sample Output 2
1
Sample Input 3
20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output 3
20
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
a, b, c からなる文字列 S が与えられます。
S の空でない部分列であって、同じ文字が隣り合わないものの個数を 998244353 で割った余りを求めてください。
ただし、 2 つの部分列が文字列として一致しても、取り出す位置が異なるならば区別するものとします。
部分列とは
S の部分列とは、S から 0 文字以上を取り除いた後、残りの文字を元の順序で連結して得られる文字列のことをいいます。 例えば、ab, ac は abc の部分列ですが、ca, bb は abc の部分列ではありません。
制約
- S は
a,b,cからなる長さ 1 以上 3 \times 10^5 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
abbc
出力例 1
11
同じ文字が隣合わない部分列は以下の 11 個です。
a( S の 1 文字目)b( S の 2 文字目)b( S の 3 文字目)c( S の 4 文字目)ab( S の 1,2 文字目)ab( S の 1,3 文字目)ac( S の 1,4 文字目)bc( S の 2,4 文字目)bc( S の 3,4 文字目)abc( S の 1,2,4 文字目)abc( S の 1,3,4 文字目)
2 番目と 3 番目のもののように、文字列として一致しても、取り出す位置が異なるならば区別することに注意してください。
入力例 2
cabcabcbcaccacbcbcaabacbacaabccacbccbcacbacbacabcacabcaccaaaaabababcbabacaccabbcacbcbcbcababcbcbabca
出力例 2
378217423
998244353 で割った余りを出力してください。
Score : 400 points
Problem Statement
You are given a string S consisting of a, b, c.
Find the number of non-empty subsequences of S in which no two adjacent characters are the same, modulo 998244353.
Two subsequences are considered distinct if they are taken from different positions, even if they are identical as strings.
What is a subsequence?
A subsequence of S is a string obtained by removing zero or more characters from S and concatenating the remaining characters in their original order. For example,ab, ac are subsequences of abc, but ca, bb are not subsequences of abc.
Constraints
- S is a string of length between 1 and 3 \times 10^5, inclusive, consisting of
a,b,c.
Input
The input is given from Standard Input in the following format:
S
Output
Output the answer.
Sample Input 1
abbc
Sample Output 1
11
The subsequences in which no two adjacent characters are the same are the following 11:
a(the 1st character of S)b(the 2nd character of S)b(the 3rd character of S)c(the 4th character of S)ab(the 1st, 2nd characters of S)ab(the 1st, 3rd characters of S)ac(the 1st, 4th characters of S)bc(the 2nd, 4th characters of S)bc(the 3rd, 4th characters of S)abc(the 1st, 2nd, 4th characters of S)abc(the 1st, 3rd, 4th characters of S)
Note that, as with the 2nd and 3rd entries, two subsequences are considered distinct if they are taken from different positions, even if they are identical as strings.
Sample Input 2
cabcabcbcaccacbcbcaabacbacaabccacbccbcacbacbacabcacabcaccaaaaabababcbabacaccabbcacbcbcbcababcbcbabca
Sample Output 2
378217423
Output the count modulo 998244353.
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
整数列 A とノートがあります。ノートには 10^9 枚のページがあります。
Q 個のクエリが与えられます。各クエリは下記の 4 種類のいずれかです。
ADD x : 整数 x を A の末尾に追加する。
DELETE : A の末尾の要素を削除する。ただし、A が空である場合は何もしない。
SAVE y : ノートの y ページ目に書かれている数列を消し、代わりに現在の A を y ページ目に書き込む。
LOAD z : A をノートの z ページ目に書かれている数列で置き換える。
はじめ、A は空列であり、ノートのすべてのページには空列の情報が書かれています。 その初期状態から、Q 個のクエリを与えられる順に実行し、各クエリの実行直後における A の末尾の要素を出力してください。
なお、入出力の量が多くなる場合があるので、高速な方法で入出力を行うことを推奨します。
制約
- 1 \leq Q \leq 5 \times 10^5
- 1 \leq x, y, z \leq 10^9
- Q, x, y, z は整数
- 与えられるクエリは問題文中の 4 種類のいずれか
入力
入力は以下の形式で標準入力から与えられる。
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
出力
下記の形式にしたがい、i = 1, 2, \ldots, Q について、i 番目までのクエリを実行した直後の A の末尾の要素 X_i を( A が空の場合は X_i := -1 とする)出力せよ。
X_1 X_2 \ldots X_Q
入力例 1
11 ADD 3 SAVE 1 ADD 4 SAVE 2 LOAD 1 DELETE DELETE LOAD 2 SAVE 1 LOAD 3 LOAD 1
出力例 1
3 3 4 4 3 -1 -1 4 4 -1 4
はじめ、A は空列、すなわち A = () であり、ノートのすべてのページには空列の情報が書かれています。
- 1 番目のクエリによって、 A の末尾に 3 が追加され、A = (3) となります。
- 2 番目のクエリによって、ノートの 1 ページ目に書かれた数列が (3) になります。A は変わらず A = (3) です。
- 3 番目のクエリによって、 A の末尾に 4 が追加され、A = (3, 4) となります。
- 4 番目のクエリによって、ノートの 2 ページ目に書かれた数列が (3, 4) になります。A は変わらず A = (3, 4) です。
- 5 番目のクエリによって、 A がノートの 1 ページ目に書かれた数列 (3) で置き換えられ、A = (3) となります。
- 6 番目のクエリによって、 A の末尾の要素が削除され、A = () となります。
- 7 番目のクエリでは、A がすでに空であるので何もしません。A は変わらず A = () です。
- 8 番目のクエリによって、 A がノートの 2 ページ目に書かれた数列 (3, 4) で置き換えられ、A = (3, 4) となります。
- 9 番目のクエリによって、ノートの 1 ページ目に書かれた数列が (3, 4) になります。A は変わらず A = (3, 4) です。
- 10 番目のクエリによって、 A がノートの 3 ページ目に書かれた数列 () で置き換えられ、A = () となります。
- 11 番目のクエリによって、 A がノートの 1 ページ目に書かれた数列 (3, 4) で置き換えられ、A = (3, 4) となります。
入力例 2
21 ADD 4 ADD 3 DELETE ADD 10 LOAD 7 SAVE 5 SAVE 5 ADD 4 ADD 4 ADD 5 SAVE 5 ADD 2 DELETE ADD 1 SAVE 5 ADD 7 ADD 8 DELETE ADD 4 DELETE LOAD 5
出力例 2
4 3 4 10 -1 -1 -1 4 4 5 5 2 5 1 1 7 8 7 4 7 1
Score : 500 points
Problem Statement
We have an integer sequence A and a notebook. The notebook has 10^9 pages.
You are given Q queries. Each query is of one of the following four kinds:
ADD x: append an integer x to the tail of A.
DELETE: remove the last term of A if A is not empty; do nothing otherwise.
SAVE y: erase the sequence recorded on the y-th page of the notebook, and record the current A onto the y-th page.
LOAD z: replace A with the sequence recorded on the z-th page of the notebook.
Initially, A is an empty sequence, and an empty sequence is recorded on each page of the notebook. Process Q queries successively in the given order and print the last term of A after processing each query.
The use of fast input and output methods is recommended because of potentially large input and output.
Constraints
- 1 \leq Q \leq 5 \times 10^5
- 1 \leq x, y, z \leq 10^9
- Q, x, y, and z are integers.
- Each of the given queries is of one of the four kinds in the Problem Statement.
Input
The input is given from Standard Input in the following format:
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Output
For each i = 1, 2, \ldots, Q, let X_i be the last element of A after processing up to the i-th query, or let X_i := -1 if A is empty, and print them in the following format:
X_1 X_2 \ldots X_Q
Sample Input 1
11 ADD 3 SAVE 1 ADD 4 SAVE 2 LOAD 1 DELETE DELETE LOAD 2 SAVE 1 LOAD 3 LOAD 1
Sample Output 1
3 3 4 4 3 -1 -1 4 4 -1 4
Initially, A is an empty sequence, so A = (), and an empty sequence is recorded on each page of the notebook.
- By the 1-st query, 3 is appended to the tail of A, resulting in A = (3).
- By the 2-nd query, the sequence recorded on the 1-st page of the notebook becomes (3). It remains that A = (3).
- By the 3-rd query, 4 is appended to the tail of A, resulting in A = (3, 4).
- By the 4-th query, the sequence recorded on the 2-nd page of the notebook becomes (3, 4). It remains that A = (3, 4).
- By the 5-th query, A is replaced by (3), which is recorded on the 1-st page of the notebook, resulting in A = (3).
- By the 6-th query, the last term of A is removed, resulting in A = ().
- By the 7-th query, nothing happens because A is already empty. It remains that A = ().
- By the 8-th query, A is replaced by (3,4), which is recorded on the 2-nd page of the notebook, resulting in A = (3, 4).
- By the 9-th query, the sequence recorded on the 1-st page of the notebook becomes (3, 4). It remains that A = (3, 4).
- By the 10-th query, A is replaced by (), which is recorded on the 3-rd page of the notebook, resulting in A = ().
- By the 11-th query, A is replaced by (3, 4), which is recorded on the 1-st page of the notebook, resulting in A = (3, 4).
Sample Input 2
21 ADD 4 ADD 3 DELETE ADD 10 LOAD 7 SAVE 5 SAVE 5 ADD 4 ADD 4 ADD 5 SAVE 5 ADD 2 DELETE ADD 1 SAVE 5 ADD 7 ADD 8 DELETE ADD 4 DELETE LOAD 5
Sample Output 2
4 3 4 10 -1 -1 -1 4 4 5 5 2 5 1 1 7 8 7 4 7 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
N 行 N 列のグリッドがあります。上から i 行目、左から j 列目のマスをマス (i,j) と表します。各マスには 1 から 9 までの数字が書かれており、マス (i,j) には数字 A_{i,j} が書かれています。
はじめ、コマがマス (1,1) にあります。また、 S を空文字列とし、以下の操作を 2N-1 回繰り返します:
- S の末尾に現在コマが存在するマスに書かれた数字を連結する。
- コマを現在のマスの下か右に 1 マス移動する。ただし、 2N-1 回目の操作では移動させない。
2N-1 回の操作の後、コマはマス (N,N) に存在し、 S の長さは 2N-1 となります。
最終的な文字列 S を整数としてみた値を M で割ったあまりがスコアとなります。
達成可能なスコアの最大値を求めてください。
制約
- 1\le N\le 20
- 2\le M\le 10^9
- 1\le A_{i,j}\le 9
- 入力される値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
出力
答えを出力せよ。
入力例 1
2 7 1 2 3 1
出力例 1
5
コマの動かし方は以下の 2 通りがあります:
- コマを (1,1),(1,2),(2,2) と順に動かしていく。S=
121となり、この場合のスコアは 121 を 7 で割った 2 である。 - コマを (1,1),(2,1),(2,2) と順に動かしていく。S=
131となり、この場合のスコアは 131 を 7 で割った 5 である。
スコアの最大値は 5 なので、 5 を出力してください。
入力例 2
3 100000 1 2 3 3 5 8 7 1 2
出力例 2
13712
入力例 3
5 402 8 1 3 8 9 8 2 4 1 8 4 1 8 5 9 6 2 1 6 7 6 6 7 7 6
出力例 3
384
Score : 525 points
Problem Statement
There is an N\times N grid. Let cell (i,j) denote the cell in the i-th row from the top and j-th column from the left. Each cell contains a digit from 1 to 9; cell (i,j) contains A_{i,j}.
Initially, a token is on cell (1,1). Let S be an empty string. Repeat the following operation 2N-1 times:
- Append to the end of S the digit in the current cell.
- Move the token one cell down or one cell to the right. However, do not move it in the (2N-1)-th operation.
After 2N-1 operations, the token is on cell (N,N) and the length of S is 2N-1.
Interpret S as an integer. The score is the remainder when this integer is divided by M.
Find the maximum achievable score.
Constraints
- 1 \leq N \leq 20
- 2 \leq M \leq 10^9
- 1 \leq A_{i,j} \leq 9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
Output
Print the answer.
Sample Input 1
2 7 1 2 3 1
Sample Output 1
5
There are two ways to move the token:
- Move through (1,1),(1,2),(2,2). Then S=
121, and the score is the remainder when 121 is divided by 7, which is 2. - Move through (1,1),(2,1),(2,2). Then S=
131, and the score is the remainder when 131 is divided by 7, which is 5.
The maximum score is 5, so print 5.
Sample Input 2
3 100000 1 2 3 3 5 8 7 1 2
Sample Output 2
13712
Sample Input 3
5 402 8 1 3 8 9 8 2 4 1 8 4 1 8 5 9 6 2 1 6 7 6 6 7 7 6
Sample Output 3
384