Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
4 枚のカードがあり、それぞれのカードには整数 A,B,C,D が書かれています。
ここに 1 枚カードを加え、フルハウスとできるか判定してください。
ただし、 5 枚組のカードは以下の条件を満たすとき、またそのときに限って、フルハウスであると呼ばれます。
- 異なる整数 x,y について、 x が書かれたカード 3 枚と y が書かれたカード 2 枚からなる。
制約
- 入力は全て整数
- 1 \le A,B,C,D \le 13
入力
入力は以下の形式で標準入力から与えられる。
A B C D
出力
1 枚カードを加えてフルハウスとできる場合は Yes 、そうでないときは No と出力せよ。
入力例 1
7 7 7 1
出力例 1
Yes
7,7,7,1 に 1 を加えた時、フルハウスとなります。
入力例 2
13 12 11 10
出力例 2
No
13,12,11,10 に何を加えてもフルハウスにはなりません。
入力例 3
3 3 5 5
出力例 3
Yes
3,3,5,5 に 3 を加えた時、フルハウスとなります。
また、 5 を加えてもフルハウスとなります。
入力例 4
8 8 8 8
出力例 4
No
8,8,8,8 に何を加えてもフルハウスにはなりません。
同じ 5 枚のカードはフルハウスではないことに注意してください。
入力例 5
1 3 4 1
出力例 5
No
Score : 100 points
Problem Statement
There are four cards with integers A,B,C,D written on them.
Determine whether a Full House can be formed by adding one card.
A set of five cards is called a Full House if and only if the following condition is satisfied:
- For two distinct integers x and y, there are three cards with x written on them and two cards with y written on them.
Constraints
- All input values are integers.
- 1 \le A,B,C,D \le 13
Input
The input is given from Standard Input in the following format:
A B C D
Output
If adding one card can form a Full House, print Yes; otherwise, print No.
Sample Input 1
7 7 7 1
Sample Output 1
Yes
Adding 1 to 7,7,7,1 forms a Full House.
Sample Input 2
13 12 11 10
Sample Output 2
No
Adding anything to 13,12,11,10 does not form a Full House.
Sample Input 3
3 3 5 5
Sample Output 3
Yes
Adding 3,3,5,5 to 3 forms a Full House.
Also, adding 5 forms a Full House.
Sample Input 4
8 8 8 8
Sample Output 4
No
Adding anything to 8,8,8,8 does not form a Full House.
Note that five identical cards do not form a Full House.
Sample Input 5
1 3 4 1
Sample Output 5
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋君は、毎日 S 時 0 分に部屋の電気をつけ、毎日 T 時 0 分に消します。
電気をつけている間に日付が変わることもあります。
X 時 30 分に部屋の電気がついているかどうか判定してください。
制約
- 0 \leq S, T, X \leq 23
- S \neq T
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
S T X
出力
X 時 30 分に部屋の電気がついているならば Yes と、そうでなければ No と出力せよ。
入力例 1
7 20 12
出力例 1
Yes
部屋の電気がついているのは 7 時 0 分から 20 時 0 分までの間です。12 時 30 分には電気がついているので、Yes と出力します。
入力例 2
20 7 12
出力例 2
No
部屋の電気がついているのは 0 時 0 分から 7 時 0 分までの間と、20 時 0 分から(次の日の)0 時 0 分までの間です。
12 時 30 分には電気がついていないので、No と出力します。
入力例 3
23 0 23
出力例 3
Yes
Score : 100 points
Problem Statement
Takahashi turns on the light of his room at S o'clock (on the 24-hour clock) every day and turns it off at T o'clock every day.
The date may change while the light is on.
Determine whether the light is on at 30 minutes past X o'clock.
Constraints
- 0 \leq S, T, X \leq 23
- S \neq T
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
S T X
Output
If the light is on at 30 minutes past X o'clock, print Yes; otherwise, print No.
Sample Input 1
7 20 12
Sample Output 1
Yes
The light is on between 7 o'clock and 20 o'clock. At 30 minutes past 12 o'clock, it is on, so we print Yes.
Sample Input 2
20 7 12
Sample Output 2
No
The light is on between 0 o'clock and 7 o'clock, and between 20 o'clock and 0 o'clock (on the next day). At 30 minutes past 12 o'clock, it is off, so we print No.
Sample Input 3
23 0 23
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
N 個の駅 1,2,\dots,N があり、これらはこの順に西から東に一直線上に並んでいます。
AtCoder 鉄道の電車はこれら N 個の駅を通り、西から東に走っています。
1 \leq i \lt j \leq N を満たす任意の 2 整数 i,j について、駅 i から電車に乗って駅 j で降りるのにコストが C_{i,j} かかります。
以下のような 3 つの整数 a,b,c が存在するかを判定してください。
- 1 \leq a \lt b \lt c \leq N
- 駅 a から電車に乗って駅 c で降りるときにかかるコストよりも、駅 a から電車に乗って駅 b で降り、再度、駅 b から電車に乗って駅 c で降りるときにかかるコストの総和の方が小さい。
制約
- 3 \leq N \leq 100
- 1 \leq C_{i,j} \leq 10^9
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
C_{1,2} C_{1,3} \dots C_{1,N}
C_{2,3} \dots C_{2,N}
\vdots
C_{N-1,N}
出力
条件を満たす 3 整数 a,b,c が存在するならば Yes を、存在しないならば No を 1 行で出力せよ。
入力例 1
3 45 450 45
出力例 1
Yes
(a,b,c) として (1,2,3) を選ぶと、
C_{a,b}+C_{b,c}=C_{1,2}+C_{2,3}=45+45
C_{a,c}=C_{1,3}=450
なので、条件を満たします。
入力例 2
4 25 40 65 30 55 25
出力例 2
No
どのように (a,b,c) を選んでも、条件を満たしません。
Score : 200 points
Problem Statement
There are N stations 1, 2, \dots, N, arranged in a straight line from west to east in this order.
The AtCoder Railway train passes through these N stations and runs from west to east.
For any two integers i, j satisfying 1 \leq i \lt j \leq N, the cost of boarding the train at station i and getting off at station j is C_{i,j}.
Determine whether there exist three integers a, b, c such that:
- 1 \leq a \lt b \lt c \leq N
- The total cost of boarding the train at station a, getting off at station b, then boarding the train again at station b, and getting off at station c is less than the cost of boarding the train at station a and getting off at station c.
Constraints
- 3 \leq N \leq 100
- 1 \leq C_{i,j} \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
C_{1,2} C_{1,3} \dots C_{1,N}
C_{2,3} \dots C_{2,N}
\vdots
C_{N-1,N}
Output
If there exist three integers a, b, c satisfying the conditions, output Yes; otherwise, output No, on a single line.
Sample Input 1
3 45 450 45
Sample Output 1
Yes
Choosing (a, b, c) = (1, 2, 3),
C_{a,b}+C_{b,c}=C_{1,2}+C_{2,3}=45+45
C_{a,c}=C_{1,3}=450
so the conditions are satisfied.
Sample Input 2
4 25 40 65 30 55 25
Sample Output 2
No
No choice of (a, b, c) satisfies the conditions.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
非負整数 A,B,C,D,E,F があり、A\times B\times C\geq D\times E\times F をみたしています。
(A\times B\times C)-(D\times E\times F) の値を 998244353 で割った余りを求めてください。
制約
- 0\leq A,B,C,D,E,F\leq 10^{18}
- A\times B\times C\geq D\times E\times F
- A,B,C,D,E,F は整数
入力
入力は以下の形式で標準入力から与えられる。
A B C D E F
出力
(A\times B\times C)-(D\times E\times F) を 998244353 で割った余りを整数で出力せよ。
入力例 1
2 3 5 1 2 4
出力例 1
22
A\times B\times C=2\times 3\times 5=30, D\times E\times F=1\times 2\times 4=8 より、
(A\times B\times C)-(D\times E\times F)=22 であり、これを 998244353 で割った余りである 22 を出力します。
入力例 2
1 1 1000000000 0 0 0
出力例 2
1755647
A\times B\times C=1000000000, D\times E\times F=0 より、
(A\times B\times C)-(D\times E\times F)=1000000000 であり、これを 998244353 で割った余りである 1755647 を出力します。
入力例 3
1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000
出力例 3
0
(A\times B\times C)-(D\times E\times F)=0 であり、これを 998244353 で割った余りである 0 を出力します。
Score : 200 points
Problem Statement
There are non-negative integers A, B, C, D, E, and F, which satisfy A\times B\times C\geq D\times E\times F.
Find the remainder when (A\times B\times C)-(D\times E\times F) is divided by 998244353.
Constraints
- 0\leq A,B,C,D,E,F\leq 10^{18}
- A\times B\times C\geq D\times E\times F
- A, B, C, D, E, and F are integers.
Input
The input is given from Standard Input in the following format:
A B C D E F
Output
Print the remainder when (A\times B\times C)-(D\times E\times F) is divided by 998244353, as an integer.
Sample Input 1
2 3 5 1 2 4
Sample Output 1
22
Since A\times B\times C=2\times 3\times 5=30 and D\times E\times F=1\times 2\times 4=8,
we have (A\times B\times C)-(D\times E\times F)=22. Divide this by 998244353 and print the remainder, which is 22.
Sample Input 2
1 1 1000000000 0 0 0
Sample Output 2
1755647
Since A\times B\times C=1000000000 and D\times E\times F=0,
we have (A\times B\times C)-(D\times E\times F)=1000000000. Divide this by 998244353 and print the remainder, which is 1755647.
Sample Input 3
1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000
Sample Output 3
0
We have (A\times B\times C)-(D\times E\times F)=0. Divide this by 998244353 and print the remainder, which is 0.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
冷蔵庫に N 種類の材料があります。これらを材料 1、\dots、材料 N と呼びます。材料 i は Q_i グラムあります。
あなたは 2 種類の料理を作れます。料理 A は、1 人分を作るのに各材料 i (1 \leq i \leq N) が A_i グラム必要です。料理 B は、1 人分を作るのに各材料 i が B_i グラム必要です。どちらも整数人分しか作れません。
冷蔵庫にある材料のみを使って、最大で合計何人分の料理を作れますか。
制約
- 1 \leq N \leq 10
- 1 \leq Q_i \leq 10^6
- 0 \leq A_i \leq 10^6
- A_i \geq 1 であるような i が存在する。
- 0 \leq B_i \leq 10^6
- B_i \geq 1 であるような i が存在する。
- 入力値はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N Q_1 Q_2 \dots Q_N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
出力
最大で合計 S 人分の料理を作れるとして、整数 S を出力せよ。
入力例 1
2 800 300 100 100 200 10
出力例 1
5
この冷蔵庫には、800 グラムの材料 1 と 300 グラムの材料 2 があります。
100 グラムの材料 1 と 100 グラムの材料 2 で料理 A を 1 人分作れ、200 グラムの材料 1 と 10 グラムの材料 2 で料理 B を 1 人分作れます。
料理 A を 2 人分、料理 B を 3 人分作るのに必要な材料 1 の量は 100 \times 2 + 200 \times 3 = 800 グラム、材料 2 の量は 100 \times 2 + 10 \times 3 = 230 グラムで、いずれも冷蔵庫にある量を超えません。このようにして合計 5 人分の料理を作ることができますが、6 人分を作る方法はなく、答えは 5 です。
入力例 2
2 800 300 100 0 0 10
出力例 2
38
800 グラムの材料 1 で料理 A を 8 人分、300 グラムの材料 2 で料理 B を 30 人分、合計 38 人分作れます。
入力例 3
2 800 300 801 300 800 301
出力例 3
0
何も作れません。
入力例 4
10 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0
出力例 4
222222
Score: 300 points
Problem Statement
Your refrigerator has N kinds of ingredients. Let us call them ingredient 1, \dots, ingredient N. You have Q_i grams of ingredient i.
You can make two types of dishes. To make one serving of dish A, you need A_i grams of each ingredient i (1 \leq i \leq N). To make one serving of dish B, you need B_i grams of each ingredient i. You can only make an integer number of servings of each type of dish.
Using only the ingredients in the refrigerator, what is the maximum total number of servings of dishes you can make?
Constraints
- 1 \leq N \leq 10
- 1 \leq Q_i \leq 10^6
- 0 \leq A_i \leq 10^6
- There is an i such that A_i \geq 1.
- 0 \leq B_i \leq 10^6
- There is an i such that B_i \geq 1.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q_1 Q_2 \dots Q_N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
Output
Assuming that you can make a maximum total of S servings of dishes, print the integer S.
Sample Input 1
2 800 300 100 100 200 10
Sample Output 1
5
This refrigerator has 800 grams of ingredient 1 and 300 grams of ingredient 2.
You can make one serving of dish A with 100 grams of ingredient 1 and 100 grams of ingredient 2, and one serving of dish B with 200 grams of ingredient 1 and 10 grams of ingredient 2.
To make two servings of dish A and three servings of dish B, you need 100 \times 2 + 200 \times 3 = 800 grams of ingredient 1, and 100 \times 2 + 10 \times 3 = 230 grams of ingredient 2, neither of which exceeds the amount available in the refrigerator. In this way, you can make a total of five servings of dishes, but there is no way to make six, so the answer is 5.
Sample Input 2
2 800 300 100 0 0 10
Sample Output 2
38
You can make 8 servings of dish A with 800 grams of ingredient 1, and 30 servings of dish B with 300 grams of ingredient 2, for a total of 38 servings.
Sample Input 3
2 800 300 801 300 800 301
Sample Output 3
0
You cannot make any dishes.
Sample Input 4
10 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0
Sample Output 4
222222
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
WAtCoder には N 人のユーザがおり、1 から N までの番号がつけられています。 また、M 個のコンテストページがあり、1 から M までの番号がつけられています。 はじめ、すべてのユーザはどのコンテストページの閲覧権限も持っていません。
Q 個のクエリが与えられるので、順に処理してください。クエリは 3 種類あり、以下のいずれかの形式で与えられます。
1 X Y: ユーザ X にコンテストページ Y の閲覧権限を付与する。2 X: ユーザ X にすべてのコンテストページの閲覧権限を付与する。3 X Y: ユーザ X がコンテストページ Y を閲覧できるかを答える。
クエリの中で、あるユーザがすでに閲覧権限を持っているコンテストページについて、重ねて閲覧権限を付与されることもあります。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq Q \leq 2 \times 10^5
- 1 \leq X \leq N
- 1 \leq Y \leq M
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリ \mathrm{query}_i は以下の 3 種類のいずれかの形式で与えられる。
1 X Y
2 X
3 X Y
出力
3 種類目のクエリのそれぞれについて、ユーザ X がコンテストページ Y を閲覧できるならば Yes を、そうでなければ No を改行区切りで出力せよ。
入力例 1
2 3 5 1 1 2 3 1 1 3 1 2 2 2 3 2 3
出力例 1
No Yes Yes
- 1 つ目のクエリで、ユーザ 1 にコンテストページ 2 の閲覧権限を付与します。
- 2 つ目のクエリの時点で、ユーザ 1 が閲覧できるコンテストページは 2 のみです。コンテストページ 1 の閲覧権限を持っていないので、
Noを出力します。 - 3 つ目のクエリの時点で、ユーザ 1 はコンテストページ 2 の閲覧権限を持っているので、
Yesを出力します。 - 4 つ目のクエリで、ユーザ 2 にすべてのコンテストページの閲覧権限を付与します。
- 5 つ目のクエリの時点で、ユーザ 2 が閲覧できるコンテストページは 1,2,3 です。コンテストページ 3 の閲覧権限を持っているので、
Yesを出力します。
入力例 2
5 5 10 2 2 3 4 4 1 1 1 1 4 1 1 4 2 1 4 4 1 2 4 3 3 2 3 5 4 3 2 1
出力例 2
No No No Yes
Score : 300 points
Problem Statement
There are N users on WAtCoder, numbered from 1 to N, and M contest pages, numbered from 1 to M. Initially, no user has view permission for any contest page.
You are given Q queries to process in order. Each query is of one of the following three types:
1 X Y: Grant user X view permission for contest page Y.2 X: Grant user X view permission for all contest pages.3 X Y: Answer whether user X can view contest page Y.
It is possible for a user to be granted permission for the same contest page multiple times.
Constraints
- 1 \le N \le 2\times 10^5
- 1 \le M \le 2\times 10^5
- 1 \le Q \le 2\times 10^5
- 1 \le X \le N
- 1 \le Y \le M
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each \mathrm{query}_i is in one of the following formats:
1 X Y
2 X
3 X Y
Output
For each query of the third type, print Yes if user X can view contest page Y, otherwise print No, each on its own line.
Sample Input 1
2 3 5 1 1 2 3 1 1 3 1 2 2 2 3 2 3
Sample Output 1
No Yes Yes
- In the first query, user 1 is granted permission to view contest page 2.
- At the second query, user 1 can view only page 2; they cannot view page 1, so print
No. - At the third query, user 1 can view page 2, so print
Yes. - In the fourth query, user 2 is granted permission to view all pages.
- At the fifth query, user 2 can view pages 1,2,3; they can view page 3, so print
Yes.
Sample Input 2
5 5 10 2 2 3 4 4 1 1 1 1 4 1 1 4 2 1 4 4 1 2 4 3 3 2 3 5 4 3 2 1
Sample Output 2
No No No Yes
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
高橋くんは睡眠記録をつけています。 睡眠記録は奇数長の数列 A=(A _ 1(=0), A _ 2,\ldots,A _ N) で表され、奇数番目は起床時刻を、偶数番目は就寝時刻を表しています。 より厳密には、睡眠記録をつけている間に高橋くんは次のような睡眠をとりました。
- すべての 1\leq i\leq\dfrac{N-1}2 を満たす整数 i について、睡眠記録をつけ始めてから A _ {2i} 分後ちょうどに寝て、A _ {2i+1} 分後ちょうどに起きた。
- それ以外の時間に寝ることも起きることもなかった。
次の Q 個の質問に答えてください。 i 番目の質問では、0\leq l _ i\leq r _ i\leq A _ N を満たす整数の組 (l _ i,r _ i) が与えられます。
- 睡眠記録をつけ始めてから l _ i 分後ちょうどから r _ i 分後ちょうどまでの r _ i-l _ i 分のうち、高橋くんが寝ていたのは何分間ですか?
制約
- 3\leq N\lt2\times10^5
- N は奇数
- 0=A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq10^9
- 1\leq Q\leq2\times10^5
- 0\leq l _ i\leq r _ i\leq A _ N\ (1\leq i\leq Q)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N Q l _ 1 r _ 1 l _ 2 r _ 2 \vdots l _ Q r _ Q
出力
答えを Q 行で出力せよ。 i 行目には i 番目の質問の答えを整数として出力せよ。
入力例 1
7 0 240 720 1320 1440 1800 2160 3 480 1920 720 1200 0 2160
出力例 1
480 0 960
高橋くんは、以下の図のように睡眠をとりました。

