実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英小文字からなる文字列 S, T が与えられます。ここで、S と T の長さは等しいです。
X を空の配列とし、以下の操作を S と T が等しくなるまで繰り返します。
- S の文字を 1 文字書き換え、X の末尾に S を追加する。
こうして得られる文字列の配列 X のうち要素数最小のものを求めてください。要素数最小のものが複数考えられる場合は、そのうち辞書順最小のものを求めてください。
文字列の配列の辞書順とは
長さ N の文字列 S = S_1 S_2 \ldots S_N が長さ N の文字列 T = T_1 T_2 \ldots T_N より辞書順で小さいとは、ある整数 1 \leq i \leq N が存在して下記の 2 つがともに成り立つことをいいます。
- S_1 S_2 \ldots S_{i-1} = T_1 T_2 \ldots T_{i-1}
- S_i が T_i よりアルファベット順で早い。
要素数 M の文字列の配列 X = (X_1,X_2,\ldots,X_M) が要素数 M の文字列の配列 Y = (Y_1,Y_2,\ldots,Y_M) より辞書順で小さいとは、ある整数 1 \leq j \leq M が存在して下記の 2 つがともに成り立つことをいいます。
- (X_1,X_2,\ldots,X_{j-1}) = (Y_1,Y_2,\ldots,Y_{j-1})
- X_j が Y_j より辞書順で小さい。
制約
- S, T は英小文字からなる長さ 1 以上 100 以下の文字列
- S と T の長さは等しい
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
答えの要素数を M として M + 1 行出力せよ。
1 行目には M の値を出力せよ。
i + 1 (1 \leq i \leq M) 行目には答えの i 番目の要素を出力せよ。
入力例 1
adbe bcbc
出力例 1
3 acbe acbc bcbc
はじめ、S = adbe
です。
以下のように操作することで、X = ( acbe
, acbc
, bcbc
) とすることができます。
-
S を
acbe
に書き換え、X の末尾にacbe
を追加する。 -
S を
acbc
に書き換え、X の末尾にacbc
を追加する。 -
S を
bcbc
に書き換え、X の末尾にbcbc
を追加する。
入力例 2
abcde abcde
出力例 2
0
入力例 3
afwgebrw oarbrenq
出力例 3
8 aawgebrw aargebrw aarbebrw aarbebnw aarbebnq aarbeenq aarbrenq oarbrenq
Score : 300 points
Problem Statement
You are given two strings S and T consisting of lowercase English letters. Here, S and T have equal lengths.
Let X be an empty array, and repeat the following operation until S equals T:
- Change one character in S, and append S to the end of X.
Find the array of strings X with the minimum number of elements obtained in this way. If there are multiple such arrays with the minimum number of elements, find the lexicographically smallest one among them.
What is lexicographical order on arrays of strings?
A string S = S_1 S_2 \ldots S_N of length N is lexicographically smaller than a string T = T_1 T_2 \ldots T_N of length N if there exists an integer 1 \leq i \leq N such that both of the following are satisfied:
- S_1 S_2 \ldots S_{i-1} = T_1 T_2 \ldots T_{i-1}
- S_i comes earlier than T_i in alphabetical order.
An array of strings X = (X_1,X_2,\ldots,X_M) with M elements is lexicographically smaller than an array of strings Y = (Y_1,Y_2,\ldots,Y_M) with M elements if there exists an integer 1 \leq j \leq M such that both of the following are satisfied:
- (X_1,X_2,\ldots,X_{j-1}) = (Y_1,Y_2,\ldots,Y_{j-1})
- X_j is lexicographically smaller than Y_j.
Constraints
- S and T are strings consisting of lowercase English letters with length between 1 and 100, inclusive.
- The lengths of S and T are equal.
Input
The input is given from Standard Input in the following format:
S T
Output
Let M be the number of elements in the desired array. Print M + 1 lines.
The first line should contain the value of M.
The i + 1-th line (1 \leq i \leq M) should contain the i-th element of the array.
Sample Input 1
adbe bcbc
Sample Output 1
3 acbe acbc bcbc
Initially, S = adbe
.
We can obtain X = ( acbe
, acbc
, bcbc
) by performing the following operations:
-
Change S to
acbe
and appendacbe
to the end of X. -
Change S to
acbc
and appendacbc
to the end of X. -
Change S to
bcbc
and appendbcbc
to the end of X.
Sample Input 2
abcde abcde
Sample Output 2
0
Sample Input 3
afwgebrw oarbrenq
Sample Output 3
8 aawgebrw aargebrw aarbebrw aarbebnw aarbebnq aarbeenq aarbrenq oarbrenq
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 枚の靴下があります。i 枚目の靴下の色は A_i です。
あなたは以下の操作をできるだけ多い回数行いたいです。最大で何回行うことができますか?
- まだペアになっていない靴下の中から同じ色の靴下を 2 枚選んでペアにする。
制約
- 1\leq N \leq 5\times 10^5
- 1\leq A_i \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
6 4 1 7 4 1 4
出力例 1
2
以下のようにして、2 回の操作を行うことができます。
- 色が 1 である靴下を 2 枚選んでペアにする。
- 色が 4 である靴下を 2 枚選んでペアにする。
このとき、色が 4 である靴下と 7 である靴下が 1 枚ずつ残るため、これ以上操作はできません。 また、どのように操作をしても 3 回以上操作を行うことはできないため、2 を出力します。
入力例 2
1 158260522
出力例 2
0
入力例 3
10 295 2 29 295 29 2 29 295 2 29
出力例 3
4
Score : 300 points
Problem Statement
You have N socks. The color of the i-th sock is A_i.
You want to perform the following operation as many times as possible. How many times can it be performed at most?
- Choose two same-colored socks that are not paired yet, and pair them.
Constraints
- 1\leq N \leq 5\times 10^5
- 1\leq A_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print an integer representing the answer.
Sample Input 1
6 4 1 7 4 1 4
Sample Output 1
2
You can do the operation twice as follows.
- Choose two socks with the color 1 and pair them.
- Choose two socks with the color 4 and pair them.
Then, you will be left with one sock with the color 4 and another with the color 7, so you can no longer do the operation. There is no way to do the operation three or more times, so you should print 2.
Sample Input 2
1 158260522
Sample Output 2
0
Sample Input 3
10 295 2 29 295 29 2 29 295 2 29
Sample Output 3
4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
正整数 N,M が与えられます。
次の条件をともにみたす最小の正整数 X を求めてください。
ただし、そのようなものが存在しない場合は -1 を出力してください。
- X は 1 以上 N 以下の整数 a,b の積として表される。ここで、a と b は同じ整数でも良い。
- X は M 以上である。
制約
- 1\leq N\leq 10^{12}
- 1\leq M\leq 10^{12}
- N,M は整数
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
問題文の条件をともにみたす最小の正整数 X を出力せよ。そのようなものが存在しない場合は -1 を出力せよ。
入力例 1
5 7
出力例 1
8
まず、7 を 1 以上 5 以下の整数 2 つの積として表すことはできません。
次に、8 は 8=2\times 4 などとして、1 以上 5 以下の整数 2 つの積として表すことができます。
よって、8 を出力します。
入力例 2
2 5
出力例 2
-1
1\times 1=1、1\times 2=2、2\times 1=2、2\times 2=4 より、
1 以上 2 以下の整数 2 つの積として表す事ができるのは 1,2,4 のみであるため、
5 以上の数はそのような整数 2 つの積として表すことができません。
よって、-1 を出力します。
入力例 3
100000 10000000000
出力例 3
10000000000
a=b=100000 (=10^5)とした時、a,b の積は 10000000000 (=10^{10}) となり、これが答えとなります。
答えが 32 bit 整数型に収まらない場合があることに注意してください。
Score : 400 points
Problem Statement
You are given positive integers N and M.
Find the smallest positive integer X that satisfies both of the conditions below, or print -1 if there is no such integer.
- X can be represented as the product of two integers a and b between 1 and N, inclusive. Here, a and b may be the same.
- X is at least M.
Constraints
- 1\leq N\leq 10^{12}
- 1\leq M\leq 10^{12}
- N and M are integers.
Input
The input is given from Standard Input in the following format:
N M
Output
Print the smallest positive integer X that satisfies both of the conditions, or -1 if there is no such integer.
Sample Input 1
5 7
Sample Output 1
8
First, 7 cannot be represented as the product of two integers between 1 and 5.
Second, 8 can be represented as the product of two integers between 1 and 5, such as 8=2\times 4.
Thus, you should print 8.
Sample Input 2
2 5
Sample Output 2
-1
Since 1\times 1=1, 1\times 2=2, 2\times 1=2, and 2\times 2=4, only 1, 2, and 4 can be represented as the product of two integers between 1 and 2,
so no number greater than or equal to 5 can be represented as the product of two such integers.
Thus, you should print -1.
Sample Input 3
100000 10000000000
Sample Output 3
10000000000
For a=b=100000 (=10^5), the product of a and b is 10000000000 (=10^{10}), which is the answer.
Note that the answer may not fit into a 32-bit integer type.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
1, \dots, N と番号づけられた N 個の都市と、1, \dots, M と番号づけられた M 本の道があります。
全ての道は一方通行であり、道 i \, (1 \leq i \leq M) を通ると、都市 A_i から都市 B_i へ移動することができます。また、道 i の長さは C_i です。
1 以上 M 以下の整数からなる長さ K の数列 E = (E_1, \dots, E_K) が与えられます。都市 1 から都市 N までいくつかの道を使って移動する方法であって、以下の条件を満たすものを良い経路と呼びます。
- 通る道の番号を通った順番に並べた列は、E の部分列である。
なお、部分列とは、数列から 0 個以上の要素を削除し、残った要素を元の順序で並べて得られる数列のことを指します。
全ての良い経路における、通る道の長さの合計の最小値を求めてください。
ただし、良い経路が存在しない場合は、そのことを報告してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M, K \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N, A_i \neq B_i \, (1 \leq i \leq M)
- 1 \leq C_i \leq 10^9 \, (1 \leq i \leq M)
- 1 \leq E_i \leq M \, (1 \leq i \leq K)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K A_1 B_1 C_1 \vdots A_M B_M C_M E_1 \ldots E_K
出力
全ての良い経路における、通る道の長さの合計の最小値を出力せよ。ただし、良い経路が存在しないならば、-1 を出力せよ。
入力例 1
3 4 4 1 2 2 2 3 2 1 3 3 1 3 5 4 2 1 2
出力例 1
4
良い経路として考えられるのは次の二つです。
- 道 4 を使う。通る道の長さの合計は 5 である。
- 道 1, 2 をこの順で使う。通る道の長さの合計は 2 + 2 = 4 である。
よって、求める最小値は 4 です。
入力例 2
3 2 3 1 2 1 2 3 1 2 1 1
出力例 2
-1
良い経路は存在しません。
入力例 3
4 4 5 3 2 2 1 3 5 2 4 7 3 4 10 2 4 1 4 3
出力例 3
14
Score : 500 points
Problem Statement
There are N towns numbered 1, \dots, N, and M roads numbered 1, \dots, M.
Every road is directed; road i (1 \leq i \leq M) leads you from Town A_i to Town B_i. The length of road i is C_i.
You are given a sequence E = (E_1, \dots, E_K) of length K consisting of integers between 1 and M. A way of traveling from town 1 to town N using roads is called a good path if:
- the sequence of the roads' numbers arranged in the order used in the path is a subsequence of E.
Note that a subsequence of a sequence is a sequence obtained by removing 0 or more elements from the original sequence and concatenating the remaining elements without changing the order.
Find the minimum sum of the lengths of the roads used in a good path.
If there is no good path, report that fact.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M, K \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N, A_i \neq B_i \, (1 \leq i \leq M)
- 1 \leq C_i \leq 10^9 \, (1 \leq i \leq M)
- 1 \leq E_i \leq M \, (1 \leq i \leq K)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M K A_1 B_1 C_1 \vdots A_M B_M C_M E_1 \ldots E_K
Output
Find the minimum sum of the lengths of the roads used in a good path.
If there is no good path, print -1
.
Sample Input 1
3 4 4 1 2 2 2 3 2 1 3 3 1 3 5 4 2 1 2
Sample Output 1
4
There are two possible good paths as follows:
- Using road 4. In this case, the sum of the length of the used road is 5.
- Using road 1 and 2 in this order. In this case, the sum of the lengths of the used roads is 2 + 2 = 4.
Therefore, the desired minimum value is 4.
Sample Input 2
3 2 3 1 2 1 2 3 1 2 1 1
Sample Output 2
-1
There is no good path.
Sample Input 3
4 4 5 3 2 2 1 3 5 2 4 7 3 4 10 2 4 1 4 3
Sample Output 3
14
実行時間制限: 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