Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
AtCoder 国では馬のレースが N 個開催されています。
i 個目のレースには A_i 歳以下の馬が出場できます。
N 個のレースのうち、 K 歳の馬が出場できるレースの個数はいくつですか?
制約
- 入力は全て整数
- 1 \le N \le 100
- 1 \le A_i \le 100
- 1 \le K \le 100
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N K
出力
答えを整数として出力せよ。
入力例 1
5 3 1 4 1 5 4
出力例 1
2
- 1 個目のレースには 3 歳以下の馬が出場できます。
- 2 個目のレースには 1 歳以下の馬が出場できます。
- 3 個目のレースには 4 歳以下の馬が出場できます。
- 4 個目のレースには 1 歳以下の馬が出場できます。
- 5 個目のレースには 5 歳以下の馬が出場できます。
5 個のレースのうち、 4 歳の馬が出場できるものは 3,5 個目の 2 つです。
入力例 2
1 1 100
出力例 2
0
K 歳の馬が出場できるレースがひとつもない場合もあります。
入力例 3
15 18 89 31 2 15 93 64 78 58 19 79 59 24 50 30 38
出力例 3
8
Score : 100 points
Problem Statement
In AtCoder Kingdom, there are N horse races being held.
Horses aged A_i or younger can participate in the i-th race.
Among the N races, how many races can a K-year-old horse participate in?
Constraints
- All input values are integers.
- 1 \le N \le 100
- 1 \le A_i \le 100
- 1 \le K \le 100
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N K
Output
Output the answer as an integer.
Sample Input 1
5 3 1 4 1 5 4
Sample Output 1
2
- Horses aged 3 or younger can participate in the 1st race.
- Horses aged 1 or younger can participate in the 2nd race.
- Horses aged 4 or younger can participate in the 3rd race.
- Horses aged 1 or younger can participate in the 4th race.
- Horses aged 5 or younger can participate in the 5th race.
Among the 5 races, a 4-year-old horse can participate in the 3rd and 5th races, which is 2 races.
Sample Input 2
1 1 100
Sample Output 2
0
There may be no races that a K-year-old horse can participate in.
Sample Input 3
15 18 89 31 2 15 93 64 78 58 19 79 59 24 50 30 38
Sample Output 3
8
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋くんは満月が好きです。
今日を 1 日目とすると、今日以降で満月を見られる最初の日は M 日目です。以後は P 日ごと、つまり M+P 日目、M+2P 日目、\ldots に満月を見られます。
1 日目から N 日目まで(両端を含む)の中で、高橋くんが満月を見られる日の数を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq M \leq P \leq 2\times 10^5
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M P
出力
答えを整数として出力せよ。
入力例 1
13 3 5
出力例 1
3
満月を見られる日は、3 日目、8 日目、13 日目、18 日目、\ldots です。
1 日目から 13 日目までの中で高橋くんが満月を見られる日は、3 日目、8 日目、13 日目の 3 個です。
入力例 2
5 6 6
出力例 2
0
高橋くんが満月を見られる日が存在しない場合もあります。
入力例 3
200000 314 318
出力例 3
628
Score : 100 points
Problem Statement
Takahashi likes full moons.
Let today be day 1. The first day on or after today on which he can see a full moon is day M. After that, he can see a full moon every P days, that is, on day M+P, day M+2P, and so on.
Find the number of days between day 1 and day N, inclusive, on which he can see a full moon.
Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq M \leq P \leq 2\times 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M P
Output
Print the answer as an integer.
Sample Input 1
13 3 5
Sample Output 1
3
He can see a full moon on day 3, 8, 13, 18, and so on.
From day 1 to 13, he can see a full moon on three days: day 3, 8, and 13.
Sample Input 2
5 6 6
Sample Output 2
0
There may be no days he can see a full moon.
Sample Input 3
200000 314 318
Sample Output 3
628
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
空でない文字列に対し、先頭の文字を末尾に移動する操作を左シフト、末尾の文字を先頭に移動する操作を右シフトと呼びます。
例えば、abcde に対して左シフトを 1 回行うと bcdea となり、右シフトを 2 回行うと deabc となります。
英小文字からなる空でない文字列 S が与えられます。S に対し左シフトと右シフトをそれぞれ好きな回数(0 回でもよい)行って得られる文字列のうち、辞書順で最小のものと最大のものを求めてください。
辞書順とは?
辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、英小文字からなる相異なる文字列 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 と決定して、アルゴリズムを終了します。
制約
- S は英小文字からなる。
- S の長さは 1 以上 1000 以下である。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
2 行にわたって出力せよ。S に対し左シフトと右シフトをそれぞれ好きな回数(0 回でもよい)行って得られる文字列のうち、辞書順で最小のものと最大のものをそれぞれ S_{\min}, S_{\max} とおいたとき、1 行目には S_{\min} を、2 行目には S_{\max} を出力せよ。
入力例 1
aaba
出力例 1
aaab baaa
操作によって、aaab , aaba , abaa , baaa の 4 通りの文字列を得ることができます。
これらのうち辞書順で最小、最大のものはそれぞれ aaab , baaa です。
入力例 2
z
出力例 2
z z
どのように操作を行っても、得られる文字列は z のみです。
入力例 3
abracadabra
出力例 3
aabracadabr racadabraab
Score : 200 points
Problem Statement
On a non-empty string, a left shift moves the first character to the end of the string, and a right shift moves the last character to the beginning of the string.
For example, a left shift on abcde results in bcdea, and two right shifts on abcde result in deabc.
You are given a non-empty S consisting of lowercase English letters. Among the strings that can be obtained by performing zero or more left shifts and zero or more right shifts on S, find the lexicographically smallest string and the lexicographically largest string.
What is the lexicographical order?
Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings S and T.
Below, let S_i denote the i-th character of S. Also, if S is lexicographically smaller than T, we will denote that fact as S \lt T; if S is lexicographically larger than T, we will denote that fact as S \gt T.
- Let L be the smaller of the lengths of S and T. For each i=1,2,\dots,L, we check whether S_i and T_i are the same.
- If there is an i such that S_i \neq T_i, let j be the smallest such i. Then, we compare S_j and T_j. If S_j comes earlier than T_j in alphabetical order, we determine that S \lt T and quit; if S_j comes later than T_j, we determine that S \gt T and quit.
- If there is no i such that S_i \neq T_i, we compare the lengths of S and T. If S is shorter than T, we determine that S \lt T and quit; if S is longer than T, we determine that S \gt T and quit.
Constraints
- S consists of lowercase English letters.
- The length of S is between 1 and 1000 (inclusive).
Input
Input is given from Standard Input in the following format:
S
Output
Print two lines. The first line should contain S_{\min}, and the second line should contain S_{\max}. Here, S_{\min} and S_{\max} are respectively the lexicographically smallest and largest strings obtained by performing zero or more left shifts and zero or more right shifts on S.
Sample Input 1
aaba
Sample Output 1
aaab baaa
By performing shifts, we can obtain four strings: aaab, aaba, abaa, baaa. The lexicographically smallest and largest among them are aaab and baaa, respectively.
Sample Input 2
z
Sample Output 2
z z
Any sequence of operations results in z.
Sample Input 3
abracadabra
Sample Output 3
aabracadabr racadabraab
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
xy 平面上に、同一直線上にない 3 点 A(x_A,y_A), B(x_B,y_B),C(x_C,y_C) があります。三角形 ABC が直角三角形であるかどうか判定してください。
制約
- -1000\leq x_A,y_A,x_B,y_B,x_C,y_C\leq 1000
- 3 点 A,B,C は同一直線上にない
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
x_A y_A x_B y_B x_C y_C
出力
三角形 ABC が直角三角形であるならば Yes を、そうでないならば No を出力せよ。
入力例 1
0 0 4 0 0 3
出力例 1
Yes
三角形 ABC は直角三角形です。