それぞれの質問の答えは以下のようになります。
- 睡眠記録をつけ始めてから 480 分後から 1920 分後の間、高橋くんは 480 分後から 720 分後、1320 分後から 1440 分後、1800 分後から 1920 分後の 3 つの睡眠をとりました。睡眠時間の合計は 240+120+120=480 分です。
- 睡眠記録をつけ始めてから 720 分後から 1200 分後の間、高橋くんは睡眠をとりませんでした。睡眠時間の合計は 0 分です。
- 睡眠記録をつけ始めてから 0 分後から 2160 分後の間、高橋くんは 240 分後から 720 分後、1320 分後から 1440 分後、1800 分後から 2160 分後の 3 つの睡眠をとりました。睡眠時間の合計は 480+120+360=960 分です。
よって、それぞれの行に 480,0,960 と出力してください。
入力例 2
21 0 20 62 192 284 310 323 324 352 374 409 452 486 512 523 594 677 814 838 946 1000 10 77 721 255 541 478 970 369 466 343 541 42 165 16 618 222 592 730 983 338 747
出力例 2
296 150 150 49 89 20 279 183 61 177
Score : 450 points
Problem Statement
Takahashi keeps a sleep log. The log is represented as an odd-length sequence A=(A _ 1(=0), A _ 2,\ldots,A _ N), where odd-numbered elements represent times he got up, and even-numbered elements represent times he went to bed. More formally, he had the following sleep sessions after starting the sleep log.
- For every integer i such that 1\leq i\leq\dfrac{N-1}2, he fell asleep exactly A _ {2i} minutes after starting the sleep log and woke up exactly A _ {2i+1} minutes after starting the sleep log.
- He did not fall asleep or wake up at any other time.
Answer the following Q questions. For the i-th question, you are given a pair of integers (l _ i,r _ i) such that 0\leq l _ i\leq r _ i\leq A _ N.
- What is the total number of minutes for which Takahashi was asleep during the r _ i-l _ i minutes from exactly l _ i minutes to r _ i minutes after starting the sleep log?
Constraints
- 3\leq N\lt2\times10^5
- N is odd.
- 0=A _ 1\lt A _ 2\lt\cdots\lt A _ N\leq10^9
- 1\leq Q\leq2\times10^5
- 0\leq l _ i\leq r _ i\leq A _ N\ (1\leq i\leq Q)
- 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 Q l _ 1 r _ 1 l _ 2 r _ 2 \vdots l _ Q r _ Q
Output
Print the answer in Q lines. The i-th line should contain an integer answering to the i-th question.
Sample Input 1
7 0 240 720 1320 1440 1800 2160 3 480 1920 720 1200 0 2160
Sample Output 1
480 0 960
Takahashi slept as shown in the following figure.

