Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
4 個のボールがあり、i 個目のボールの色は A_i です。
同じ色のボールを 2 つ選び両方捨てるという操作を最大何回行えるか求めてください。
制約
- A_1,A_2,A_3,A_4 はそれぞれ 1 以上 4 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
A_1 A_2 A_3 A_4
出力
操作回数の最大値を整数として出力せよ。
入力例 1
2 1 2 1
出力例 1
2
1 個目のボールと 3 個目のボールはどちらも色が 2 なので、1 個目のボールと 3 個目のボールを共に捨てる操作を行えます。
次に、2 個目のボールと 4 個目のボールはどちらも色が 1 なので、2 個目のボールと 4 個目のボールを共に捨てる操作を行えます。
合計で 2 回操作を行えます。
入力例 2
4 4 4 1
出力例 2
1
入力例 3
1 2 3 4
出力例 3
0
操作を一度も行えない場合もあります。
Score : 100 points
Problem Statement
There are four balls, and the color of the i-th ball is A_i.
Find the maximum number of times you can perform this operation: choose two balls of the same color and discard both.
Constraints
- Each of A_1, A_2, A_3, A_4 is an integer between 1 and 4, inclusive.
Input
The input is given from Standard Input in the following format:
A_1 A_2 A_3 A_4
Output
Print the maximum number of times the operation can be performed as an integer.
Sample Input 1
2 1 2 1
Sample Output 1
2
The first and third balls both have color 2, so you can perform the operation to discard the first and third balls together.
Next, the second and fourth balls both have color 1, so you can perform the operation to discard the second and fourth balls together.
Hence, you can perform a total of two operations.
Sample Input 2
4 4 4 1
Sample Output 2
1
Sample Input 3
1 2 3 4
Sample Output 3
0
There are cases where you cannot perform the operation even once.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
AtCoder 市では、N 種類のゴミを定期的に収集しています。i\;(=1,2,\dots,N) 種類目のゴミは、日付を q_i で割ったあまりが r_i の日に収集されます。
Q 個の質問に答えてください。j\;(=1,2,\dots,Q) 番目の質問では、d_j 日に t_j 種類目のゴミが出たときに、次にそれが収集される日を答えてください。
ただし、i 種類目のゴミが出た日が、 i 種類目のゴミが回収される日であった場合、そのゴミは同じ日に収集されるとします。
制約
- 1 \leq N \leq 100
- 0 \leq r_i < q_i \leq 10^9
- 1 \leq Q \leq 100
- 1 \leq t_j \leq N
- 1 \leq d_j \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N q_1 r_1 q_2 r_2 \vdots q_N r_N Q t_1 d_1 t_2 d_2 \vdots t_Q d_Q
出力
Q 行出力せよ。j\;(1\leq j \leq Q) 行目には、j 番目の質問に対する答えを出力せよ。
入力例 1
2 7 3 4 2 5 1 1 1 3 1 4 1 15 2 7
出力例 1
3 3 10 17 10
- 1 番目の質問:1 種類目のゴミが 1 日以降に初めて回収されるのは 3 日です。
- 2 番目の質問:1 種類目のゴミが 3 日以降に初めて回収されるのは 3 日です。
- 3 番目の質問:1 種類目のゴミが 4 日以降に初めて回収されるのは 10 日です。
Score : 200 points
Problem Statement
In AtCoder City, N types of garbage are collected regularly. The i-th type of garbage (i=1,2,\dots,N) is collected on days when the date modulo q_i equals r_i.
Answer Q queries. In the j-th query (j=1,2,\dots,Q), given that the t_j-th type of garbage is put out on day d_j, answer the next day on which it will be collected.
Here, if the i-th type of garbage is put out on a day when that type of garbage is collected, then the garbage will be collected on the same day.
Constraints
- 1 \leq N \leq 100
- 0 \leq r_i < q_i \leq 10^9
- 1 \leq Q \leq 100
- 1 \leq t_j \leq N
- 1 \leq d_j \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N q_1 r_1 q_2 r_2 \vdots q_N r_N Q t_1 d_1 t_2 d_2 \vdots t_Q d_Q
Output
Print Q lines. The j-th line (1\leq j \leq Q) should contain the answer to the j-th query.
Sample Input 1
2 7 3 4 2 5 1 1 1 3 1 4 1 15 2 7
Sample Output 1
3 3 10 17 10
- 1st query: The 1st type of garbage is collected on day 3 for the first time after day 1.
- 2nd query: The 1st type of garbage is collected on day 3 for the first time after day 3.
- 3rd query: The 1st type of garbage is collected on day 10 for the first time after day 4.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の正数列 A=(A_1,A_2,\dots,A_N) が与えられます。以下で定義される長さ N の数列 B = (B_1,B_2,\dots,B_N) を求めてください。
- i=1,2,\dots,N について、B_i を次のように定義する:
- A_i と等しい要素が i の直前に出現した位置を B_i とする。そのような位置が存在しなければ B_i=-1 とする。
より正確には、正整数 j であって,A_i = A_j となる j < i が存在するならば、そのうち最大の j を B_i とする。そのような j が存在しなければ B_i=-1 とする。
- A_i と等しい要素が i の直前に出現した位置を B_i とする。そのような位置が存在しなければ B_i=-1 とする。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
B の要素を空白区切りで1行に出力せよ。
入力例 1
5 1 2 1 1 3
出力例 1
-1 -1 1 3 -1
- i=1: A_1=1 より前に 1 は現れないので、B_1=-1 です。
- i=2: A_2=2 より前に 2 は現れないので、B_2=-1 です。
- i=3: A_3=1 の直前に現れた 1 は A_1 なので、B_3=1 です。
- i=4: A_4=1 の直前に現れた 1 は A_3 なので、B_4=3 です。
- i=5: A_5=3 より前に 3 は現れないので、B_5=-1 です。
入力例 2
4 1 1000000000 1000000000 1
出力例 2
-1 -1 2 1
Score : 300 points
Problem Statement
You are given a sequence of N positive numbers, A = (A_1, A_2, \dots, A_N). Find the sequence B = (B_1, B_2, \dots, B_N) of length N defined as follows.
- For i = 1, 2, \dots, N, define B_i as follows:
- Let B_i be the most recent position before i where an element equal to A_i appeared. If such a position does not exist, let B_i = -1.
More precisely, if there exists a positive integer j such that A_i = A_j and j < i, let B_i be the largest such j. If no such j exists, let B_i = -1.
- Let B_i be the most recent position before i where an element equal to A_i appeared. If such a position does not exist, let B_i = -1.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the elements of B in one line, separated by spaces.
Sample Input 1
5 1 2 1 1 3
Sample Output 1
-1 -1 1 3 -1
- i = 1: There is no 1 before A_1 = 1, so B_1 = -1.
- i = 2: There is no 2 before A_2 = 2, so B_2 = -1.
- i = 3: The most recent occurrence of 1 before A_3 = 1 is A_1, so B_3 = 1.
- i = 4: The most recent occurrence of 1 before A_4 = 1 is A_3, so B_4 = 3.
- i = 5: There is no 3 before A_5 = 3, so B_5 = -1.
Sample Input 2
4 1 1000000000 1000000000 1
Sample Output 2
-1 -1 2 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
H 行 W 列のマス目があります。マス目の上から i 番目、左から j 番目のマスをマス (i,j) と表記します。
マス (i,j) は S_{i,j} が .
のとき空きマスであり、#
のとき障害物があります。
ある空きマスを出発し、上下左右に隣接するマスへの移動を K 回行う方法であって、障害物のあるマスを通らず、同じマスを 2 回以上通らないようなものの個数を数えてください。
具体的には、長さ K+1 の列 ((i_0,j_0),(i_1,j_1),\dots,(i_K,j_K)) であって、以下を満たすものの個数を数えてください。
- 各 0 \leq k \leq K について、 1 \leq i_k \leq H, 1 \leq j_k \leq W かつ S_{i_k,j_k} は
.
である - 各 0 \leq k \leq K-1 について、 |i_{k+1}-i_k| + |j_{k+1}-j_k| = 1
- 各 0 \leq k < l \leq K について、 (i_k,j_k)\neq (i_l,j_l) である
制約
- 1 \leq H,W \leq 10
- 1 \leq K \leq 11
- H,W,K は整数
- S_{i,j} は
.
または#
- 空きマスが少なくとも 1 つ存在する
入力
入力は以下の形式で標準入力から与えられる。
H W K S_{1,1}S_{1,2}\dots S_{1,W} S_{2,1}S_{2,2}\dots S_{2,W} \vdots S_{H,1}S_{H,2}\dots S_{H,W}
出力
答えを出力せよ。
入力例 1
2 2 2 .# ..
出力例 1
2
可能な経路は、以下の 2 通りです。
- (1,1) \to (2,1) \to (2,2)
- (2,2) \to (2,1) \to (1,1)
入力例 2
2 3 1 .#. #.#
出力例 2
0
入力例 3
10 10 11 ....#..#.. .#.....##. ..#...##.. ...#...... ......##.. ..#......# #........# ..##...... .###....#. ...#.....#
出力例 3
218070
Score : 425 points
Problem Statement
There is a grid of H \times W cells. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.
Cell (i, j) is empty if S_{i,j} is .
, and blocked if it is #
.
Count the number of ways to start from an empty cell and make K moves to adjacent cells (up, down, left, or right), without passing through blocked squares and not visiting the same cell more than once.
Specifically, count the number of sequences of length K+1, ((i_0, j_0), (i_1, j_1), \dots, (i_K, j_K)), satisfying the following.
- 1 \leq i_k \leq H, 1 \leq j_k \leq W, and S_{i_k, j_k} is
.
, for each 0 \leq k \leq K. - |i_{k+1} - i_k| + |j_{k+1} - j_k| = 1 for each 0 \leq k \leq K-1.
- (i_k, j_k) \neq (i_l, j_l) for each 0 \leq k < l \leq K.
Constraints
- 1 \leq H, W \leq 10
- 1 \leq K \leq 11
- H, W, and K are integers.
- Each S_{i,j} is
.
or#
. - There is at least one empty cell.
Input
The input is given from Standard Input in the following format:
H W K S_{1,1}S_{1,2}\dots S_{1,W} S_{2,1}S_{2,2}\dots S_{2,W} \vdots S_{H,1}S_{H,2}\dots S_{H,W}
Output
Print the answer.
Sample Input 1
2 2 2 .# ..
Sample Output 1
2
Here are the two possible paths:
- (1,1) \rightarrow (2,1) \rightarrow (2,2)
- (2,2) \rightarrow (2,1) \rightarrow (1,1)
Sample Input 2
2 3 1 .#. #.#
Sample Output 2
0
Sample Input 3
10 10 11 ....#..#.. .#.....##. ..#...##.. ...#...... ......##.. ..#......# #........# ..##...... .###....#. ...#.....#
Sample Output 3
218070
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
長さ N の非負整数列 A=(A_1,A_2,\dots,A_N) と、正整数 M が与えられます。
次の値を求めてください。
\[ \sum_{1 \leq l \leq r \leq N} \left( \left(\sum_{l \leq i \leq r} A_i\right) \mathbin{\mathrm{mod}} M \right) \]
ここで、X \mathbin{\mathrm{mod}} M で、非負整数 X を M で割ったあまりを表します。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq A_i \leq 10^9
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
3 4 2 5 0
出力例 1
10
- A_1 \mathbin{\mathrm{mod}} M = 2
- (A_1+A_2) \mathbin{\mathrm{mod}} M = 3
- (A_1+A_2+A_3) \mathbin{\mathrm{mod}} M = 3
- A_2 \mathbin{\mathrm{mod}} M = 1
- (A_2+A_3) \mathbin{\mathrm{mod}} M = 1
- A_3 \mathbin{\mathrm{mod}} M = 0
これらの総和の 10 が答えです。外側の総和のあまりは取らないことに注意してください。
入力例 2
10 100 320 578 244 604 145 839 156 857 556 400
出力例 2
2736
Score : 475 points
Problem Statement
You are given a sequence A = (A_1, A_2, \dots, A_N) of N non-negative integers, and a positive integer M.
Find the following value:
\[ \sum_{1 \leq l \leq r \leq N} \left( \left(\sum_{l \leq i \leq r} A_i\right) \mathbin{\mathrm{mod}} M \right). \]
Here, X \mathbin{\mathrm{mod}} M denotes the remainder when the non-negative integer X is divided by M.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 0 \leq A_i \leq 10^9
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
3 4 2 5 0
Sample Output 1
10
- A_1 \mathbin{\mathrm{mod}} M = 2
- (A_1+A_2) \mathbin{\mathrm{mod}} M = 3
- (A_1+A_2+A_3) \mathbin{\mathrm{mod}} M = 3
- A_2 \mathbin{\mathrm{mod}} M = 1
- (A_2+A_3) \mathbin{\mathrm{mod}} M = 1
- A_3 \mathbin{\mathrm{mod}} M = 0
The answer is the sum of these values, 10. Note that the outer sum is not taken modulo M.
Sample Input 2
10 100 320 578 244 604 145 839 156 857 556 400
Sample Output 2
2736
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点の木が与えられます。i 番目の辺 (1\leq i\leq N-1) は頂点 u_i と頂点 v_i を双方向に結んでいます。
与えられた木に無向辺を一本追加して得られるグラフは、必ずちょうど一つの閉路を含みます。
そのようなグラフのうち、以下の条件を全て満たすものの個数を求めてください。
- グラフは単純グラフ
- グラフの閉路に含まれる頂点の次数は全て 3
制約
- 3 \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}
出力
答えを出力せよ。
入力例 1
6 1 2 2 3 3 4 4 5 3 6
出力例 1
1
頂点 2 と頂点 4 を結ぶ辺を追加して得られるグラフは単純グラフであり、閉路に含まれる頂点の次数は全て 3 なので条件を満たします。
入力例 2
7 1 2 2 7 3 5 7 3 6 2 4 7
出力例 2
0
条件を満たすグラフが存在しない場合もあります。
入力例 3
15 1 15 11 14 2 10 1 7 9 8 6 9 4 12 14 5 4 9 8 11 7 4 1 13 3 6 11 10
出力例 3
6
Score : 500 points
Problem Statement
You are given a tree with N vertices. The i-th edge (1 \leq i \leq N-1) connects vertices u_i and v_i bidirectionally.
Adding one undirected edge to the given tree always yields a graph with exactly one cycle.
Among such graphs, how many satisfy all of the following conditions?
- The graph is simple.
- All vertices in the cycle have degree 3.
Constraints
- 3 \leq N \leq 2 \times 10^5
- 1 \leq u_i, v_i \leq N
- The given graph is a tree.
- All input values are integers.
Input
The 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 the answer.
Sample Input 1
6 1 2 2 3 3 4 4 5 3 6
Sample Output 1
1
Adding an edge connecting vertices 2 and 4 yields a simple graph where all vertices in the cycle have degree 3, so it satisfies the conditions.
Sample Input 2
7 1 2 2 7 3 5 7 3 6 2 4 7
Sample Output 2
0
There are cases where no graphs satisfy the conditions.
Sample Input 3
15 1 15 11 14 2 10 1 7 9 8 6 9 4 12 14 5 4 9 8 11 7 4 1 13 3 6 11 10
Sample Output 3
6
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 650 点
問題文
整数 A,B,M が与えられます。
(1,2,\ldots,AB-1) の順列 P=(P_1,\ldots,P_{AB-1}) であって、以下の条件を全て満たすものは何通りありますか?個数を M で割ったあまりを求めてください。
- P の最長増加部分列の長さは A
- P の最長減少部分列の長さは B
- 整数 n であって、P の末尾に n+0.5 を追加しても最長増加部分列の長さも最長減少部分列の長さも変化しないようなものが存在する
制約
- 入力される数値は全て整数
- 2\leq A,B
- AB\leq 120
- 10^8\leq M\leq 10^9
- M は素数
入力
入力は以下の形式で標準入力から与えられる。
A B M
出力
条件を満たす順列の個数を M で割ったあまりを出力せよ。
入力例 1
3 2 998244353
出力例 1
10
例えば、P=(2,4,5,1,3) は条件を満たします。これは以下のようにして確認できます。
- P の最長増加部分列の長さは 3
- P の最長減少部分列の長さは 2
- n=4 とすると、(2,4,5,1,3,4.5) の最長増加部分列の長さは 3 かつ最長減少部分列の長さは 2
条件を満たす (1,2,3,4,5) の順列は 10 通りです。
入力例 2
10 12 924844033
出力例 2
623378361
個数を M で割ったあまりを出力してください。
Score : 650 points
Problem Statement
You are given integers A, B, and M.
How many permutations P = (P_1, \dots, P_{AB-1}) of (1, 2, \ldots, AB - 1) satisfy all of the following conditions? Find the count modulo M.
- The length of a longest increasing subsequence of P is A.
- The length of a longest decreasing subsequence of P is B.
- There exists an integer n such that appending n + 0.5 to the end of P does not change either of the lengths of a longest increasing subsequence and a longest decreasing subsequence.
Constraints
- All input values are integers.
- 2 \leq A, B
- AB \leq 120
- 10^8 \leq M \leq 10^9
- M is a prime.
Input
The input is given from Standard Input in the following format:
A B M
Output
Print the number of permutations satisfying the conditions, modulo M.
Sample Input 1
3 2 998244353
Sample Output 1
10
For example, P = (2, 4, 5, 1, 3) satisfies the conditions. This can be confirmed as follows:
- The length of a longest increasing subsequence of P is 3.
- The length of a longest decreasing subsequence of P is 2.
- For n = 4, the lengths of longest increasing and decreasing subsequences of (2, 4, 5, 1, 3, 4.5) are 3 and 2, respectively.
There are 10 permutations of (1, 2, 3, 4, 5) that satisfy the conditions.
Sample Input 2
10 12 924844033
Sample Output 2
623378361
Print the count modulo M.