Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の単純無向グラフが与えられます。辺 i は頂点 u_i と頂点 v_i を結んでいます。
グラフに含まれる連結成分の個数を求めてください。
注釈
単純無向グラフ とは、単純で辺に向きの無いグラフのことをいいます。
グラフが 単純 であるとは、グラフが自己ループや多重辺を含まないことをいいます。
あるグラフの 部分グラフ とは、元のグラフのいくつかの頂点といくつかの辺を選んでできるグラフのことをいいます。
グラフが 連結 であるとは、グラフに含まれるすべての頂点同士が辺を経由して互いに行き来できることをいいます。
連結成分 とは、連結な部分グラフのうち、そのグラフを含んだより大きい連結な部分グラフが存在しないものをいいます。
制約
- 1 \leq N \leq 100
- 0 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq u_i, v_i \leq N
- 入力で与えられるグラフは単純
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
答えを出力せよ。
入力例 1
5 3 1 2 1 3 4 5
出力例 1
2
与えられるグラフに含まれる連結成分は次の 2 個です。
- 頂点 1, 2, 3 および辺 1, 2 からなる部分グラフ
- 頂点 4, 5 および辺 3 からなる部分グラフ

入力例 2
5 0
出力例 2
5
入力例 3
4 6 1 2 1 3 1 4 2 3 2 4 3 4
出力例 3
1
Score : 300 points
Problem Statement
You are given a simple undirected graph with N vertices numbered 1 to N and M edges numbered 1 to M. Edge i connects vertex u_i and vertex v_i.
Find the number of connected components in this graph.
Notes
A simple undirected graph is a graph that is simple and has undirected edges.
A graph is simple if and only if it has no self-loop or multi-edge.
A subgraph of a graph is a graph formed from some of the vertices and edges of that graph.
A graph is connected if and only if one can travel between every pair of vertices via edges.
A connected component is a connected subgraph that is not part of any larger connected subgraph.
Constraints
- 1 \leq N \leq 100
- 0 \leq M \leq \frac{N(N - 1)}{2}
- 1 \leq u_i, v_i \leq N
- The given graph is simple.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
Output
Print the answer.
Sample Input 1
5 3 1 2 1 3 4 5
Sample Output 1
2
The given graph contains the following two connected components:
- a subgraph formed from vertices 1, 2, 3, and edges 1, 2;
- a subgraph formed from vertices 4, 5, and edge 3.