入力例 2
-4 3 2 1 3 4
出力例 2
Yes
三角形 ABC は直角三角形です。

入力例 3
2 4 -3 2 1 -2
出力例 3
No
三角形 ABC は直角三角形ではありません。

Score : 200 points
Problem Statement
In the xy-plane, there are three points A(x_A, y_A), B(x_B, y_B), and C(x_C, y_C) that are not collinear. Determine whether the triangle ABC is a right triangle.
Constraints
- -1000 \leq x_A, y_A, x_B, y_B, x_C, y_C \leq 1000
- The three points A, B, and C are not collinear.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
x_A y_A x_B y_B x_C y_C
Output
Print Yes if the triangle ABC is a right triangle, and No otherwise.
Sample Input 1
0 0 4 0 0 3
Sample Output 1
Yes
The triangle ABC is a right triangle.

Sample Input 2
-4 3 2 1 3 4
Sample Output 2
Yes
The triangle ABC is a right triangle.

Sample Input 3
2 4 -3 2 1 -2
Sample Output 3
No
The triangle ABC is not a right triangle.

Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
以下の条件を満たす長さ N の整数列を辞書順で小さい方から順に全て出力して下さい。
- i 番目の要素は 1 以上 R_i 以下
- 総和が K の倍数
数列の辞書順とは?
数列 A = (A_1, \ldots, A_{|A|}) が B = (B_1, \ldots, B_{|B|}) より辞書順で真に小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。- |A|<|B| かつ (A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|}) である。
- ある整数 1\leq i\leq \min\{|A|,|B|\} が存在して、下記の 2 つがともに成り立つ。
- (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
- A_i < B_i
制約
- 入力は全て整数
- 1 \le N \le 8
- 2 \le K \le 10
- 1 \le R_i \le 5
入力
入力は以下の形式で標準入力から与えられる。
N K R_1 R_2 \dots R_N
出力
出力すべき数列が X 個あり、そのうち i 個目が A_i=(A_{i,1},A_{i,2},\dots,A_{i,N}) であったとき、答えを以下の形式で出力せよ。
A_{1,1} A_{1,2} \dots A_{1,N}
A_{2,1} A_{2,2} \dots A_{2,N}
\vdots
A_{X,1} A_{X,2} \dots A_{X,N}
入力例 1
3 2 2 1 3
出力例 1
1 1 2 2 1 1 2 1 3
出力すべき数列は 3 個で、辞書順で (1,1,2),(2,1,1),(2,1,3) です。
入力例 2
1 2 1
出力例 2
出力すべき数列が無い場合もあります。
この場合、出力は空で構いません。
入力例 3
5 5 2 3 2 3 2
出力例 3
1 1 1 1 1 1 2 2 3 2 1 3 1 3 2 1 3 2 2 2 1 3 2 3 1 2 1 2 3 2 2 2 1 3 2 2 2 2 2 2 2 2 2 3 1 2 3 1 2 2 2 3 1 3 1 2 3 2 1 2 2 3 2 2 1
Score : 300 points
Problem Statement
Print all integer sequences of length N that satisfy the following conditions, in ascending lexicographical order.
- The i-th element is between 1 and R_i, inclusive.
- The sum of all elements is a multiple of K.
What is lexicographical order for sequences?
A sequence A = (A_1, \ldots, A_{|A|}) is lexicographically smaller than B = (B_1, \ldots, B_{|B|}) if either 1. or 2. below holds:- |A|<|B| and (A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|}).
- There exists an integer 1\leq i\leq \min\{|A|,|B|\} such that both of the following are true:
- (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
- A_i < B_i
Constraints
- All input values are integers.
- 1 \le N \le 8
- 2 \le K \le 10
- 1 \le R_i \le 5
Input
The input is given from Standard Input in the following format:
N K R_1 R_2 \dots R_N
Output
Print the answer in the following format, where X is the number of sequences to print, the i-th of which is A_i=(A_{i,1},A_{i,2},\dots,A_{i,N}):
A_{1,1} A_{1,2} \dots A_{1,N}
A_{2,1} A_{2,2} \dots A_{2,N}
\vdots
A_{X,1} A_{X,2} \dots A_{X,N}
Sample Input 1
3 2 2 1 3
Sample Output 1
1 1 2 2 1 1 2 1 3
There are three sequences to be printed, which are (1,1,2),(2,1,1),(2,1,3) in lexicographical order.
Sample Input 2
1 2 1
Sample Output 2
There may be no sequences to print.
In this case, the output can be empty.
Sample Input 3
5 5 2 3 2 3 2
Sample Output 3
1 1 1 1 1 1 2 2 3 2 1 3 1 3 2 1 3 2 2 2 1 3 2 3 1 2 1 2 3 2 2 2 1 3 2 2 2 2 2 2 2 2 2 3 1 2 3 1 2 2 2 3 1 3 1 2 3 2 1 2 2 3 2 2 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英小文字からなる 2 つの文字列 S, T が与えられます。 次の操作を好きな回数( 0 回でも良い)行うことで、S を T と一致させることができるかを判定してください。
S において同じ文字が 2 文字連続しているところの間に、その文字と同じ文字を 1 つ挿入する。 すなわち、下記の 3 つの手順からなる操作を行う。
- 現在の S の長さを N とし、S = S_1S_2\ldots S_N とする。
- 1 以上 N-1 以下の整数 i であって、S_i = S_{i+1} を満たすものを 1 つ選択する。(ただし、そのような i が存在しない場合は、何もせずに手順 3.をスキップして操作を終了する。)
- S の i 文字目と i+1 文字目の間に文字 S_i(= S_{i+1}) を 1 つ挿入する。その結果、S は長さ N+1 の文字列 S_1S_2\ldots S_i S_i S_{i+1} \ldots S_N となる。
制約
- S と T はそれぞれ英小文字からなる長さ 2 以上 2 \times 10^5 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
S を T と一致させることができる場合は Yes を、そうでない場合は No を出力せよ。
ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。
入力例 1
abbaac abbbbaaac
出力例 1
Yes
下記の 3 回の操作によって、S = abbaac を T = abbbbaaac に一致させることができます。
- まず、S の 2 文字目と 3 文字目の間に
bを挿入する。その結果、S =abbbaacとなる。 - 次に、再び S の 2 文字目と 3 文字目の間に
bを挿入する。その結果、S =abbbbaacとなる。 - 最後に、S の 6 文字目と 7 文字目の間に
aを挿入する。その結果、S =abbbbaaacとなる。
よって、Yes を出力します。
入力例 2
xyzz xyyzz
出力例 2
No
どのように操作を行っても、 S = xyzz を T = xyyzz に一致させることはできません。
よって、No を出力します。
Score : 300 points
Problem Statement
You are given two strings S and T. Determine whether it is possible to make S equal T by performing the following operation some number of times (possibly zero).
Between two consecutive equal characters in S, insert a character equal to these characters. That is, take the following three steps.
- Let N be the current length of S, and S = S_1S_2\ldots S_N.
- Choose an integer i between 1 and N-1 (inclusive) such that S_i = S_{i+1}. (If there is no such i, do nothing and terminate the operation now, skipping step 3.)
- Insert a single copy of the character S_i(= S_{i+1}) between the i-th and (i+1)-th characters of S. Now, S is a string of length N+1: S_1S_2\ldots S_i S_i S_{i+1} \ldots S_N.
Constraints
- Each of S and T is a string of length between 2 and 2 \times 10^5 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
S T
Output
If it is possible to make S equal T, print Yes; otherwise, print No.
Note that the judge is case-sensitive.
Sample Input 1
abbaac abbbbaaac
Sample Output 1
Yes
You can make S = abbaac equal T = abbbbaaac by the following three operations.
- First, insert
bbetween the 2-nd and 3-rd characters of S. Now, S =abbbaac. - Next, insert
bagain between the 2-nd and 3-rd characters of S. Now, S =abbbbaac. - Lastly, insert
abetween the 6-th and 7-th characters of S. Now, S =abbbbaaac.
Thus, Yes should be printed.
Sample Input 2
xyzz xyyzz
Sample Output 2
No
No sequence of operations makes S = xyzz equal T = xyyzz.
Thus, No should be printed.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君はすぬけ君たちを捕まえようとしています。
数直線上の座標 0,1,2,3,4 の 5 箇所に穴があり、すぬけ君たちの巣につながっています。
これから N 匹のすぬけ君が穴から出てきます。i 番目のすぬけ君は時刻 T_i に座標 X_i の穴から出てきて、大きさは A_i であることがわかっています。
高橋君は時刻 0 に座標 0 におり、数直線上を単位時間あたり 1 以下の速さで移動することができます。
すぬけ君が穴から出てきたのと同じ時刻に同じ座標に高橋君がいるとき、かつ、そのときに限り、高橋君はすぬけ君を捕まえることができます。
すぬけ君を捕まえるのにかかる時間は無視できます。
高橋君が適切に行動したとき、捕まえることができるすぬけ君の大きさの合計の最大値を求めてください。
制約
- 1 \leq N \leq 10^5
- 0 < T_1 < T_2 < \ldots < T_N \leq 10^5
- 0 \leq X_i \leq 4
- 1 \leq A_i \leq 10^9
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N T_1 X_1 A_1 T_2 X_2 A_2 \vdots T_N X_N A_N
出力
答えを整数として出力せよ。
入力例 1
3 1 0 100 3 3 10 5 4 1
出力例 1
101
次のように行動するのが最適です。
- 座標 0 で待機し、時刻 1 に 1 番目のすぬけ君を捕まえる
- 座標 4 へ移動し、時刻 5 に 3 番目のすぬけ君を捕まえる
1 番目と 2 番目のすぬけ君を両方とも捕まえることはできないので、これが最大です。
入力例 2
3 1 4 1 2 4 1 3 4 1
出力例 2
0
高橋君はすぬけ君を 1 匹も捕まえることができません。
入力例 3
10 1 4 602436426 2 1 623690081 3 3 262703497 4 4 628894325 5 3 450968417 6 1 161735902 7 1 707723857 8 2 802329211 9 0 317063340 10 2 125660016
出力例 3
2978279323
Score : 400 points
Problem Statement
Takahashi is trying to catch many Snuke.
There are five pits at coordinates 0, 1, 2, 3, and 4 on a number line, connected to Snuke's nest.
Now, N Snuke will appear from the pits. It is known that the i-th Snuke will appear from the pit at coordinate X_i at time T_i, and its size is A_i.
Takahashi is at coordinate 0 at time 0 and can move on the line at a speed of at most 1.
He can catch a Snuke appearing from a pit if and only if he is at the coordinate of that pit exactly when it appears.
The time it takes to catch a Snuke is negligible.
Find the maximum sum of the sizes of Snuke that Takahashi can catch by moving optimally.
Constraints
- 1 \leq N \leq 10^5
- 0 < T_1 < T_2 < \ldots < T_N \leq 10^5
- 0 \leq X_i \leq 4
- 1 \leq A_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N T_1 X_1 A_1 T_2 X_2 A_2 \vdots T_N X_N A_N
Output
Print the answer as an integer.
Sample Input 1
3 1 0 100 3 3 10 5 4 1
Sample Output 1
101
The optimal strategy is as follows.
- Wait at coordinate 0 to catch the first Snuke at time 1.
- Go to coordinate 4 to catch the third Snuke at time 5.
It is impossible to catch both the first and second Snuke, so this is the best he can.
Sample Input 2
3 1 4 1 2 4 1 3 4 1
Sample Output 2
0
Takahashi cannot catch any Snuke.
Sample Input 3
10 1 4 602436426 2 1 623690081 3 3 262703497 4 4 628894325 5 3 450968417 6 1 161735902 7 1 707723857 8 2 802329211 9 0 317063340 10 2 125660016
Sample Output 3
2978279323
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
あなたは 1 以上 6 以下の整数が等確率で出るサイコロと整数 1 を持っています。
あなたは持っている整数が N 未満である間、次の操作を繰り返します。
- サイコロを振り、出た目を x とする。持っている整数に x を掛ける。
全ての操作を終了した時に、持っている整数が N に一致する確率を \text{mod }998244353 で求めてください。
確率 \text{mod }998244353 とは?
求める確率は必ず有理数となることが証明できます。 またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ一つ存在することが証明できます。この R を求めてください。制約
- 2 \leq N \leq 10^{18}
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
全ての操作を終了した時に、持っている整数が N に一致する確率を \text{mod }998244353 で出力せよ。
入力例 1
6
出力例 1
239578645
操作が終了するまでの手順としてあり得る一例を挙げると次のようになります。
- はじめ, 持っている整数は 1 である。
- サイコロを振り, 2 が出る。持っている整数は 1 \times 2 = 2 になる。
- サイコロを振り, 4 が出る。持っている整数は 2 \times 4 = 8 になる。
- 持っている整数が 6 以上になったので操作を終了する。
操作がこのように進んだ場合、操作後に持っている整数は 8 であり N = 6 に一致しません。
操作後に持っている整数が 6 である確率は \frac{7}{25} です。 239578645 \times 25 \equiv 7 \pmod{998244353} より、 239578645 を出力してください。
入力例 2
7
出力例 2
0
どのような目が出ても、操作後に持っている整数が 7 になることはありません。
入力例 3
300
出力例 3
183676961
入力例 4
979552051200000000
出力例 4
812376310
Score : 500 points
Problem Statement
You have an integer 1 and a die that shows integers between 1 and 6 (inclusive) with equal probability.
You repeat the following operation while your integer is strictly less than N:
- Cast a die. If it shows x, multiply your integer by x.
Find the probability, modulo 998244353, that your integer ends up being N.
How to find a probability modulo 998244353?
We can prove that the sought probability is always rational. Additionally, under the constraints of this problem, when that value is represented as \frac{P}{Q} with two coprime integers P and Q, we can prove that there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Find this R.Constraints
- 2 \leq N \leq 10^{18}
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the probability, modulo 998244353, that your integer ends up being N.
Sample Input 1
6
Sample Output 1
239578645
One of the possible procedures is as follows.
- Initially, your integer is 1.
- You cast a die, and it shows 2. Your integer becomes 1 \times 2 = 2.
- You cast a die, and it shows 4. Your integer becomes 2 \times 4 = 8.
- Now your integer is not less than 6, so you terminate the procedure.
Your integer ends up being 8, which is not equal to N = 6.
The probability that your integer ends up being 6 is \frac{7}{25}. Since 239578645 \times 25 \equiv 7 \pmod{998244353}, print 239578645.
Sample Input 2
7
Sample Output 2
0
No matter what the die shows, your integer never ends up being 7.
Sample Input 3
300
Sample Output 3
183676961
Sample Input 4
979552051200000000
Sample Output 4
812376310
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
1\le L\le R\le N を満たす整数の組 (L,R) に対し f(L,R) を以下のように定義します。
- 何も書かれていない黒板に R-L+1 個の整数 A_L,A_{L+1},\ldots,A_R を順に書き込む。
- 以下の操作を黒板に書かれた整数が全て消えるまで繰り返す。
- 整数 l,r を選ぶ。ただし、 l\le r かつ黒板に l 以上 r 以下の整数が全て 1 つ以上書かれているように l,r を選ぶ必要がある。その後、黒板に書かれた l 以上 r 以下の整数を全て消す。
- 黒板に書かれた整数が全て消えるまでに必要な操作回数の最小値を f(L,R) とする。
\displaystyle \sum_{L=1}^N\sum_{R=L}^N f(L,R) を求めてください。
制約
- 1\le N\le 3\times 10^5
- 1\le A_i\le N
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
4 1 3 1 4
出力例 1
16
例えば (L,R)=(1,4) の場合、以下の手順で f(L,R) を計算することができます。
- 黒板に 1,3,1,4 が書かれている。
- (l,r)=(1,1) を選び、黒板に書かれた 1 を全て消す。黒板には 3,4 が書かれた状態になる。
- (l,r)=(3,4) を選び、黒板に書かれた 3,4 を全て消す。黒板は何も書かれていない状態になる。
- 2 回未満の操作で黒板の整数を全て消すことはできないので、f(1,4)=2 である。
同様の計算で、例えば f(2,4)=2, f(1,1)=1 なども分かります。
\displaystyle \sum_{L=1}^N\sum_{R=L}^N f(L,R)=16 なので、 16 を出力してください。
入力例 2
5 3 1 4 2 4
出力例 2
23
入力例 3
10 5 1 10 9 2 5 6 9 1 6
出力例 3
129
Score : 525 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.
For each integer pair (L,R) with 1 \le L \le R \le N, define f(L,R) as follows:
- Start with an empty blackboard. Write the R-L+1 integers A_L, A_{L+1}, \ldots, A_R on the blackboard in order.
- Repeat the following operation until all integers on the blackboard are erased:
- Choose integers l, r with l \le r such that every integer from l through r appears at least once on the blackboard. Then, erase all integers from l through r that are on the blackboard.
- Let f(L,R) be the minimum number of such operations needed to erase all the integers from the blackboard.
Find \displaystyle \sum_{L=1}^N \sum_{R=L}^N f(L,R).
Constraints
- 1 \le N \le 3 \times 10^5
- 1 \le A_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
Print the answer.
Sample Input 1
4 1 3 1 4
Sample Output 1
16
For example, in the case of (L,R)=(1,4):
- The blackboard has 1,3,1,4.
- Choose (l,r)=(1,1) and erase all occurrences of 1. The blackboard now has 3,4.
- Choose (l,r)=(3,4) and erase all occurrences of 3 and 4. The blackboard becomes empty.
- It cannot be done in fewer than two operations, so f(1,4) = 2.
Similarly, you can find f(2,4)=2, f(1,1)=1, etc.
\displaystyle \sum_{L=1}^N \sum_{R=L}^N f(L,R) = 16, so print 16.
Sample Input 2
5 3 1 4 2 4
Sample Output 2
23
Sample Input 3
10 5 1 10 9 2 5 6 9 1 6
Sample Output 3
129