実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 匹の鳩がおり、 1 から N までの番号がつけられています。また、 N 個の巣があり、 1 から N までの番号がつけられています。はじめ、鳩 i は巣 i にいます (1\leq i\leq N)。
Q 個のクエリが与えられるので順に処理してください。 クエリは 2 種類あり、以下のいずれかの形式で与えられます。
1 P H: 鳩 P を巣 H に移動させる。2: 複数の鳩がいる巣の個数を出力する。
制約
- 2\leq N\leq 10^6
- 1\leq Q\leq 3\times 10^5
- 1\leq P,H\leq N
- 1 つ目の形式のクエリについて、鳩 P は移動する前に巣 H にいない
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の 2 種類のいずれかの形式で与えられる。
1 P H
2
出力
問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。
入力例 1
4 7 2 1 1 2 2 1 3 2 2 1 3 4 2
出力例 1
0 1 1 2
初め、鳩 1,2,3,4 はそれぞれ巣 1,2,3,4 にいます。
- 1 つ目のクエリについて、巣 1,2,3,4 にはそれぞれ 1,1,1,1 匹います。鳩が複数いる巣は存在しないので 0 を出力します。
- 2 つ目のクエリについて、鳩 1 を巣 2 に移動します。
- 3 つ目のクエリについて、巣 1,2,3,4 にはそれぞれ 0,2,1,1 匹います。鳩が複数いる巣は巣 2 の 1 つなので 1 を出力します。
- 4 つ目のクエリについて、鳩 3 を巣 2 に移動します。
- 5 つ目のクエリについて、巣 1,2,3,4 にはそれぞれ 0,3,0,1 匹います。鳩が複数いる巣は巣 2 の 1 つなので 1 を出力します。
- 6 つ目のクエリについて、鳩 3 を巣 4 に移動します。
- 7 つ目のクエリについて、巣 1,2,3,4 にはそれぞれ 0,2,0,2 匹います。鳩が複数いる巣は巣 2,4 の 2 つなので 2 を出力します。
入力例 2
5 10 2 1 4 3 1 4 5 2 1 3 1 2 1 2 3 1 2 5 1 1 3 2
出力例 2
0 1 2 1
Score : 300 points
Problem Statement
There are N pigeons numbered from 1 to N, and there are N nests numbered from 1 to N. Initially, pigeon i is in nest i for 1\leq i\leq N.
You are given Q queries, which you must process in order. There are two types of queries, each given in one of the following formats:
1 P H: Move pigeon P to nest H.2: Output the number of nests that contain more than one pigeon.
Constraints
- 2 \leq N \leq 10^6
- 1 \leq Q \leq 3\times 10^5
- 1 \leq P, H \leq N
- For a query of the first type, pigeon P is not in nest H before the move.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in one of the following two formats:
1 P H
2
Output
Print the answer to each query on a new line according to the instructions in the problem statement.
Sample Input 1
4 7 2 1 1 2 2 1 3 2 2 1 3 4 2
Sample Output 1
0 1 1 2
Initially, pigeons 1,2,3,4 are in nests 1,2,3,4, respectively.
- For the 1st query, the counts of pigeons in nests 1,2,3,4 are 1,1,1,1. No nests contain multiple pigeons, output 0.
- For the 2nd query, move pigeon 1 to nest 2.
- For the 3rd query, the counts become 0,2,1,1, respectively. One nest (nest 2) contains multiple pigeons, so output 1.
- For the 4th query, move pigeon 3 to nest 2.
- For the 5th query, the counts become 0,3,0,1, respectively. One nest (nest 2) contains multiple pigeons, so output 1.
- For the 6th query, move pigeon 3 to nest 4.
- For the 7th query, the counts become 0,2,0,2, respectively. Two nests (nests 2 and 4) contain multiple pigeons, so output 2.
Sample Input 2
5 10 2 1 4 3 1 4 5 2 1 3 1 2 1 2 3 1 2 5 1 1 3 2
Sample Output 2
0 1 2 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
二次元平面があります。1 以上 9 以下の整数 r,c について、S_{r} の c 番目の文字が # であるとき座標 (r,c) にポーンが置いてあり、S_{r} の c 番目の文字が . であるとき座標 (r,c) に何も置かれていません。
この平面上の正方形であって、4 頂点全てにポーンが置いてあるものの個数を求めてください。
制約
- S_1,\ldots,S_9 はそれぞれ
#と.からなる長さ 9 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S_1 S_2 \vdots S_9
出力
答えを出力せよ。
入力例 1
##....... ##....... ......... .......#. .....#... ........# ......#.. ......... .........
出力例 1
2
座標 (1,1),(1,2),(2,2),(2,1) を頂点とする正方形は、4 頂点全てにポーンが置かれています。
座標 (4,8),(5,6),(7,7),(6,9) を頂点とする正方形も、4 頂点全てにポーンが置かれています。
よって答えは 2 です。
入力例 2
.#....... #.#...... .#....... ......... ....#.#.# ......... ....#.#.# ........# .........
出力例 2
3
Score : 300 points
Problem Statement
There is a two-dimensional plane. For integers r and c between 1 and 9, there is a pawn at the coordinates (r,c) if the c-th character of S_{r} is #, and nothing if the c-th character of S_{r} is ..
Find the number of squares in this plane with pawns placed at all four vertices.
Constraints
- Each of S_1,\ldots,S_9 is a string of length 9 consisting of
#and..
Input
The input is given from Standard Input in the following format:
S_1 S_2 \vdots S_9
Output
Print the answer.
Sample Input 1
##....... ##....... ......... .......#. .....#... ........# ......#.. ......... .........
Sample Output 1
2
The square with vertices (1,1), (1,2), (2,2), and (2,1) have pawns placed at all four vertices.
The square with vertices (4,8), (5,6), (7,7), and (6,9) also have pawns placed at all four vertices.
Thus, the answer is 2.
Sample Input 2
.#....... #.#...... .#....... ......... ....#.#.# ......... ....#.#.# ........# .........
Sample Output 2
3
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋くんはレストランで、 N 品からなる奇妙なフルコースを楽しむことにしました。
このコースのうち i 番目の料理は以下の通りです。
- X_i=0 の場合、美味しさが Y_i の 解毒剤入り の料理
- X_i=1 の場合、美味しさが Y_i の 毒入り の料理
高橋くんが料理を食べると、高橋くんの状態は以下のように変化します。
- 最初、高橋くんはお腹を壊していない。
- 高橋くんが お腹を壊していない 時、
- 解毒剤入り の料理を食べても、高橋くんは お腹を壊していないまま である。
- 毒入り の料理を食べると、高橋くんは お腹を壊す 。
- 高橋くんが お腹を壊している 時、
- 解毒剤入り の料理を食べると、高橋くんは お腹を壊していない状態になる 。
- 毒入り の料理を食べると、高橋くんは 死ぬ 。
コースは以下の流れで進行します。
- i = 1, \ldots, N についてこの順に、以下の処理を繰り返す。
- まず、 i 番目の料理が高橋くんに提供される。
- 次に、 高橋くんはこの料理に対し「食べる」か「下げてもらう」かを選択する。
- 「食べる」を選択した場合、高橋くんは i 番目の料理を食べる。食べた料理に応じて高橋くんの状態も変化する。
- 「下げてもらう」を選択した場合、高橋くんは i 番目の料理を食べない。この料理を後で提供してもらったり何らかの手段で保存したりすることはできない。
- 最後に、 (状態が変化するなら変化後の時点で) 高橋くんが死んでいない場合、
- i \neq N なら次の料理に進む。
- i = N なら高橋くんは生きて退店する。
高橋くんはこのあと重要な仕事があるため、高橋くんは生きて退店しなければなりません。
この条件の下で高橋くんが各料理に対し「食べる」「下げてもらう」を選択したとき、高橋くんが 食べた料理の美味しさの総和として考えられる最大値 ( 但し、何も食べなかった場合は 0 ) を出力してください。
制約
- 入力は全て整数
- 1 \le N \le 3 \times 10^5
- X_i \in \{0,1\}
- つまり、 X_i は 0,1 のどちらかである。
- -10^9 \le Y_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
出力
答えを整数として出力せよ。
入力例 1
5 1 100 1 300 0 -200 1 500 1 300
出力例 1
600
以下のように選択することで食べた料理の美味しさの総和を 600 にでき、これが考えられる最大値です。
- 1 番目の料理を下げてもらう。高橋くんはお腹を壊していません。
- 2 番目の料理を食べる。高橋くんはお腹を壊し、食べた料理の美味しさの総和は 300 となります。
- 3 番目の料理を食べる。高橋くんはお腹を壊していない状態に戻り、食べた料理の美味しさの総和は 100 となります。
- 4 番目の料理を食べる。高橋くんはお腹を壊し、食べた料理の美味しさの総和は 600 となります。
- 5 番目の料理を下げてもらう。高橋くんはお腹を壊しています。
- 最終的に高橋くんは死んでいないので、高橋くんは生きて退店する。
入力例 2
4 0 -1 1 -2 0 -3 1 -4
出力例 2
0
この入力の場合何も食べないことが最善ですが、この場合答えは 0 となります。
入力例 3
15 1 900000000 0 600000000 1 -300000000 0 -700000000 1 200000000 1 300000000 0 -600000000 1 -900000000 1 600000000 1 -100000000 1 -400000000 0 900000000 0 200000000 1 -500000000 1 900000000
出力例 3
4100000000
答えが 32 bit 符号付き整数に収まらない可能性があります。
Score : 400 points
Problem Statement
Takahashi has decided to enjoy a wired full-course meal consisting of N courses in a restaurant.
The i-th course is:
- if X_i=0, an antidotal course with a tastiness of Y_i;
- if X_i=1, a poisonous course with a tastiness of Y_i.
When Takahashi eats a course, his state changes as follows:
- Initially, Takahashi has a healthy stomach.
- When he has a healthy stomach,
- if he eats an antidotal course, his stomach remains healthy;
- if he eats a poisonous course, he gets an upset stomach.
- When he has an upset stomach,
- if he eats an antidotal course, his stomach becomes healthy;
- if he eats a poisonous course, he dies.
The meal progresses as follows.
- Repeat the following process for i = 1, \ldots, N in this order.
- First, the i-th course is served to Takahashi.
- Next, he chooses whether to "eat" or "skip" the course.
- If he chooses to "eat" it, he eats the i-th course. His state also changes depending on the course he eats.
- If he chooses to "skip" it, he does not eat the i-th course. This course cannot be served later or kept somehow.
- Finally, (if his state changes, after the change) if he is not dead,
- if i \neq N, he proceeds to the next course.
- if i = N, he makes it out of the restaurant alive.
An important meeting awaits him, so he must make it out of there alive.
Find the maximum possible sum of tastiness of the courses that he eats (or 0 if he eats nothing) when he decides whether to "eat" or "skip" the courses under that condition.
Constraints
- All input values are integers.
- 1 \le N \le 3 \times 10^5
- X_i \in \{0,1\}
- In other words, X_i is either 0 or 1.
- -10^9 \le Y_i \le 10^9
Input
The input is given from Standard Input in the following format:
N X_1 Y_1 X_2 Y_2 \vdots X_N Y_N
Output
Print the answer as an integer.
Sample Input 1
5 1 100 1 300 0 -200 1 500 1 300
Sample Output 1
600
The following choices result in a total tastiness of the courses that he eats amounting to 600, which is the maximum possible.
- He skips the 1-st course. He now has a healthy stomach.
- He eats the 2-nd course. He now has an upset stomach, and the total tastiness of the courses that he eats amounts to 300.
- He eats the 3-rd course. He now has a healthy stomach again, and the total tastiness of the courses that he eats amounts to 100.
- He eats the 4-th course. He now has an upset stomach, and the total tastiness of the courses that he eats amounts to 600.
- He skips the 5-th course. He now has an upset stomach.
- In the end, he is not dead, so he makes it out of the restaurant alive.
Sample Input 2
4 0 -1 1 -2 0 -3 1 -4
Sample Output 2
0
For this input, it is optimal to eat nothing, in which case the answer is 0.
Sample Input 3
15 1 900000000 0 600000000 1 -300000000 0 -700000000 1 200000000 1 300000000 0 -600000000 1 -900000000 1 600000000 1 -100000000 1 -400000000 0 900000000 0 200000000 1 -500000000 1 900000000
Sample Output 3
4100000000
The answer may not fit into a 32-bit integer type.
実行時間制限: 2 sec / メモリ制限: 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
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
高橋君はくじを引こうとしています。
くじを 1 回引くごとに、N 種類の賞品のいずれかが手に入ります。賞品 i が手に入る確率は \frac{W_i}{\sum_{j=1}^{N}W_j} であり、各くじの結果は独立です。
くじを K 回引いたとき、ちょうど M 種類の賞品が手に入る確率はいくらでしょうか? \bmod 998244353 で求めてください。
注記
有理数を出力する際は、まずその有理数を分数 \frac{y}{x} として表してください。 ここで、x,y は整数であり、x は 998244353 で割り切れてはなりません(この問題の制約下で、そのような表現は必ず可能です)。 そして、xz\equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の唯一の整数 z を出力してください。
制約
- 1 \leq K \leq 50
- 1 \leq M \leq N \leq 50
- 0 < W_i
- 0 < W_1 + \ldots + W_N < 998244353
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M K W_1 \vdots W_N
出力
答えを出力せよ。
入力例 1
2 1 2 2 1
出力例 1
221832079
各くじの結果として、賞品 1 が手に入る確率が \frac{2}{3}、賞品 2 が手に入る確率が \frac{1}{3} です。
2 回のくじの結果として、ともに賞品 1 を手に入れる確率が \frac{4}{9}、ともに賞品 2 を手に入れる確率が \frac{1}{9} であるため、求める答えは \frac{5}{9} です。
これを注記にしたがって \bmod 998244353 で出力すると 221832079 になります。
入力例 2
3 3 2 1 1 1
出力例 2
0
くじを 2 回引いて 3 種類の賞品を手に入れることはできません。したがって求める確率は 0 です。
入力例 3
3 3 10 499122176 499122175 1
出力例 3
335346748
入力例 4
10 8 15 1 1 1 1 1 1 1 1 1 1
出力例 4
755239064
Score : 500 points
Problem Statement
Takahashi is participating in a lottery.
Each time he takes a draw, he gets one of the N prizes available. Prize i is awarded with probability \frac{W_i}{\sum_{j=1}^{N}W_j}. The results of the draws are independent of each other.
What is the probability that he gets exactly M different prizes from K draws? Find it modulo 998244353.
Note
To print a rational number, start by representing it as a fraction \frac{y}{x}. Here, x and y should be integers, and x should not be divisible by 998244353 (under the Constraints of this problem, such a representation is always possible). Then, print the only integer z between 0 and 998244352 (inclusive) such that xz\equiv y \pmod{998244353}.
Constraints
- 1 \leq K \leq 50
- 1 \leq M \leq N \leq 50
- 0 < W_i
- 0 < W_1 + \ldots + W_N < 998244353
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M K W_1 \vdots W_N
Output
Print the answer.
Sample Input 1
2 1 2 2 1
Sample Output 1
221832079
Each draw awards Prize 1 with probability \frac{2}{3} and Prize 2 with probability \frac{1}{3}.
He gets Prize 1 at both of the two draws with probability \frac{4}{9}, and Prize 2 at both draws with probability \frac{1}{9}, so the sought probability is \frac{5}{9}.
The modulo 998244353 representation of this value, according to Note, is 221832079.
Sample Input 2
3 3 2 1 1 1
Sample Output 2
0
It is impossible to get three different prizes from two draws, so the sought probability is 0.
Sample Input 3
3 3 10 499122176 499122175 1
Sample Output 3
335346748
Sample Input 4
10 8 15 1 1 1 1 1 1 1 1 1 1
Sample Output 4
755239064