Sample Input 2
5 0
Sample Output 2
5
Sample Input 3
4 6 1 2 1 3 1 4 2 3 2 4 3 4
Sample Output 3
1
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
配点 : 400 点
問題文
スーパーマンである高橋くんは、地上で困っている人を助けるため、あるビルの屋上から飛び降りようとしています。 高橋くんがいる星には重力の大きさを表す g という値が定まっており、 高橋くんが落下を開始してから地面に到達するまでにかかる時間は \frac{A}{\sqrt{g}} です。
現在の時刻は 0 であり、g = 1 が成り立ちます。 高橋くんは、今から次の操作を好きな回数(0 回でもよい)行います。
- 超能力により g の値を 1 増やす。時間が B 経過する。
その後、高橋くんはビルから飛び降ります。落下を開始した後は g の値を変えることはできません。 また、操作によって経過する時間と落下にかかる時間以外は考えないものとします。
高橋くんが地面に到達できる最も早い時刻を求めてください。
制約
- 1 \leq A \leq 10^{18}
- 1 \leq B \leq 10^{18}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
高橋くんが地面に到達できる最も早い時刻を出力せよ。 出力は、真の値との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。
入力例 1
10 1
出力例 1
7.7735026919
- 操作を 0 回行うとき、地面に到達する時刻は 1\times 0+\frac{10}{\sqrt{1}} = 10 です。
- 操作を 1 回行うとき、地面に到達する時刻は 1\times 1+\frac{10}{\sqrt{2}} \fallingdotseq 8.07 です。
- 操作を 2 回行うとき、地面に到達する時刻は 1\times 2+\frac{10}{\sqrt{3}} \fallingdotseq 7.77 です。
- 操作を 3 回行うとき、地面に到達する時刻は 1\times 3+\frac{10}{\sqrt{4}} = 8 です。
操作を 4 回以上行っても、地面への到達時刻は遅くなるのみです。 よって、操作を 2 回行ってから飛び降りるのが最適で、答えは 2+\frac{10}{\sqrt{3}} です。
入力例 2
5 10
出力例 2
5.0000000000
操作を 1 回も行わないのが最適です。
入力例 3
1000000000000000000 100
出力例 3
8772053214538.5976562500
Score : 400 points
Problem Statement
A superman, Takahashi, is about to jump off the roof of a building to help a person in trouble on the ground. Takahashi's planet has a constant value g that represents the strength of gravity, and the time it takes for him to reach the ground after starting to fall is \frac{A}{\sqrt{g}}.
It is now time 0, and g = 1. Takahashi will perform the following operation as many times as he wants (possibly zero).
- Use a superpower to increase the value of g by 1. This takes a time of B.
Then, he will jump off the building. After starting to fall, he cannot change the value of g. Additionally, we only consider the time it takes to perform the operation and fall.
Find the earliest time Takahashi can reach the ground.
Constraints
- 1 \leq A \leq 10^{18}
- 1 \leq B \leq 10^{18}
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
A B
Output
Print the earliest time Takahashi can reach the ground. Your output will be accepted when its absolute or relative error from the true value is at most 10^{-6}.
Sample Input 1
10 1
Sample Output 1
7.7735026919
- If he performs the operation zero times, he will reach the ground at time 1\times 0+\frac{10}{\sqrt{1}} = 10.
- If he performs the operation once, he will reach the ground at time 1\times 1+\frac{10}{\sqrt{2}} \fallingdotseq 8.07.
- If he performs the operation twice, he will reach the ground at time 1\times 2+\frac{10}{\sqrt{3}} \fallingdotseq 7.77.
- If he performs the operation three times, he will reach the ground at time 1\times 3+\frac{10}{\sqrt{4}} = 8.
Performing the operation four or more times will only delay the time to reach the ground. Therefore, it is optimal to perform the operation twice before jumping off, and the answer is 2+\frac{10}{\sqrt{3}}.
Sample Input 2
5 10
Sample Output 2
5.0000000000
It is optimal not to perform the operation at all.
Sample Input 3
1000000000000000000 100
Sample Output 3
8772053214538.5976562500
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
K, E, Y のみからなる文字列 S が与えられます。
S の隣接する 2 文字を入れ替える操作を K 回まで行えるとき、作ることができる文字列は何種類ありますか?
制約
- 2 \leq |S| \leq 30
- 0 \leq K \leq 10^9
- S は
K,E,Yのみからなる
入力
入力は以下の形式で標準入力から与えられる。
S K
出力
答えを出力せよ。
入力例 1
KEY 1
出力例 1
3
KEY に対して 1 回以下の操作を行うことで得られる文字列は KEY, EKY, KYE の 3 種類です。
入力例 2
KKEE 2
出力例 2
4
KKEE に対して 2 回以下の操作を行うことで得られる文字列は KKEE, KEKE, EKKE, KEEK の 4 種類です。
入力例 3
KKEEYY 1000000000
出力例 3
90
Score : 500 points
Problem Statement
Given is a string S consisting of K, E, Y.
How many strings are there that can be obtained with at most K swaps of two adjacent characters in S?
Constraints
- 2 \leq |S| \leq 30
- 0 \leq K \leq 10^9
- S consists of
K,E,Y.
Input
Input is given from Standard Input in the following format:
S K
Output
Print the answer.
Sample Input 1
KEY 1
Sample Output 1
3
With at most one swap, there are three strings that can be obtained: KEY, EKY, KYE.
Sample Input 2
KKEE 2
Sample Output 2
4
With at most two swaps, there are four strings that can be obtained: KKEE, KEKE, EKKE, KEEK.
Sample Input 3
KKEEYY 1000000000
Sample Output 3
90
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
M 個の数字 C_i が与えられます。
1 以上 N 以下の整数のうち、先頭に余分な 0 をつけずに 10 進法で表した時に C_1,\ldots,C_M を全て含むようなもの全ての和を、 998244353 で割った余りを求めてください。
制約
- 1 \leq N < 10^{10^4}
- 1 \leq M \leq 10
- 0 \leq C_1 < \ldots < C_M \leq 9
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M C_1 \ldots C_M
出力
答えを出力せよ。
入力例 1
104 2 0 1
出力例 1
520
1 以上 104 以下の整数のうち、10 進法で表した時に 0, 1 を共に含むようなものは、10,100,101,102,103,104 の 6 個あります。
これらの和は 520 です。
入力例 2
999 4 1 2 3 4
出力例 2
0
1 以上 999 以下の整数で、1, 2, 3, 4 を全て含むようなものは存在しません。
入力例 3
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 5 0 2 4 6 8
出力例 3
397365274
998244353 で割った余りを求めてください。
Score : 500 points
Problem Statement
Given are M digits C_i.
Find the sum, modulo 998244353, of all integers between 1 and N (inclusive) that contain all of C_1, \ldots, C_M when written in base 10 without unnecessary leading zeros.
Constraints
- 1 \leq N < 10^{10^4}
- 1 \leq M \leq 10
- 0 \leq C_1 < \ldots < C_M \leq 9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M C_1 \ldots C_M
Output
Print the answer.
Sample Input 1
104 2 0 1
Sample Output 1
520
Between 1 and 104, there are six integers that contain both 0 and 1 when written in base 10: 10,100,101,102,103,104.
The sum of them is 520.
Sample Input 2
999 4 1 2 3 4
Sample Output 2
0
Between 1 and 999, no integer contains all of 1, 2, 3, 4.
Sample Input 3
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 5 0 2 4 6 8
Sample Output 3
397365274
Be sure to find the sum modulo 998244353.