Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
サンタさんに手紙を出したい高橋くんは、 X 円切手が 1 枚だけ貼られた封筒を用意しました。
サンタさんに手紙を届けるためには、貼られている切手の総額が Y 円以上である必要があります。
高橋くんは、この封筒に 10 円切手を何枚か貼り足すことで、貼られている切手の総額を Y 円以上にしたいです。
高橋くんはこの封筒に、最小で何枚の 10 円切手を貼り足す必要がありますか?
制約
- X,Y は整数
- 1 \le X,Y \le 1000
入力
入力は以下の形式で標準入力から与えられる。
X Y
出力
答えを整数として出力せよ。
入力例 1
80 94
出力例 1
2
- 80 円切手に 0 枚の 10 円切手を貼り足せば総額が 80 円となり、これは手紙を届けるのに必要な 94 円未満です。
- 80 円切手に 1 枚の 10 円切手を貼り足せば総額が 90 円となり、これは手紙を届けるのに必要な 94 円未満です。
- 80 円切手に 2 枚の 10 円切手を貼り足せば総額が 100 円となり、これは手紙を届けるのに必要な 94 円以上です。
入力例 2
1000 63
出力例 2
0
もともと貼られている切手だけで金額が十分である可能性もあります。
入力例 3
270 750
出力例 3
48
Score : 100 points
Problem Statement
Takahashi wants to send a letter to Santa Claus. He has an envelope with an X-yen (Japanese currency) stamp stuck on it.
To be delivered to Santa Claus, the envelope must have stamps in a total value of at least Y yen.
Takahashi will put some more 10-yen stamps so that the envelope will have stamps worth at least Y yen in total.
At least how many more 10-yen stamps does Takahashi need to put on the envelope?
Constraints
- X and Y are integers.
- 1 \le X,Y \le 1000
Input
Input is given from Standard Input in the following format:
X Y
Output
Print the answer as an integer.
Sample Input 1
80 94
Sample Output 1
2
- After adding zero 10-yen stamps to the 80-yen stamp, the total is 80 yen, which is less than the required amount of 94 yen.
- After adding one 10-yen stamp to the 80-yen stamp, the total is 90 yen, which is less than the required amount of 94 yen.
- After adding two 10-yen stamps to the 80-yen stamp, the total is 100 yen, which is not less than the required amount of 94 yen.
Sample Input 2
1000 63
Sample Output 2
0
The envelope may already have a stamp with enough value.
Sample Input 3
270 750
Sample Output 3
48
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
文字列 S が与えられるので、この文字列が Hello,World! と完全に一致するなら AC 、そうでないなら WA と出力してください。
「完全に一致する」とは?
文字列 A と B が完全に一致するとは、文字列 A と B の長さが等しく、かつ全ての 1 \le i \le |A| を満たす整数 i について A の先頭から i 文字目と B の先頭から i 文字目とが(英大文字か小文字かも含めて)一致することを指します。制約
- 1 \le |S| \le 15
- S は英大小文字,
,,!のみからなる
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
Hello,World!
出力例 1
AC
文字列 S は Hello,World! と完全に一致します。
入力例 2
Hello,world!
出力例 2
WA
先頭から 7 文字目の W が、 Hello,World! では大文字ですが S では小文字です。よって S は Hello,World! と一致しません。
入力例 3
Hello!World!
出力例 3
WA
Score : 100 points
Problem Statement
Given a string S, print AC if it perfectly matches Hello,World!; otherwise, print WA.
What is a perfect match?
Strings A is said to perfectly match B when the length of A is equal to that of B, and the i-th character of A is the same as the i-th character of B for every integer i such that 1 \le i \le |A|.Constraints
- 1 \le |S| \le 15
- S consists of English lowercase letters, English uppercase letters,
,, and!.
Input
Input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
Hello,World!
Sample Output 1
AC
The string S perfectly matches Hello,World!.
Sample Input 2
Hello,world!
Sample Output 2
WA
The seventh character from the beginning should be an uppercase W in Hello,World!, but S has a lowercase w in that position. Thus, S does not match Hello,World!.
Sample Input 3
Hello!World!
Sample Output 3
WA
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
1 以上 26 以下の整数からなる長さ 26 の数列 P=(P_1,P_2, \ldots ,P_{26}) が与えられます。ここで、P の要素は相異なることが保証されます。
以下の条件を満たす長さ 26 の文字列 S を出力してください。
- 任意の i\, (1 \leq i \leq 26) について、S の i 文字目は辞書順で小さい方から P_i 番目の英小文字である。
制約
- 1 \leq P_i \leq 26
- P_i \neq P_j (i \neq j)
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
P_1 P_2 \ldots P_{26}
出力
文字列 S を出力せよ。
入力例 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
出力例 1
abcdefghijklmnopqrstuvwxyz
入力例 2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
出力例 2
bacdefghijklmnopqrstuvwxyz
入力例 3
5 11 12 16 25 17 18 1 7 10 4 23 20 3 2 24 26 19 14 9 6 22 8 13 15 21
出力例 3
eklpyqragjdwtcbxzsnifvhmou
Score : 200 points
Problem Statement
You are given a sequence of 26 integers P=(P_1,P_2, \ldots ,P_{26}) consisting of integers from 1 through 26. It is guaranteed that all elements in P are distinct.
Print a string S of length 26 that satisfies the following condition.
- For every i (1 \leq i \leq 26), the i-th character of S is the lowercase English letter that comes P_i-th in alphabetical order.
Constraints
- 1 \leq P_i \leq 26
- P_i \neq P_j (i \neq j)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
P_1 P_2 \ldots P_{26}
Output
Print the string S.
Sample Input 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Sample Output 1
abcdefghijklmnopqrstuvwxyz
Sample Input 2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Sample Output 2
bacdefghijklmnopqrstuvwxyz
Sample Input 3
5 11 12 16 25 17 18 1 7 10 4 23 20 3 2 24 26 19 14 9 6 22 8 13 15 21
Sample Output 3
eklpyqragjdwtcbxzsnifvhmou
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
スーパーマーケットで卵のパックが売られています。
卵 6 個入りのパックは S 円、卵 8 個入りのパックは M 円、卵 12 個入りのパックは L 円です。
どのパックも何パックでも購入できるとき、N 個以上の卵を買うために必要な最小の金額を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq S,M,L \leq 10^4
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N S M L
出力
答えを出力せよ。
入力例 1
16 120 150 200
出力例 1
300
8 個入りのパックを 2 個買うのが最適です。
入力例 2
10 100 50 10
出力例 2
10
12 個入りのパックを 1 個買うのが最適です。
入力例 3
99 600 800 1200
出力例 3
10000
8 個入りのパックと 12 個入りのパックを 5 個ずつ買うのが最適です。
Score : 200 points
Problem Statement
A supermarket sells egg packs.
A pack of 6 eggs costs S yen, a pack of 8 eggs costs M yen, and a pack of 12 eggs costs L yen.
When you can buy any number of each pack, find the minimum amount of money required to purchase at least N eggs.
Constraints
- 1 \leq N \leq 100
- 1 \leq S,M,L \leq 10^4
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N S M L
Output
Print the answer.
Sample Input 1
16 120 150 200
Sample Output 1
300
It is optimal to buy two 8-egg packs.
Sample Input 2
10 100 50 10
Sample Output 2
10
It is optimal to buy one 12-egg pack.
Sample Input 3
99 600 800 1200
Sample Output 3
10000
It is optimal to buy five 8-egg packs and five 12-egg packs.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
二次元平面の点 (0,0) に高橋君がいます。初め、高橋君の体力は H です。また、二次元平面には M 個の体力を回復するアイテムがあり、i 個目のアイテムは点 (x_i,y_i) に置いてあります。
高橋君は、これから N 回の移動をします。i 回目の移動は以下の方法で行われます。
-
今高橋君がいる点を (x,y) とする。体力を 1 消費し、S の i 番目の文字 S_i に応じて以下の点に移動する。
- S_i が
Rのとき: (x+1,y) - S_i が
Lのとき: (x-1,y) - S_i が
Uのとき: (x,y+1) - S_i が
Dのとき: (x,y-1)
- S_i が
-
高橋君の体力が負になった場合、高橋君は倒れてしまい、移動をやめる。そうでない場合、移動した点にアイテムがあり、かつ高橋君の体力が K 未満ならば、移動した点に置かれたアイテムを消費し、高橋君の体力が K になる。
高橋君が一度も倒れることなく N 回の移動を行えるか判定してください。
制約
- 1\leq N,M,H,K\leq 2\times 10^5
- S は
R,L,U,Dからなる長さ N の文字列 - |x_i|,|y_i| \leq 2\times 10^5
- (x_i,y_i) は互いに異なる
- S 以外の入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M H K S x_1 y_1 \vdots x_M y_M
出力
高橋君が一度も倒れることなく N 回の移動を行える場合 Yes を、そうでない場合 No を出力せよ。
入力例 1
4 2 3 1 RUDL -1 -1 1 0
出力例 1
Yes
初め高橋君の体力は 3 です。以下で移動を説明します。
-
1 回目の移動: S_i が
Rなので点 (1,0) に移動する。高橋君の体力は 2 に減る。点 (1,0) にはアイテムが置いてあるが、高橋君の体力は K=1 以上なのでアイテムは消費されない。 -
2 回目の移動: S_i が
Uなので点 (1,1) に移動する。高橋君の体力は 1 に減る。 -
3 回目の移動: S_i が
Dなので点 (1,0) に移動する。高橋君の体力は 0 に減る。点 (1,0) にはアイテムが置いてあり、体力は K=1 未満なのでアイテムを消費し、体力が 1 になる。 -
4 回目の移動: S_i が
Lなので点 (0,0) に移動する。高橋君の体力は 0 に減る。
以上より、高橋君は倒れずに 4 回の移動を行えるので、Yes を出力してください。体力は 0 になってもいいことに注意してください。
入力例 2
5 2 1 5 LDRLD 0 0 -1 -1
出力例 2
No
初め高橋君の体力は 1 です。以下で移動を説明します。
-
1 回目の移動: S_i が
Lなので点 (-1,0) に移動する。高橋君の体力は 0 に減る。 -
2 回目の移動: S_i が
Dなので点 (-1,-1) に移動する。高橋君の体力は -1 に減る。体力が -1 になってしまったので、高橋君は倒れてしまい、移動をやめる。
以上より、高橋君は倒れてしまうので、No を出力してください。
高橋君がはじめいる点 (0,0) にはアイテムが置いてありますが、移動後にアイテムは消費されるので、1 回目の移動前にアイテムを消費しないことに注意してください。
Score : 300 points
Problem Statement
On a two-dimensional plane, Takahashi is initially at point (0, 0), and his initial health is H. M items to recover health are placed on the plane; the i-th of them is placed at (x_i,y_i).
Takahashi will make N moves. The i-th move is as follows.
-
Let (x,y) be his current coordinates. He consumes a health of 1 to move to the following point, depending on S_i, the i-th character of S:
- (x+1,y) if S_i is
R; - (x-1,y) if S_i is
L; - (x,y+1) if S_i is
U; - (x,y-1) if S_i is
D.
- (x+1,y) if S_i is
-
If Takahashi's health has become negative, he collapses and stops moving. Otherwise, if an item is placed at the point he has moved to, and his health is strictly less than K, then he consumes the item there to make his health K.
Determine if Takahashi can complete the N moves without being stunned.
Constraints
- 1\leq N,M,H,K\leq 2\times 10^5
- S is a string of length N consisting of
R,L,U, andD. - |x_i|,|y_i| \leq 2\times 10^5
- (x_i, y_i) are pairwise distinct.
- All values in the input are integers, except for S.
Input
The input is given from Standard Input in the following format:
N M H K S x_1 y_1 \vdots x_M y_M
Output
Print Yes if he can complete the N moves without being stunned; print No otherwise.
Sample Input 1
4 2 3 1 RUDL -1 -1 1 0
Sample Output 1
Yes
Initially, Takahashi's health is 3. We describe the moves below.
-
1-st move: S_i is
R, so he moves to point (1,0). His health reduces to 2. Although an item is placed at point (1,0), he do not consume it because his health is no less than K=1. -
2-nd move: S_i is
U, so he moves to point (1,1). His health reduces to 1. -
3-rd move: S_i is
D, so he moves to point (1,0). His health reduces to 0. An item is placed at point (1,0), and his health is less than K=1, so he consumes the item to make his health 1. -
4-th move: S_i is
L, so he moves to point (0,0). His health reduces to 0.
Thus, he can make the 4 moves without collapsing, so Yes should be printed. Note that the health may reach 0.
Sample Input 2
5 2 1 5 LDRLD 0 0 -1 -1
Sample Output 2
No
Initially, Takahashi's health is 1. We describe the moves below.
-
1-st move: S_i is
L, so he moves to point (-1,0). His health reduces to 0. -
2-nd move: S_i is
D, so he moves to point (-1,-1). His health reduces to -1. Now that the health is -1, he collapses and stops moving.
Thus, he will be stunned, so No should be printed.
Note that although there is an item at his initial point (0,0), he does not consume it before the 1-st move, because items are only consumed after a move.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
1, 2, \ldots, N の番号がついた N 人の人が二次元平面上におり、人 i は座標 (X_i,Y_i) で表される地点にいます。
人 1 がウイルスに感染しました。ウイルスに感染した人から距離が D 以内にいる人にウイルスはうつります。
ただし、距離はユークリッド距離、すなわち 2 点 (a_1, a_2) と (b_1, b_2) に対し、この 2 点間の距離が \sqrt {(a_1-b_1)^2 + (a_2-b_2)^2} であるものとして定められています。
十分に時間が経過した、すなわち人 i がウイルスに感染しているならば 人 i との距離が D 以内にいるすべての人がウイルスに感染している状態になったときに、各 i について人 i がウイルスに感染しているか判定してください。
制約
- 1 \leq N, D \leq 2000
- -1000 \leq X_i, Y_i \leq 1000
- i \neq j のとき (X_i, Y_i) \neq (X_j, Y_j)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
N 行出力せよ。i 行目には、人 i がウイルスに感染しているならば Yes を、そうでないならば No を出力せよ。
入力例 1
4 5 2 -1 3 1 8 8 0 5
出力例 1
Yes Yes No Yes
人 1 と人 2 の距離は \sqrt 5 であるため、人 2 はウイルスに感染します。
また、人 2 と人 4 の距離は 5 であるため、人 4 はウイルスに感染します。
人 3 は距離 5 以内に人がいないので、ウイルスに感染することはありません。
入力例 2
3 1 0 0 -1000 -1000 1000 1000
出力例 2
Yes No No
入力例 3
9 4 3 2 6 -1 1 6 6 5 -2 -3 5 3 2 -3 2 1 2 6
出力例 3
Yes No No Yes Yes Yes Yes Yes No
Score : 300 points
Problem Statement
There are N people numbered 1, 2, \ldots, N on a two-dimensional plane, and person i is at the point represented by the coordinates (X_i,Y_i).
Person 1 has been infected with a virus. The virus spreads to people within a distance of D from an infected person.
Here, the distance is defined as the Euclidean distance, that is, for two points (a_1, a_2) and (b_1, b_2), the distance between these two points is \sqrt {(a_1-b_1)^2 + (a_2-b_2)^2}.
After a sufficient amount of time has passed, that is, when all people within a distance of D from person i are infected with the virus if person i is infected, determine whether person i is infected with the virus for each i.
Constraints
- 1 \leq N, D \leq 2000
- -1000 \leq X_i, Y_i \leq 1000
- (X_i, Y_i) \neq (X_j, Y_j) if i \neq j.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
Output
Print N lines. The i-th line should contain Yes if person i is infected with the virus, and No otherwise.
Sample Input 1
4 5 2 -1 3 1 8 8 0 5
Sample Output 1
Yes Yes No Yes
The distance between person 1 and person 2 is \sqrt 5, so person 2 gets infected with the virus.
Also, the distance between person 2 and person 4 is 5, so person 4 gets infected with the virus.
Person 3 has no one within a distance of 5, so they will not be infected with the virus.
Sample Input 2
3 1 0 0 -1000 -1000 1000 1000
Sample Output 2
Yes No No
Sample Input 3
9 4 3 2 6 -1 1 6 6 5 -2 -3 5 3 2 -3 2 1 2 6
Sample Output 3
Yes No No Yes Yes Yes Yes Yes No
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
正整数 N が与えられます。N は、2 つの相異なる素数 p,q を用いて N=p^2q と表せることがわかっています。
p,q を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 入力は全て整数
- 1\leq T\leq 10
- 1\leq N \leq 9\times 10^{18}
- N は、2 つの相異なる素数 p,q を用いて N=p^2q と表せる
入力
入力は以下の形式で標準入力から与えられる。ここで \text{test}_i は i 番目のテストケースを意味する。
T
\text{test}_1
\text{test}_2
\vdots
\text{test}_T
各テストケースは以下の形式で与えられる。
N
出力
T 行出力せよ。
i\ (1\leq i \leq T) 行目には、i 番目のテストケースにおける p,q を空白区切りで出力せよ。 なお、この問題の制約下では、N=p^2q を満たす素数 p,q の組は 1 通りしか存在しないことが証明できる。
入力例 1
3 2023 63 1059872604593911
出力例 1
17 7 3 7 104149 97711
1 番目のテストケースについて、N=2023=17^2\times 7 です。よって、p=17,q=7 です。
Score : 400 points
Problem Statement
You are given a positive integer N. It is known that N can be represented as N=p^2q using two different prime numbers p and q.
Find p and q.
You have T test cases to solve.
Constraints
- All values in the input are integers.
- 1\leq T\leq 10
- 1\leq N \leq 9\times 10^{18}
- N can be represented as N=p^2q using two different prime numbers p and q.
Input
The input is given from Standard Input in the following format, where \text{test}_i represents the i-th test case:
T
\text{test}_1
\text{test}_2
\vdots
\text{test}_T
Each test case is in the following format:
N
Output
Print T lines.
The i-th (1\leq i \leq T) line should contain p and q for the i-th test case, separated by a space. Under the constraints of this problem, it can be proved that the pair of prime numbers p and q such that N=p^2q is unique.
Sample Input 1
3 2023 63 1059872604593911
Sample Output 1
17 7 3 7 104149 97711
For the first test case, we have N=2023=17^2\times 7. Thus, p=17 and q=7.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
AtCoder 王国には都市 1,2,\ldots,N の N 個の都市と、道路 1,2,\ldots,M の M 本の道路があります。
道路 i は都市 A_i と B_i を双方向に結び、距離は C_i です。
どの都市間もいくつかの道路を通って行き来することができます。
財政難である王国は、どの都市間もいくつかの道路を通って行き来できるという条件を満たすように N-1 本の道路を保守し、それ以外の道路を廃道にすることにしました。
保守する道路のみを通って都市 1 から都市 i へ移動するときの距離を d_i とするとき、保守する道路の選び方であって、d_2+d_3+\ldots+d_N を最小化するようなものを 1 つ出力してください。
制約
- 2 \leq N \leq 2\times 10^5
- N-1 \leq M \leq 2\times 10^5
- 1 \leq A_i < B_i \leq N
- i\neq j のとき、(A_i,B_i)\neq(A_j,B_j)
- 1\leq C_i \leq 10^9
- どの都市間もいくつかの道路を通って行き来することができる
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
出力
保守するような道路の番号を空白区切りで出力せよ。出力の順序は問わない。
答えが複数存在する場合、どれを出力しても正解とみなされる。
入力例 1
3 3 1 2 1 2 3 2 1 3 10
出力例 1
1 2
保守する道路の選び方と d_i の値は次のようになります。
- 道路 1,2 を保守するとき、d_2=1, d_3=3
- 道路 1,3 を保守するとき、d_2=1, d_3=10
- 道路 2,3 を保守するとき、d_2=12, d_3=10
よって、道路 1,2 を保守するときに d_2+d_3 が最小になります。
入力例 2
4 6 1 2 1 1 3 1 1 4 1 2 3 1 2 4 1 3 4 1
出力例 2
3 1 2
Score : 500 points
Problem Statement
The Kingdom of AtCoder has N cities called City 1,2,\ldots,N and M roads called Road 1,2,\ldots,M.
Road i connects Cities A_i and B_i bidirectionally and has a length of C_i.
One can travel between any two cities using some roads.
Under financial difficulties, the kingdom has decided to maintain only N-1 roads so that one can still travel between any two cities using those roads and abandon the rest.
Let d_i be the total length of the roads one must use when going from City 1 to City i using only maintained roads. Print a choice of roads to maintain that minimizes d_2+d_3+\ldots+d_N.
Constraints
- 2 \leq N \leq 2\times 10^5
- N-1 \leq M \leq 2\times 10^5
- 1 \leq A_i < B_i \leq N
- (A_i,B_i)\neq(A_j,B_j) if i\neq j.
- 1\leq C_i \leq 10^9
- One can travel between any two cities using some roads.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
Output
Print the indices of roads to maintain, in arbitrary order, with spaces in between.
If multiple solutions exist, you may print any of them.
Sample Input 1
3 3 1 2 1 2 3 2 1 3 10
Sample Output 1
1 2
Here are the possible choices of roads to maintain and the corresponding values of d_i.
- Maintain Road 1 and 2: d_2=1, d_3=3.
- Maintain Road 1 and 3: d_2=1, d_3=10.
- Maintain Road 2 and 3: d_2=12, d_3=10.
Thus, maintaining Road 1 and 2 minimizes d_2+d_3.
Sample Input 2
4 6 1 2 1 1 3 1 1 4 1 2 3 1 2 4 1 3 4 1
Sample Output 2
3 1 2
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
A \cap B = \emptyset を満たす 2 つの整数の集合 A, B に対して、f(A,B) を以下のように定義します。
- A \cup B に含まれる要素を昇順に並べた数列を C=(C_1,C_2,\dots,C_{|A|+|B|}) とする。
- A=\lbrace C_{k_1},C_{k_2},\dots,C_{k_{|A|}}\rbrace となるような k_1,k_2,\dots,k_{|A|} をとる。 このとき、\displaystyle f(A,B)=\sum_{i=1}^{|A|} k_i とする。
例えば、A=\lbrace 1,3\rbrace,B=\lbrace 2,8\rbrace のとき、C=(1,2,3,8) より A=\lbrace C_1,C_3\rbrace なので、f(A,B)=1+3=4 です。
それぞれが M 個の要素からなる N 個の整数の集合 S_1,S_2\dots,S_N があり、各 i\ (1 \leq i \leq N) について S_i = \lbrace A_{i,1},A_{i,2},\dots,A_{i,M}\rbrace です。 ただし、S_i \cap S_j = \emptyset\ (i \neq j) が保証されます。
\displaystyle \sum_{1\leq i<j \leq N} f(S_i, S_j) を求めてください。
制約
- 1\leq N \leq 10^4
- 1\leq M \leq 10^2
- 1\leq A_{i,j} \leq 10^9
- i_1 \neq i_2 または j_1 \neq j_2 ならば A_{i_1,j_1} \neq A_{i_2,j_2}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M
A_{1,1} A_{1,2} \dots A_{1,M}
\vdots
A_{N,1} A_{N,2} \dots A_{N,M}
出力
答えを整数として出力せよ。
入力例 1
3 2 1 3 2 8 4 6
出力例 1
12
S_1,S_2 はそれぞれ問題文中で例示した A,B と一致し、f(S_1,S_2)=1+3=4 です。 f(S_1,S_3)=1+2=3,f(S_2,S_3)=1+4=5 であるため、4+3+5=12 が答えです。
入力例 2
1 1 306
出力例 2
0
入力例 3
4 4 155374934 164163676 576823355 954291757 797829355 404011431 353195922 138996221 191890310 782177068 818008580 384836991 160449218 545531545 840594328 501899080
出力例 3
102
Score : 525 points
Problem Statement
For two sets of integers, A and B, such that A \cap B = \emptyset, we define f(A,B) as follows.
- Let C=(C_1,C_2,\dots,C_{|A|+|B|}) be a sequence consisting of the elements of A \cup B, sorted in ascending order.
- Take k_1,k_2,\dots,k_{|A|} such that A=\lbrace C_{k_1},C_{k_2},\dots,C_{k_{|A|}}\rbrace. Then, let \displaystyle f(A,B)=\sum_{i=1}^{|A|} k_i.
For example, if A=\lbrace 1,3\rbrace and B=\lbrace 2,8\rbrace, then C=(1,2,3,8), so A=\lbrace C_1,C_3\rbrace; thus, f(A,B)=1+3=4.
We have N sets of integers, S_1,S_2\dots,S_N, each of which has M elements. For each i\ (1 \leq i \leq N), S_i = \lbrace A_{i,1},A_{i,2},\dots,A_{i,M}\rbrace. Here, it is guaranteed that S_i \cap S_j = \emptyset\ (i \neq j).
Find \displaystyle \sum_{1\leq i<j \leq N} f(S_i, S_j).
Constraints
- 1\leq N \leq 10^4
- 1\leq M \leq 10^2
- 1\leq A_{i,j} \leq 10^9
- If i_1 \neq i_2 or j_1 \neq j_2, then A_{i_1,j_1} \neq A_{i_2,j_2}.
- 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} \dots A_{1,M}
\vdots
A_{N,1} A_{N,2} \dots A_{N,M}
Output
Print the answer as an integer.
Sample Input 1
3 2 1 3 2 8 4 6
Sample Output 1
12
S_1 and S_2 respectively coincide with A and B exemplified in the problem statement, and f(S_1,S_2)=1+3=4. Since f(S_1,S_3)=1+2=3 and f(S_2,S_3)=1+4=5, the answer is 4+3+5=12.
Sample Input 2
1 1 306
Sample Output 2
0
Sample Input 3
4 4 155374934 164163676 576823355 954291757 797829355 404011431 353195922 138996221 191890310 782177068 818008580 384836991 160449218 545531545 840594328 501899080
Sample Output 3
102