Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
文字列 S が与えられます。次の操作を ちょうど 1 回 行った後の文字列としてあり得るものがいくつあるか求めてください。
- S の長さを N とする。 1\leq i<j\leq N をみたす整数の組 (i,j) を選び、S の i 文字目と j 文字目を入れ替える。
なお、この問題の制約下で操作を必ず行うことができることが証明できます。
制約
- S は英小文字からなる長さ 2 以上 10^6 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S に対して問題文中の操作をちょうど 1 回行った後の文字列としてあり得るものの個数を出力せよ。
入力例 1
abc
出力例 1
3
S の長さは 3 であるため、1\leq i<j\leq 3 をみたす整数の組 (i,j) としては、 (1,2), (1,3), (2,3) の 3 通りが考えられます。
- S の 1 文字目と 2 文字目を入れ替えた時、S は
bacとなります。 - S の 1 文字目と 3 文字目を入れ替えた時、S は
cbaとなります。 - S の 2 文字目と 3 文字目を入れ替えた時、S は
acbとなります。
よって、abc に対して操作を行った後の文字列としては、bac, cba, acb の 3 つがあり得るため、3 を出力します。
入力例 2
aaaaa
出力例 2
1
どの 2 つの文字を入れ替えても S は aaaaa のままです。よって、操作後の文字列としてあり得るものは 1 つです。
Points: 350 points
Problem Statement
You are given a string S. Find the number of strings that can result from performing the following operation exactly once.
- Let N be the length of S. Choose a pair of integers (i,j) such that 1\leq i<j\leq N and swap the i-th and j-th characters of S.
It can be proved that you can always perform it under the constraints of this problem.
Constraints
- S is a string of length between 2 and 10^6, inclusive, consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print the number of strings that can result from performing the operation in the problem statement exactly once on S.
Sample Input 1
abc
Sample Output 1
3
The length of S is 3, so 1\leq i<j\leq 3 is satisfied by three pairs of integers (i,j): (1,2), (1,3), and (2,3).
- Swapping the 1-st and 2-nd characters of S results in S being
bac. - Swapping the 1-st and 3-rd characters of S results in S being
cba. - Swapping the 2-nd and 3-rd characters of S results in S being
acb.
Therefore, the operation on abc results in one of the three strings: bac, cba, and acb, so print 3.
Sample Input 2
aaaaa
Sample Output 2
1
Swapping any two characters results in S remaining aaaaa. Thus, only one string can result from the operation.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
正整数 N が与えられます。
正整数の組 (A,B,C,D) であって、AB + CD = N を満たすものの個数を求めてください。
なお、本問の制約の下、答えが 9 \times 10^{18} 以下であることが証明できます。
制約
- 2 \leq N \leq 2 \times 10^5
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
4
出力例 1
8
(A,B,C,D) として以下の 8 個が考えられます。
- (A,B,C,D)=(1,1,1,3)
- (A,B,C,D)=(1,1,3,1)
- (A,B,C,D)=(1,2,1,2)
- (A,B,C,D)=(1,2,2,1)
- (A,B,C,D)=(1,3,1,1)
- (A,B,C,D)=(2,1,1,2)
- (A,B,C,D)=(2,1,2,1)
- (A,B,C,D)=(3,1,1,1)
入力例 2
292
出力例 2
10886
入力例 3
19876
出力例 3
2219958
Score : 300 points
Problem Statement
You are given a positive integer N.
Find the number of quadruples of positive integers (A,B,C,D) such that AB + CD = N.
Under the constraints of this problem, it can be proved that the answer is at most 9 \times 10^{18}.
Constraints
- 2 \leq N \leq 2 \times 10^5
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
4
Sample Output 1
8
Here are the eight desired quadruples.
- (A,B,C,D)=(1,1,1,3)
- (A,B,C,D)=(1,1,3,1)
- (A,B,C,D)=(1,2,1,2)
- (A,B,C,D)=(1,2,2,1)
- (A,B,C,D)=(1,3,1,1)
- (A,B,C,D)=(2,1,1,2)
- (A,B,C,D)=(2,1,2,1)
- (A,B,C,D)=(3,1,1,1)
Sample Input 2
292
Sample Output 2
10886
Sample Input 3
19876
Sample Output 3
2219958
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
AtCoder Land のお土産屋に N 個の箱が売られています。
箱には 1, 2, \ldots, N の番号が付いており、箱 i の価格は A_i 円であり、A_i 個のお菓子が入っています。
高橋君は N 個の箱のうち M 個の箱を選んで買って帰り、1, 2, \ldots, M の名前が付いた M 人の人に 1 つずつ箱を渡そうとしています。
ただし、高橋君は以下の条件を満たすことができるように箱を買いたいです。
- 各 i = 1, 2, \ldots, M について、人 i には B_i 個以上のお菓子が入った箱を渡す
1 人に 2 個以上の箱を渡すことや同じ箱を複数人に渡すことはできないことに注意してください。
適切に箱を M 個買うことで条件を満たすことができるか判定し、条件を満たすことができる場合は高橋君が支払う金額の合計の最小値を求めてください。
制約
- 1 \leq M \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_M
出力
適切に箱を M 個買うことで条件を満たすことができる場合は高橋君が支払う金額の合計の最小値を出力せよ。そうでない場合は -1 を出力せよ。
入力例 1
4 2 3 4 5 4 1 4
出力例 1
7
高橋君は箱 1, 4 を買い、箱 1 を人 1、箱 4 を人 2 に渡すことで条件を満たすことができます。
このとき高橋君が支払う金額の合計は 7 円であり、支払う金額が 7 円未満のときは条件を満たすことはできないため、7 を出力します。
入力例 2
3 3 1 1 1 1000000000 1000000000 1000000000
出力例 2
-1
入力例 3
7 3 2 6 8 9 5 1 11 3 5 7
出力例 3
19
Score : 350 points
Problem Statement
A souvenir shop at AtCoder Land sells N boxes.
The boxes are numbered 1 to N, and box i has a price of A_i yen and contains A_i pieces of candy.
Takahashi wants to buy M out of the N boxes and give one box each to M people named 1, 2, \ldots, M.
Here, he wants to buy boxes that can satisfy the following condition:
- For each i = 1, 2, \ldots, M, person i is given a box containing at least B_i pieces of candy.
Note that it is not allowed to give more than one box to a single person or to give the same box to multiple people.
Determine whether it is possible to buy M boxes that can satisfy the condition, and if it is possible, find the minimum total amount of money Takahashi needs to pay.
Constraints
- 1 \leq M \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq 10^9
- All input values 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
If it is possible to buy M boxes that can satisfy the condition, print the minimum total amount of money Takahashi needs to pay. Otherwise, print -1.
Sample Input 1
4 2 3 4 5 4 1 4
Sample Output 1
7
Takahashi can buy boxes 1 and 4, and give box 1 to person 1 and box 4 to person 2 to satisfy the condition.
In this case, he needs to pay 7 yen in total, and it is impossible to satisfy the condition by paying less than 7 yen, so print 7.
Sample Input 2
3 3 1 1 1 1000000000 1000000000 1000000000
Sample Output 2
-1
Sample Input 3
7 3 2 6 8 9 5 1 11 3 5 7
Sample Output 3
19
Time Limit: 6 sec / Memory Limit: 1024 MiB
配点 : 500 点
コンテスト開催当時はメモリ制限が2GBでしたが、ジャッジ環境が異なるため、現在はメモリ制限を1GBに設定しております。なお、このメモリ制限でもAC出来ることは確認しています。
問題文
ここに、 N \times N のチェス盤があります。このチェス盤の上から i 行目、左から j 列目にあるマスをマス (i,j) と呼びます。
チェス盤の情報は N 個の文字列 S_i として与えられます。
文字列 S_i の j 文字目である S_{i,j} には、以下の情報が含まれています。
- S_{i,j}=
.のとき マス (i,j) には何も置かれていない。 - S_{i,j}=
#のとき マス (i,j) には白のポーンが 1 つ置かれている。このポーンを動かしたり取り除いたりすることはできない。
この盤面のマス (A_x,A_y) に、白のビショップを 1 つ置きました。
この白のビショップをチェスのルール (注記参照) に従ってマス (A_x,A_y) からマス (B_x,B_y) に移動させるために必要な最小の手数を求めてください。
ただし、移動できない場合は代わりに -1 を出力してください。
注記
マス (i,j) に置かれている白の ビショップ は、 1 手で以下のルールに従って移動することができます。
-
各正整数 d について、以下の条件を全て満たせばマス (i+d,j+d) に移動できる。
- マス (i+d,j+d) が盤内に存在する
- 全ての正整数 l \le d について、 (i+l,j+l) に白のポーンがない
-
各正整数 d について、以下の条件を全て満たせばマス (i+d,j-d) に移動できる。
- マス (i+d,j-d) が盤内に存在する
- 全ての正整数 l \le d について、 (i+l,j-l) に白のポーンがない
-
各正整数 d について、以下の条件を全て満たせばマス (i-d,j+d) に移動できる。
- マス (i-d,j+d) が盤内に存在する
- 全ての正整数 l \le d について、 (i-l,j+l) に白のポーンがない
-
各正整数 d について、以下の条件を全て満たせばマス (i-d,j-d) に移動できる。
- マス (i-d,j-d) が盤内に存在する
- 全ての正整数 l \le d について、 (i-l,j-l) に白のポーンがない
制約
- 2 \le N \le 1500
- 1 \le A_x,A_y \le N
- 1 \le B_x,B_y \le N
- (A_x,A_y) \neq (B_x,B_y)
- S_i は
.および#からなる N 文字の文字列 - S_{A_x,A_y}=
. - S_{B_x,B_y}=
.
入力
入力は以下の形式で標準入力から与えられる。
N A_x A_y B_x B_y S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
5 1 3 3 5 ....# ...#. ..... .#... #....
出力例 1
3
以下のように移動させることで 3 手でビショップを (1,3) から (3,5) まで移動させることができます。 2 手以内でビショップを (1,3) から (3,5) まで移動させることはできません。
- (1,3) \rightarrow (2,2) \rightarrow (4,4) \rightarrow (3,5)
入力例 2
4 3 2 4 2 .... .... .... ....
出力例 2
-1
どのようにビショップを動かしても (3,2) から (4,2) に移動させることはできません。
入力例 3
18 18 1 1 18 .................. .####............. .#..#..####....... .####..#..#..####. .#..#..###...#.... .#..#..#..#..#.... .......####..#.... .............####. .................. .................. .####............. ....#..#..#....... .####..#..#..####. .#.....####..#.... .####.....#..####. ..........#..#..#. .............####. ..................
出力例 3
9
Score : 500 points
During the time of the contest, the memory limit was set to 2GB. However, due to a change in the judging environment, the memory limit has now been set to 1GB. Please note that it has been confirmed that solutions can still achieve an Acceptable Completion (AC) within this memory limit.
Problem Statement
We have an N \times N chessboard. Let (i, j) denote the square at the i-th row from the top and j-th column from the left of this board.
The board is described by N strings S_i.
The j-th character of the string S_i, S_{i,j}, means the following.
- If S_{i,j}=
., the square (i, j) is empty. - If S_{i,j}=
#, the square (i, j) is occupied by a white pawn, which cannot be moved or removed.
We have put a white bishop on the square (A_x, A_y).
Find the minimum number of moves needed to move this bishop from (A_x, A_y) to (B_x, B_y) according to the rules of chess (see Notes).
If it cannot be moved to (B_x, B_y), report -1 instead.
Notes
A white bishop on the square (i, j) can go to the following positions in one move.
-
For each positive integer d, it can go to (i+d,j+d) if all of the conditions are satisfied.
- The square (i+d,j+d) exists in the board.
- For every positive integer l \le d, (i+l,j+l) is not occupied by a white pawn.
-
For each positive integer d, it can go to (i+d,j-d) if all of the conditions are satisfied.
- The square (i+d,j-d) exists in the board.
- For every positive integer l \le d, (i+l,j-l) is not occupied by a white pawn.
-
For each positive integer d, it can go to (i-d,j+d) if all of the conditions are satisfied.
- The square (i-d,j+d) exists in the board.
- For every positive integer l \le d, (i-l,j+l) is not occupied by a white pawn.
-
For each positive integer d, it can go to (i-d,j-d) if all of the conditions are satisfied.
- The square (i-d,j-d) exists in the board.
- For every positive integer l \le d, (i-l,j-l) is not occupied by a white pawn.
Constraints
- 2 \le N \le 1500
- 1 \le A_x,A_y \le N
- 1 \le B_x,B_y \le N
- (A_x,A_y) \neq (B_x,B_y)
- S_i is a string of length N consisting of
.and#. - S_{A_x,A_y}=
. - S_{B_x,B_y}=
.
Input
Input is given from Standard Input in the following format:
N A_x A_y B_x B_y S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
5 1 3 3 5 ....# ...#. ..... .#... #....
Sample Output 1
3
We can move the bishop from (1,3) to (3,5) in three moves as follows, but not in two or fewer moves.
- (1,3) \rightarrow (2,2) \rightarrow (4,4) \rightarrow (3,5)
Sample Input 2
4 3 2 4 2 .... .... .... ....
Sample Output 2
-1
There is no way to move the bishop from (3,2) to (4,2).
Sample Input 3
18 18 1 1 18 .................. .####............. .#..#..####....... .####..#..#..####. .#..#..###...#.... .#..#..#..#..#.... .......####..#.... .............####. .................. .................. .####............. ....#..#..#....... .####..#..#..####. .#.....####..#.... .####.....#..####. ..........#..#..#. .............####. ..................
Sample Output 3
9
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点の木が与えられます。頂点には 1,2,\ldots ,N の番号がついており、i 番目の辺は頂点 u_i,v_i を結ぶ無向辺です。
各整数 i\,(1 \leq i \leq N) に対して、\sum_{j=1}^{N}dis(i,j) を求めてください。
ただし、dis(i,j) は頂点 i から頂点 j に到達する際にたどる必要のある最小の辺数です。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq u_i < v_i \leq N
- 与えられるグラフは木
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
出力
N 行出力せよ。
i 行目には \sum_{j=1}^{N}dis(i,j) を出力せよ。
入力例 1
3 1 2 2 3
出力例 1
3 2 3
dis(1,1)+dis(1,2)+dis(1,3)=0+1+2=3、
dis(2,1)+dis(2,2)+dis(2,3)=1+0+1=2、
dis(3,1)+dis(3,2)+dis(3,3)=2+1+0=3、
です。
入力例 2
2 1 2
出力例 2
1 1
入力例 3
6 1 6 1 5 1 3 1 4 1 2
出力例 3
5 9 9 9 9 9
Score : 500 points
Problem Statement
Given is a tree with N vertices. The vertices are numbered 1,2,\ldots ,N, and the i-th edge is an undirected edge connecting Vertices u_i and v_i.
For each integer i\,(1 \leq i \leq N), find \sum_{j=1}^{N}dis(i,j).
Here, dis(i,j) denotes the minimum number of edges that must be traversed to go from Vertex i to Vertex j.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq u_i < v_i \leq N
- The given graph is a tree.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
Output
Print N lines.
The i-th line should contain \sum_{j=1}^{N}dis(i,j).
Sample Input 1
3 1 2 2 3
Sample Output 1
3 2 3
We have:
dis(1,1)+dis(1,2)+dis(1,3)=0+1+2=3,
dis(2,1)+dis(2,2)+dis(2,3)=1+0+1=2,
dis(3,1)+dis(3,2)+dis(3,3)=2+1+0=3.
Sample Input 2
2 1 2
Sample Output 2
1 1
Sample Input 3
6 1 6 1 5 1 3 1 4 1 2
Sample Output 3
5 9 9 9 9 9