実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
5 枚のカードがあり、それぞれのカードには整数 A,B,C,D,E が書かれています。
この 5 枚組は以下の条件を満たすとき、またそのときに限って、フルハウスであると呼ばれます。
- 同じ数が書かれたカード 3 枚と、別の同じ数が書かれたカード 2 枚からなる。
5 枚組がフルハウスであるか判定してください。
制約
- 1 \leq A,B,C,D,E\leq 13
- A,B,C,D,E 全てが同じ値ではない
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
A B C D E
出力
フルハウスである場合 Yes を、そうでないとき No を出力せよ。
入力例 1
1 2 1 2 1
出力例 1
Yes
1 が書かれたカード 3 枚と、2 が書かれたカード 2 枚からなるため、これはフルハウスです。
入力例 2
12 12 11 1 2
出力例 2
No
フルハウスの条件を満たしません。
Score : 100 points
Problem Statement
We have five cards with integers A, B, C, D, and E written on them, one on each card.
This set of five cards is called a Full house if and only if the following condition is satisfied:
- the set has three cards with a same number written on them, and two cards with another same number written on them.
Determine whether the set is a Full house.
Constraints
- 1 \leq A,B,C,D,E\leq 13
- Not all of A, B, C, D, and E are the same.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
A B C D E
Output
If the set is a Full house, print Yes; otherwise, print No.
Sample Input 1
1 2 1 2 1
Sample Output 1
Yes
The set has three cards with 1 written on them and two cards with 2 written on them, so it is a Full house.
Sample Input 2
12 12 11 1 2
Sample Output 2
No
The condition is not satisfied.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
長さ N の整数列 A=(A_1,A_2,\dots,A_N) と整数 X が与えられます。
X が A に含まれるか判定してください。
制約
- 入力は全て整数
- 1 \le N \le 100
- 1 \le A_i \le 100
- 1 \le X \le 100
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N X
出力
X が A に含まれるなら Yes 、含まれないなら No と出力せよ。
入力例 1
5 3 1 4 1 5 4
出力例 1
Yes
A=(3,1,4,1,5), X=4 であり、 X は A に含まれます。
入力例 2
4 100 100 100 100 100
出力例 2
Yes
X が複数回 A に含まれる場合もあります。
入力例 3
6 2 3 5 7 11 13 1
出力例 3
No
Score : 100 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N and an integer X.
Determine whether X is contained in A.
Constraints
- All input values are integers.
- 1 \le N \le 100
- 1 \le A_i \le 100
- 1 \le X \le 100
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N X
Output
If X is contained in A, print Yes; otherwise, print No.
Sample Input 1
5 3 1 4 1 5 4
Sample Output 1
Yes
We have A=(3,1,4,1,5) and X=4; X is contained in A.
Sample Input 2
4 100 100 100 100 100
Sample Output 2
Yes
X may be contained in A multiple times.
Sample Input 3
6 2 3 5 7 11 13 1
Sample Output 3
No
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
長さ N の整数列 A = (A_1, A_2, \dots, A_N), B = (B_1, B_2, \dots, B_N) が与えられます。
A の要素はすべて異なります。B の要素もすべて異なります。
次の 2 つを出力してください。
- A にも B にも含まれ、その位置も一致している整数の個数。言い換えると、A_i = B_i を満たす整数 i の個数。
- A にも B にも含まれるが、その位置は異なる整数の個数。言い換えると、A_i = B_j, i \neq j を満たす整数の組 (i, j) の個数。
制約
- 1 \leq N \leq 1000
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- A_1, A_2, \dots, A_N はすべて異なる。
- B_1, B_2, \dots, B_N はすべて異なる。
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
出力
答えを 2 行出力せよ。1 行目には 1. の個数、2 行目には 2. の個数を出力せよ。
入力例 1
4 1 3 5 2 2 3 1 4
出力例 1
1 2
A にも B にも含まれ、その位置も一致している整数は A_2 = B_2 = 3 の 1 個です。
A にも B にも含まれるが、その位置は異なる整数は A_1 = B_3 = 1 と A_4 = B_1 = 2 の 2 個です。
入力例 2
3 1 2 3 4 5 6
出力例 2
0 0
1., 2. ともに条件を満たす整数は存在しません。
入力例 3
7 4 8 1 7 9 5 6 3 5 1 7 8 2 6
出力例 3
3 2
Score : 200 points
Problem Statement
You are given integer sequences, each of length N: A = (A_1, A_2, \dots, A_N) and B = (B_1, B_2, \dots, B_N).
All elements of A are different. All elements of B are different, too.
Print the following two values.
- The number of integers contained in both A and B, appearing at the same position in the two sequences. In other words, the number of integers i such that A_i = B_i.
- The number of integers contained in both A and B, appearing at different positions in the two sequences. In other words, the number of pairs of integers (i, j) such that A_i = B_j and i \neq j.
Constraints
- 1 \leq N \leq 1000
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- A_1, A_2, \dots, A_N are all different.
- B_1, B_2, \dots, B_N are all different.
- All values in input are integers.
Input
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 answers in two lines: the answer to1. in the first line, and the answer to2. in the second line.
Sample Input 1
4 1 3 5 2 2 3 1 4
Sample Output 1
1 2
There is one integer contained in both A and B, appearing at the same position in the two sequences: A_2 = B_2 = 3.
There are two integers contained in both A and B, appearing at different positions in the two sequences: A_1 = B_3 = 1 and A_4 = B_1 = 2.
Sample Input 2
3 1 2 3 4 5 6
Sample Output 2
0 0
In both 1. and 2., no integer satisfies the condition.
Sample Input 3
7 4 8 1 7 9 5 6 3 5 1 7 8 2 6
Sample Output 3
3 2
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 150 点
問題文
健康に気を使っている高橋君は、M 種類の栄養素について、食事によって十分な量を摂取できているか気になりました。
i 番目の栄養素は 1 日あたり A_i 以上摂取することが目標です。
高橋君は今日 N 品の食品を食べ、i 品目の食品からは栄養素 j を X_{i,j} 摂取しました。
M 種類全ての栄養素で目標を達成しているかどうかを判定してください。
制約
- 1 \leq N \leq 100
- 1 \leq M \leq 100
- 0 \leq A_i,X_{i,j} \leq 10^7
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M
A_1 \ldots A_M
X_{1,1} \ldots X_{1,M}
\vdots
X_{N,1} \ldots X_{N,M}
出力
M 種類全ての栄養素で目標を達成しているなら Yes、そうでないならば No を出力せよ。
入力例 1
2 3 10 20 30 20 0 10 0 100 100
出力例 1
Yes
栄養素 1 は 1 品目から 20、2 品目から 0 摂取したため、合わせて 20 摂取しており、10 以上摂取するという目標を達成しています。
栄養素 2,3 についても同様に目標を達成しています。
入力例 2
2 4 10 20 30 40 20 0 10 30 0 100 100 0
出力例 2
No
栄養素 4 について目標を達成していません。
Score : 150 points
Problem Statement
Takahashi is health-conscious and concerned about whether he is getting enough of M types of nutrients from his diet.
For the i-th nutrient, his goal is to take at least A_i units per day.
Today, he ate N foods, and from the i-th food, he took X_{i,j} units of nutrient j.
Determine whether he has met the goal for all M types of nutrients.
Constraints
- 1 \leq N \leq 100
- 1 \leq M \leq 100
- 0 \leq A_i, X_{i,j} \leq 10^7
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
A_1 \ldots A_M
X_{1,1} \ldots X_{1,M}
\vdots
X_{N,1} \ldots X_{N,M}
Output
Print Yes if the goal is met for all M types of nutrients, and No otherwise.
Sample Input 1
2 3 10 20 30 20 0 10 0 100 100
Sample Output 1
Yes
For nutrient 1, Takahashi took 20 units from the 1-st food and 0 units from the 2-nd food, totaling 20 units, thus meeting the goal of taking at least 10 units.
Similarly, he meets the goal for nutrients 2 and 3.
Sample Input 2
2 4 10 20 30 40 20 0 10 30 0 100 100 0
Sample Output 2
No
The goal is not met for nutrient 4.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
N 棟のビルが等間隔に一列に並んでいます。手前から i 番目のビルの高さは H_i です。
あなたは次の条件をともに満たすようにいくつかのビルを選んで電飾で飾ろうとしています。
- 選んだビルたちは高さが等しい
- 選んだビルたちは等間隔に並んでいる
最大でいくつのビルを選ぶことができますか? なお、ちょうど 1 つのビルを選んだときは条件を満たすとみなします。
制約
- 1 \leq N \leq 3000
- 1 \leq H_i \leq 3000
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N H_1 \ldots H_N
出力
答えを出力せよ。
入力例 1
8 5 7 5 7 7 5 7 7
出力例 1
3
手前から 2,5,8 番目のビルを選ぶと条件を満たします。
入力例 2
10 100 200 300 400 500 600 700 800 900 1000
出力例 2
1
1つのビルを選んだときは条件を満たすとみなします。
入力例 3
32 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5
出力例 3
3
Score : 350 points
Problem Statement
There are N buildings arranged in a line at equal intervals. The height of the i-th building from the front is H_i.
You want to decorate some of these buildings with illuminations so that both of the following conditions are satisfied:
- The chosen buildings all have the same height.
- The chosen buildings are arranged at equal intervals.
What is the maximum number of buildings you can choose? If you choose exactly one building, it is considered to satisfy the conditions.
Constraints
- 1 \leq N \leq 3000
- 1 \leq H_i \leq 3000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H_1 \ldots H_N
Output
Print the answer.
Sample Input 1
8 5 7 5 7 7 5 7 7
Sample Output 1
3
Choosing the 2nd, 5th, and 8th buildings from the front satisfies the conditions.
Sample Input 2
10 100 200 300 400 500 600 700 800 900 1000
Sample Output 2
1
Choosing just one building is considered to satisfy the conditions.
Sample Input 3
32 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5
Sample Output 3
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N の狭義単調増加列 A=(A _ 1,A _ 2,\ldots,A _ N) と、長さ M の狭義単調増加列 B=(B _ 1,B _ 2,\ldots,B _ M) が与えられます。 ここで、すべての i,j\ (1\leq i\leq N,1\leq j\leq M) について A _ i\neq B _ j が成り立っています。
長さ N+M の狭義単調増加列 C=(C _ 1,C _ 2,\ldots,C _ {N+M}) を、次の操作を行った結果得られる列として定めます。
- C を A と B を連結した列とする。厳密には、i=1,2,\ldots,N について C _ i=A _ i とし、i=N+1,N+2,\ldots,N+M について C _ i=B _ {i-N} とする。
- C を昇順にソートする。
A _ 1,A _ 2,\ldots,A _ N と B _ 1,B _ 2,\ldots,B _ M がそれぞれ C では何番目にあるか求めてください。 より厳密には、まず i=1,2,\ldots,N について C _ k=A _ i を満たす k を順に求めたのち、j=1,2,\ldots,M について C _ k=B _ j を満たす k を順に求めてください。
制約
- 1\leq N,M\leq 10^5
- 1\leq A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq 10^9
- 1\leq B _ 1\lt B _ 2\lt\cdots\lt B _ M\leq 10^9
- すべての i,j\ (1\leq i\leq N,1\leq j\leq M) について A _ i\neq B _ j
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A _ 1 A _ 2 \ldots A _ N B _ 1 B _ 2 \ldots B _ M
出力
答えを 2 行で出力せよ。
1 行目には A _ 1,A _ 2,\ldots,A _ N がそれぞれ C では何番目にあるかを空白区切りで出力せよ。
2 行目には B _ 1,B _ 2,\ldots,B _ M がそれぞれ C では何番目にあるかを空白区切りで出力せよ。
入力例 1
4 3 3 14 15 92 6 53 58
出力例 1
1 3 4 7 2 5 6
C は (3,6,14,15,53,58,92) となります。 A=(3,14,15,92) の要素はそれぞれ 1,3,4,7 番目にあり、B=(6,53,58) の要素はそれぞれ 2,5,6 番目にあります。
入力例 2
4 4 1 2 3 4 100 200 300 400
出力例 2
1 2 3 4 5 6 7 8
入力例 3
8 12 3 4 10 15 17 18 22 30 5 7 11 13 14 16 19 21 23 24 27 28
出力例 3
1 2 5 9 11 12 15 20 3 4 6 7 8 10 13 14 16 17 18 19
Score : 300 points
Problem Statement
You are given strictly increasing sequences of length N and M: A=(A _ 1,A _ 2,\ldots,A _ N) and B=(B _ 1,B _ 2,\ldots,B _ M). Here, A _ i\neq B _ j for every i and j (1\leq i\leq N,1\leq j\leq M).
Let C=(C _ 1,C _ 2,\ldots,C _ {N+M}) be a strictly increasing sequence of length N+M that results from the following procedure.
- Let C be the concatenation of A and B. Formally, let C _ i=A _ i for i=1,2,\ldots,N, and C _ i=B _ {i-N} for i=N+1,N+2,\ldots,N+M.
- Sort C in ascending order.
For each of A _ 1,A _ 2,\ldots,A _ N, B _ 1,B _ 2,\ldots,B _ M, find its position in C. More formally, for each i=1,2,\ldots,N, find k such that C _ k=A _ i, and for each j=1,2,\ldots,M, find k such that C _ k=B _ j.
Constraints
- 1\leq N,M\leq 10^5
- 1\leq A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq 10^9
- 1\leq B _ 1\lt B _ 2\lt\cdots\lt B _ M\leq 10^9
- A _ i\neq B _ j for every i and j (1\leq i\leq N,1\leq j\leq M).
- 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 B _ 1 B _ 2 \ldots B _ M
Output
Print the answer in two lines.
The first line should contain the positions of A _ 1,A _ 2,\ldots,A _ N in C, with spaces in between.
The second line should contain the positions of B _ 1,B _ 2,\ldots,B _ M in C, with spaces in between.
Sample Input 1
4 3 3 14 15 92 6 53 58
Sample Output 1
1 3 4 7 2 5 6
C will be (3,6,14,15,53,58,92). Here, the 1-st, 3-rd, 4-th, 7-th elements are from A=(3,14,15,92), and the 2-nd, 5-th, 6-th elements are from B=(6,53,58).
Sample Input 2
4 4 1 2 3 4 100 200 300 400
Sample Output 2
1 2 3 4 5 6 7 8
Sample Input 3
8 12 3 4 10 15 17 18 22 30 5 7 11 13 14 16 19 21 23 24 27 28
Sample Output 3
1 2 5 9 11 12 15 20 3 4 6 7 8 10 13 14 16 17 18 19
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
高橋くんは、これから N 個のプレゼントをもらいます。
高橋くんにはテンションという非負整数のパラメータがあり、テンションはプレゼントをもらうごとに変動します。 それぞれのプレゼントは価値 P 、テンション上げ度 A 、テンション下げ度 B という 3 つのパラメータをもち、これらのパラメータによって高橋くんのテンションは次のように変動します。
- もらったプレゼントの価値 P がテンションの値以上であるとき、高橋くんはプレゼントに喜び、テンションが A だけ増加する。
- もらったプレゼントの価値 P がテンションの値より小さいとき、高橋くんはプレゼントにがっかりし、テンションが B だけ減少する。ただし、高橋くんのテンションの値が B より小さかった場合、高橋くんのテンションは 0 になる。
i 番目 (1\le i\le N) に受け取るプレゼントの価値は P _ i 、テンション上げ度は A _ i 、テンション下げ度は B _ i です。
Q 個の質問が与えられるので、その全てに答えてください。 i 番目 (1\le i\le Q) の質問では、非負整数 X _ i が与えられるので次の質問に答えてください。
高橋くんのテンションがはじめ X _ i だったときの、N 個のプレゼントをすべて受け取ったあとの高橋くんのテンションを求めよ。
制約
- 1\le N\le10000
- 1\le P _ i\le500\ (1\le i\le N)
- 1\le A _ i\le500\ (1\le i\le N)
- 1\le B _ i\le500\ (1\le i\le N)
- 1\le Q\le5\times10 ^ 5
- 0\le X _ i\le10 ^ 9\ (1\le i\le Q)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N P _ 1 A _ 1 B _ 1 P _ 2 A _ 2 B _ 2 \vdots P _ N A _ N B _ N Q X _ 1 X _ 2 \vdots X _ Q
出力
Q 行にわたって出力せよ。 i 行目には、i 番目の質問に対する答えを出力せよ。
入力例 1
4 3 1 4 1 5 9 2 6 5 3 5 8 11 0 1 2 3 4 5 6 7 8 9 10
出力例 1
6 0 0 0 5 6 0 0 0 0 0
高橋くんのテンションがはじめ 10 だったとき、高橋くんのテンションは以下のように変動します。
- 1 つめのプレゼントの価値 3 は高橋くんのテンション 10 未満なので、テンション下げ度 4 だけ高橋くんのテンションが減少し、高橋くんのテンションが 6 になる。
- 2 つめのプレゼントの価値 1 は高橋くんのテンション 6 未満で、高橋くんのテンション 6 はテンション下げ度 9 未満なので、高橋くんのテンションが 0 になる。
- 3 つめのプレゼントの価値 2 は高橋くんのテンション 0 以上なので、テンション上げ度 6 だけ高橋くんのテンションが増加し、高橋くんのテンションが 6 になる。
- 4 つめのプレゼントの価値 3 は高橋くんのテンション 6 未満で、高橋くんのテンション 6 はテンション下げ度 8 未満なので、高橋くんのテンションが 0 になる。
よって、最終的な高橋くんのテンションは 0 になります。
入力例 2
3 500 500 500 500 500 500 500 500 500 1 1000000000
出力例 2
999998500
高橋くんのテンションが高すぎるため、最高のプレゼントを貰っていても高橋くんのテンションは下がり続けます。
入力例 3
20 124 370 105 280 200 420 425 204 302 435 141 334 212 287 231 262 410 481 227 388 466 222 314 366 307 205 401 226 460 452 336 291 119 302 104 432 478 348 292 246 337 403 102 404 371 368 399 417 291 416 351 236 263 231 170 415 482 101 339 184 20 1162 1394 1695 2501 3008 3298 4053 4093 4330 5199 5302 5869 5875 6332 6567 7483 7562 7725 9723 9845
出力例 3
339 339 339 339 339 339 339 339 339 339 339 339 339 389 339 643 722 885 2883 3005
Score : 425 points
Problem Statement
Takahashi will receive N presents.
He has a parameter called mood, which is a non-negative integer, and his mood changes every time he receives a present. Each present has three parameters: value P, mood increase A, and mood decrease B, and his mood changes as follows based on these parameters:
- When the value P of the received present is greater than or equal to his mood, he is happy with the present, and his mood increases by A.
- When the value P of the received present is less than his mood, he is disappointed with the present, and his mood decreases by B. However, if his mood is originally less than B, it becomes 0.
The i-th (1\le i\le N) present he receives has value P _ i, mood increase A _ i, and mood decrease B _ i.
You are given Q questions, so answer all of them. In the i-th (1\le i\le Q) question, you are given a non-negative integer X _ i, so answer the following question:
Find Takahashi's mood after receiving all N presents when his mood is initially X _ i.
Constraints
- 1\le N\le10000
- 1\le P _ i\le500\ (1\le i\le N)
- 1\le A _ i\le500\ (1\le i\le N)
- 1\le B _ i\le500\ (1\le i\le N)
- 1\le Q\le5\times10 ^ 5
- 0\le X _ i\le10 ^ 9\ (1\le i\le Q)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N P _ 1 A _ 1 B _ 1 P _ 2 A _ 2 B _ 2 \vdots P _ N A _ N B _ N Q X _ 1 X _ 2 \vdots X _ Q
Output
Output Q lines. The i-th line should contain the answer to the i-th question.
Sample Input 1
4 3 1 4 1 5 9 2 6 5 3 5 8 11 0 1 2 3 4 5 6 7 8 9 10
Sample Output 1
6 0 0 0 5 6 0 0 0 0 0
When Takahashi's initial mood is 10, his mood changes as follows:
- The value 3 of the first present is less than his mood 10, so his mood decreases by the mood decrease 4, and his mood becomes 6.
- The value 1 of the second present is less than his mood 6, and Takahashi's mood 6 is less than the mood decrease 9, so his mood becomes 0.
- The value 2 of the third present is not less than his mood 0, so his mood increases by the mood increase 6, and his mood becomes 6.
- The value 3 of the fourth present is less than his mood 6, and Takahashi's mood 6 is less than the mood decrease 8, so his mood becomes 0.
Therefore, his final mood is 0.
Sample Input 2
3 500 500 500 500 500 500 500 500 500 1 1000000000
Sample Output 2
999998500
Because Takahashi's mood is too high, his mood keeps decreasing even when he receives the best presents.
Sample Input 3
20 124 370 105 280 200 420 425 204 302 435 141 334 212 287 231 262 410 481 227 388 466 222 314 366 307 205 401 226 460 452 336 291 119 302 104 432 478 348 292 246 337 403 102 404 371 368 399 417 291 416 351 236 263 231 170 415 482 101 339 184 20 1162 1394 1695 2501 3008 3298 4053 4093 4330 5199 5302 5869 5875 6332 6567 7483 7562 7725 9723 9845
Sample Output 3
339 339 339 339 339 339 339 339 339 339 339 339 339 389 339 643 722 885 2883 3005
実行時間制限: 2 sec / メモリ制限: 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
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
すぬけ君は高橋君と青木君にプレゼントを 1 個ずつ渡そうと考えています。
プレゼントの候補は N 種類あり、i 番目の候補は、高橋君にとって嬉しさ A_i 、青木君にとって嬉しさ B_i です。
高橋君と青木君はとても嫉妬深いので、相手がもらったプレゼントの自分にとっての嬉しさが、自分がもらったプレゼントの自分にとっての嬉しさより大きい場合、相手に嫉妬してけんかになってしまいます。
N^2 通りあるプレゼントの渡し方のうち、高橋君と青木君がけんかしないようなものは何通りありますか?
制約
- 1 \leq N \leq 2\times 10^5
- 0 \leq A_i \leq 10^9
- 0 \leq B_i \leq 10^9
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N B_1 \ldots B_N
出力
答えを出力せよ。
入力例 1
3 50 100 150 1 3 2
出力例 1
4
例えば高橋君に 1 番目の候補を、青木君に 2 番目の候補をプレゼントした場合、 青木君がもらったプレゼントの高橋君にとっての嬉しさが 100、 高橋君がもらったプレゼントの高橋君にとっての嬉しさは 50 なので、高橋君は青木君に嫉妬し、けんかしてしまいます。
また、例えば高橋君に 3 番目の候補を、青木君に 2 番目の候補をプレゼントした場合、2 人はけんかしません。
2 人に同じものをプレゼントしてもよいことに注意してください。
入力例 2
3 123456789 123456 123 987 987654 987654321
出力例 2
6
入力例 3
10 3 1 4 1 5 9 2 6 5 3 2 7 1 8 2 8 1 8 2 8
出力例 3
37
Score : 500 points
Problem Statement
Snuke is planning on giving one gift each to Takahashi and Aoki.
There are N candidates for the gifts. Takahashi's impression of the i-th candidate is A_i, and Aoki's impression of it is B_i.
The two are very jealous. If Takahashi's impression of the gift Aoki gets is greater than Takahashi's impression of the gift Takahashi gets, Takahashi gets jealous of Aoki and starts fighting, and vice versa.
Among the N^2 possible ways of giving the gifts, how many do not lead to fighting?
Constraints
- 1 \leq N \leq 2\times 10^5
- 0 \leq A_i \leq 10^9
- 0 \leq B_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 \ldots A_N B_1 \ldots B_N
Output
Print the answer.
Sample Input 1
3 50 100 150 1 3 2
Sample Output 1
4
For example, if we give the 1-st candidate to Takahashi and the 2-nd candidate to Aoki, Takahashi's impression of the gift Aoki gets is 100, while Takahashi's impression of the gift Takahashi gets is 50, so Takahashi gets jealous of Aoki and starts fighting.
As another example, if we give the 3-rd candidate to Takahashi and the 2-nd candidate to Aoki, the two will not start fighting.
Note that it is allowed to give the same gift to the two.
Sample Input 2
3 123456789 123456 123 987 987654 987654321
Sample Output 2
6
Sample Input 3
10 3 1 4 1 5 9 2 6 5 3 2 7 1 8 2 8 1 8 2 8
Sample Output 3
37