The answers to each question are as follows.
- Between 480 minutes and 1920 minutes after starting the sleep log, Takahashi slept from 480 minutes to 720 minutes, from 1320 minutes to 1440 minutes, and from 1800 minutes to 1920 minutes in 3 sleep sessions. The total sleep time is 240+120+120=480 minutes.
- Between 720 minutes and 1200 minutes after starting the sleep log, Takahashi did not sleep. The total sleep time is 0 minutes.
- Between 0 minutes and 2160 minutes after starting the sleep log, Takahashi slept from 240 minutes to 720 minutes, from 1320 minutes to 1440 minutes, and from 1800 minutes to 2160 minutes in 3 sleep sessions. The total sleep time is 480+120+360=960 minutes.
Therefore, the three lines of the output should contain 480, 0, and 960.
Sample Input 2
21 0 20 62 192 284 310 323 324 352 374 409 452 486 512 523 594 677 814 838 946 1000 10 77 721 255 541 478 970 369 466 343 541 42 165 16 618 222 592 730 983 338 747
Sample Output 2
296 150 150 49 89 20 279 183 61 177
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
あなたの今日のラッキーギリシャ文字はシグマです。シグマを 2 つも使ったこの問題を解けば、きっと幸運が舞い込むことでしょう。
長さ N の正整数列 A = (A_1, \cdots, A_N) および長さ M の正整数列 B = (B_1, \cdots, B_M) が与えられます。
\displaystyle \sum_{i=1}^{N} \sum_{j=1}^{M} A_i \cdot B_j \cdot (i \bmod j) の値を 998244353 で割ったあまりを求めてください。
制約
- 1 \leq N,M \leq 5 \times 10^5
- 1 \leq A_i, B_j \leq 5 \times 10^5
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \cdots A_N B_1 B_2 \cdots B_M
出力
答えを 1 行に出力せよ。
入力例 1
6 4 1 6 9 2 3 1 1 10 3 7
出力例 1
508
以下の 24 個の値の合計は 508 です。
- A_1 \cdot B_1 \cdot (1 \bmod 1) = 1 \cdot 1 \cdot 0 = 0
- A_1 \cdot B_2 \cdot (1 \bmod 2) = 1 \cdot 10 \cdot 1 = 10
- A_1 \cdot B_3 \cdot (1 \bmod 3) = 1 \cdot 3 \cdot 1 = 3
- A_1 \cdot B_4 \cdot (1 \bmod 4) = 1 \cdot 7 \cdot 1 = 7
- A_2 \cdot B_1 \cdot (2 \bmod 1) = 6 \cdot 1 \cdot 0 = 0
- A_2 \cdot B_2 \cdot (2 \bmod 2) = 6 \cdot 10 \cdot 0 = 0
- A_2 \cdot B_3 \cdot (2 \bmod 3) = 6 \cdot 3 \cdot 2 = 36
- A_2 \cdot B_4 \cdot (2 \bmod 4) = 6 \cdot 7 \cdot 2 = 84
- A_3 \cdot B_1 \cdot (3 \bmod 1) = 9 \cdot 1 \cdot 0 = 0
- A_3 \cdot B_2 \cdot (3 \bmod 2) = 9 \cdot 10 \cdot 1 = 90
- A_3 \cdot B_3 \cdot (3 \bmod 3) = 9 \cdot 3 \cdot 0 = 0
- A_3 \cdot B_4 \cdot (3 \bmod 4) = 9 \cdot 7 \cdot 3 = 189
- A_4 \cdot B_1 \cdot (4 \bmod 1) = 2 \cdot 1 \cdot 0 = 0
- A_4 \cdot B_2 \cdot (4 \bmod 2) = 2 \cdot 10 \cdot 0 = 0
- A_4 \cdot B_3 \cdot (4 \bmod 3) = 2 \cdot 3 \cdot 1 = 6
- A_4 \cdot B_4 \cdot (4 \bmod 4) = 2 \cdot 7 \cdot 0 = 0
- A_5 \cdot B_1 \cdot (5 \bmod 1) = 3 \cdot 1 \cdot 0 = 0
- A_5 \cdot B_2 \cdot (5 \bmod 2) = 3 \cdot 10 \cdot 1 = 30
- A_5 \cdot B_3 \cdot (5 \bmod 3) = 3 \cdot 3 \cdot 2 = 18
- A_5 \cdot B_4 \cdot (5 \bmod 4) = 3 \cdot 7 \cdot 1 = 21
- A_6 \cdot B_1 \cdot (6 \bmod 1) = 1 \cdot 1 \cdot 0 = 0
- A_6 \cdot B_2 \cdot (6 \bmod 2) = 1 \cdot 10 \cdot 0 = 0
- A_6 \cdot B_3 \cdot (6 \bmod 3) = 1 \cdot 3 \cdot 0 = 0
- A_6 \cdot B_4 \cdot (6 \bmod 4) = 1 \cdot 7 \cdot 2 = 14
入力例 2
20 20 36625 195265 98908 111868 111868 47382 147644 472464 472464 416653 111868 195265 327972 327972 262769 75439 381156 451275 36625 195265 327972 111868 416653 177330 340019 262769 47382 262769 47382 340019 47382 262769 327972 327972 359676 381156 327972 36625 451275 381156
出力例 2
58141644
Score : 450 points
Problem Statement
Your lucky Greek letter for today is sigma. Solve this problem that uses sigma twice, and good fortune will surely come your way.
You are given a sequence of positive integers A = (A_1, \cdots, A_N) of length N and a sequence of positive integers B = (B_1, \cdots, B_M) of length M.
Find the value, modulo 998244353, of \displaystyle \sum_{i=1}^{N} \sum_{j=1}^{M} A_i \cdot B_j \cdot (i \bmod j).
Constraints
- 1 \leq N,M \leq 5 \times 10^5
- 1 \leq A_i, B_j \leq 5 \times 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \cdots A_N B_1 B_2 \cdots B_M
Output
Output the answer on a single line.
Sample Input 1
6 4 1 6 9 2 3 1 1 10 3 7
Sample Output 1
508
The sum of the following 24 values is 508.
- A_1 \cdot B_1 \cdot (1 \bmod 1) = 1 \cdot 1 \cdot 0 = 0
- A_1 \cdot B_2 \cdot (1 \bmod 2) = 1 \cdot 10 \cdot 1 = 10
- A_1 \cdot B_3 \cdot (1 \bmod 3) = 1 \cdot 3 \cdot 1 = 3
- A_1 \cdot B_4 \cdot (1 \bmod 4) = 1 \cdot 7 \cdot 1 = 7
- A_2 \cdot B_1 \cdot (2 \bmod 1) = 6 \cdot 1 \cdot 0 = 0
- A_2 \cdot B_2 \cdot (2 \bmod 2) = 6 \cdot 10 \cdot 0 = 0
- A_2 \cdot B_3 \cdot (2 \bmod 3) = 6 \cdot 3 \cdot 2 = 36
- A_2 \cdot B_4 \cdot (2 \bmod 4) = 6 \cdot 7 \cdot 2 = 84
- A_3 \cdot B_1 \cdot (3 \bmod 1) = 9 \cdot 1 \cdot 0 = 0
- A_3 \cdot B_2 \cdot (3 \bmod 2) = 9 \cdot 10 \cdot 1 = 90
- A_3 \cdot B_3 \cdot (3 \bmod 3) = 9 \cdot 3 \cdot 0 = 0
- A_3 \cdot B_4 \cdot (3 \bmod 4) = 9 \cdot 7 \cdot 3 = 189
- A_4 \cdot B_1 \cdot (4 \bmod 1) = 2 \cdot 1 \cdot 0 = 0
- A_4 \cdot B_2 \cdot (4 \bmod 2) = 2 \cdot 10 \cdot 0 = 0
- A_4 \cdot B_3 \cdot (4 \bmod 3) = 2 \cdot 3 \cdot 1 = 6
- A_4 \cdot B_4 \cdot (4 \bmod 4) = 2 \cdot 7 \cdot 0 = 0
- A_5 \cdot B_1 \cdot (5 \bmod 1) = 3 \cdot 1 \cdot 0 = 0
- A_5 \cdot B_2 \cdot (5 \bmod 2) = 3 \cdot 10 \cdot 1 = 30
- A_5 \cdot B_3 \cdot (5 \bmod 3) = 3 \cdot 3 \cdot 2 = 18
- A_5 \cdot B_4 \cdot (5 \bmod 4) = 3 \cdot 7 \cdot 1 = 21
- A_6 \cdot B_1 \cdot (6 \bmod 1) = 1 \cdot 1 \cdot 0 = 0
- A_6 \cdot B_2 \cdot (6 \bmod 2) = 1 \cdot 10 \cdot 0 = 0
- A_6 \cdot B_3 \cdot (6 \bmod 3) = 1 \cdot 3 \cdot 0 = 0
- A_6 \cdot B_4 \cdot (6 \bmod 4) = 1 \cdot 7 \cdot 2 = 14
Sample Input 2
20 20 36625 195265 98908 111868 111868 47382 147644 472464 472464 416653 111868 195265 327972 327972 262769 75439 381156 451275 36625 195265 327972 111868 416653 177330 340019 262769 47382 262769 47382 340019 47382 262769 327972 327972 359676 381156 327972 36625 451275 381156
Sample Output 2
58141644
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
正整数 N と長さ N の正整数列 A=(A_1,A_2,\dots,A_N) と B=(B_1,B_2,\dots,B_N) が与えられます。
N \times N のマス目があります。上から i 行目、左から j 列目のマスをマス (i,j) と呼びます。1 \le i,j \le N を満たす整数の組 (i,j) に対し、マス (i,j) に A_i + B_j が書かれています。以下のクエリを Q 個処理してください。
- 1 \le h_1 \le h_2 \le N,1 \le w_1 \le w_2 \le N を満たす整数の組 h_1,h_2,w_1,w_2 が与えられる。左上隅が (h_1,w_1)、右下隅が (h_2,w_2) である矩形領域に含まれる整数の最大公約数を求めよ。
制約
- 1 \le N,Q \le 2 \times 10^5
- 1 \le A_i,B_i \le 10^9
- 1 \le h_1 \le h_2 \le N
- 1 \le w_1 \le w_2 \le N
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N Q
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の形式で与えられる。
h_1 h_2 w_1 w_2
出力
Q 行出力せよ。i 行目には \mathrm{query}_i の答えを出力せよ。
入力例 1
3 5 3 5 2 8 1 3 1 2 2 3 1 3 1 3 1 1 1 1 2 2 2 2 3 3 1 1
出力例 1
2 1 11 6 10
マス (i,j) に書かれている整数を C_{i,j} とします。
1 個目のクエリについて、C_{1,2}=4,C_{1,3}=6,C_{2,2}=6,C_{2,3}=8 なのでこれらの最大公約数の 2 が答えとなります。
入力例 2
1 1 9 100 1 1 1 1
出力例 2
109
Score : 500 points
Problem Statement
You are given a positive integer N and sequences of N positive integers each: A=(A_1,A_2,\dots,A_N) and B=(B_1,B_2,\dots,B_N).
We have an N \times N grid. The square at the i-th row from the top and the j-th column from the left is called the square (i,j). For each pair of integers (i,j) such that 1 \le i,j \le N, the square (i,j) has the integer A_i + B_j written on it. Process Q queries of the following form.
- You are given a quadruple of integers h_1,h_2,w_1,w_2 such that 1 \le h_1 \le h_2 \le N,1 \le w_1 \le w_2 \le N. Find the greatest common divisor of the integers contained in the rectangle region whose top-left and bottom-right corners are (h_1,w_1) and (h_2,w_2), respectively.
Constraints
- 1 \le N,Q \le 2 \times 10^5
- 1 \le A_i,B_i \le 10^9
- 1 \le h_1 \le h_2 \le N
- 1 \le w_1 \le w_2 \le N
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is in the following format:
h_1 h_2 w_1 w_2
Output
Print Q lines. The i-th line should contain the answer to \mathrm{query}_i.
Sample Input 1
3 5 3 5 2 8 1 3 1 2 2 3 1 3 1 3 1 1 1 1 2 2 2 2 3 3 1 1
Sample Output 1
2 1 11 6 10
Let C_{i,j} denote the integer on the square (i,j).
For the 1-st query, we have C_{1,2}=4,C_{1,3}=6,C_{2,2}=6,C_{2,3}=8, so the answer is their greatest common divisor, which is 2.
Sample Input 2
1 1 9 100 1 1 1 1
Sample Output 2
109