Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 300 点
問題文
文字列 S が与えられます。S を K 回繰り返してできる文字列を T とします。 T の文字をひとつ選んで他の文字に書き換える操作を繰り返すことで T のどの隣り合う 2 文字も相異なるようにするとき、 必要な操作の回数の最小値を求めてください。
制約
- 1 \leq |S| \leq 100
- S は英小文字からなる
- 1 \leq K \leq 10^9
- K は整数である
入力
入力は以下の形式で標準入力から与えられる。
S K
出力
必要な操作の回数の最小値を出力せよ。
入力例 1
issii 2
出力例 1
4
T は issiiissii
です。例えば、T を ispiqisyhi
に書き換えれば、どの隣り合う 2 文字も異なるようにできます。
入力例 2
qq 81
出力例 2
81
入力例 3
cooooooooonteeeeeeeeeest 999993333
出力例 3
8999939997
Score : 300 points
Problem Statement
Given is a string S. Let T be the concatenation of K copies of S. We can repeatedly perform the following operation: choose a character in T and replace it with a different character. Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.
Constraints
- 1 \leq |S| \leq 100
- S consists of lowercase English letters.
- 1 \leq K \leq 10^9
- K is an integer.
Input
Input is given from Standard Input in the following format:
S K
Output
Print the minimum number of operations required.
Sample Input 1
issii 2
Sample Output 1
4
T is issiiissii
. For example, we can rewrite it into ispiqisyhi
, and now any two adjacent characters are different.
Sample Input 2
qq 81
Sample Output 2
81
Sample Input 3
cooooooooonteeeeeeeeeest 999993333
Sample Output 3
8999939997
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 500 点
問題文
N 頂点 M 辺の連結無向グラフが与えられます。頂点には 1 から N までの番号がついています。
辺の情報はマス目 S を用いて表され、S_{i,j} が 1
のとき頂点 i,j を結ぶ辺が存在し、そうでないとき存在しないことを表します。
頂点全体を空でない集合 V_1,\dots,V_k に分解し、以下を満たすようにすることが可能か判定してください。 可能な場合、集合の個数 k の最大値を求めてください。
- どの辺も、番号が隣り合う頂点集合の頂点どうしを結ぶ。より正確には、どの辺 (i,j) に対しても、ある 1\leq t\leq k-1 が存在し、i\in V_t,j\in V_{t+1} または i\in V_{t+1},j\in V_t のいずれかを満たす。
制約
- 2 \leq N \leq 200
- S_{i,j} は
0
または1
である - S_{i,i} は
0
である - S_{i,j}=S_{j,i}
- 与えられるグラフは連結
- N は整数である
入力
入力は以下の形式で標準入力から与えられる。
N S_{1,1}...S_{1,N} : S_{N,1}...S_{N,N}
出力
条件を満たす分割が不可能な場合、-1 を出力せよ。 そうでない場合、集合の個数 k の最大値を出力せよ。
入力例 1
2 01 10
出力例 1
2
頂点 1,2 をそれぞれ V_1,V_2 に含めればよいです。
入力例 2
3 011 101 110
出力例 2
-1
入力例 3
6 010110 101001 010100 101000 100000 010000
出力例 3
4
Score : 500 points
Problem Statement
Given is a connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the edges are described by a grid of characters S.
If S_{i,j} is 1
, there is an edge connecting Vertex i and j; otherwise, there is no such edge.
Determine whether it is possible to divide the vertices into non-empty sets V_1, \dots, V_k such that the following condition is satisfied. If the answer is yes, find the maximum possible number of sets, k, in such a division.
- Every edge connects two vertices belonging to two "adjacent" sets. More formally, for every edge (i,j), there exists 1\leq t\leq k-1 such that i\in V_t,j\in V_{t+1} or i\in V_{t+1},j\in V_t holds.
Constraints
- 2 \leq N \leq 200
- S_{i,j} is
0
or1
. - S_{i,i} is
0
. - S_{i,j}=S_{j,i}
- The given graph is connected.
- N is an integer.
Input
Input is given from Standard Input in the following format:
N S_{1,1}...S_{1,N} : S_{N,1}...S_{N,N}
Output
If it is impossible to divide the vertices into sets so that the condition is satisfied, print -1. Otherwise, print the maximum possible number of sets, k, in a division that satisfies the condition.
Sample Input 1
2 01 10
Sample Output 1
2
We can put Vertex 1 in V_1 and Vertex 2 in V_2.
Sample Input 2
3 011 101 110
Sample Output 2
-1
Sample Input 3
6 010110 101001 010100 101000 100000 010000
Sample Output 3
4
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 800 点
問題文
整数 N, X が与えられます。0 以上 X 以下のすべての整数 k に対し、 k に以下の操作を繰り返すことによって次に k に戻るまでの操作回数 (戻らない場合 0) を足し合わせた値を 998244353 で割ったあまりを求めてください。
- 現在の整数が奇数なら、1 を引いて 2 で割る。そうでなければ、2 で割って 2^{N-1} を足す。
制約
- 1 \leq N \leq 2\times 10^5
- 0 \leq X < 2^N
- X は 2 進表記でちょうど N 桁与えられる (X が N 桁より少ない場合、leading zero をつけて与えられる。)
- 入力はすべて整数である
入力
入力は以下の形式で標準入力から与えられる。
N X
出力
0 以上 X 以下のすべての整数に対し、 操作を繰り返すことによってもとの整数に戻るまでの操作回数 (戻らない場合 0) を足し合わせた値を 998244353 で割ったあまりを出力せよ。
入力例 1
3 111
出力例 1
40
例えば、k=3 のとき、操作によって整数は 1,0,4,6,7,3 と順に変化します。したがって、k=3 のときの操作回数は 6 です。
入力例 2
6 110101
出力例 2
616
入力例 3
30 001110011011011101010111011100
出力例 3
549320998
Score : 800 points
Problem Statement
Given are integers N and X. For each integer k between 0 and X (inclusive), find the answer to the following question, then compute the sum of all those answers, modulo 998244353.
- Let us repeat the following operation on the integer k. Operation: if the integer is currently odd, subtract 1 from it and divide it by 2; otherwise, divide it by 2 and add 2^{N-1} to it. How many operations need to be performed until k returns to its original value? (The answer is considered to be 0 if k never returns to its original value.)
Constraints
- 1 \leq N \leq 2\times 10^5
- 0 \leq X < 2^N
- X is given in binary and has exactly N digits. (In case X has less than N digits, it is given with leading zeroes.)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X
Output
Print the sum of the answers to the questions for the integers between 0 and X (inclusive), modulo 998244353.
Sample Input 1
3 111
Sample Output 1
40
For example, for k=3, the operation changes k as follows: 1,0,4,6,7,3. Therefore the answer for k=3 is 6.
Sample Input 2
6 110101
Sample Output 2
616
Sample Input 3
30 001110011011011101010111011100
Sample Output 3
549320998
Time Limit: 4 sec / Memory Limit: 1024 MB
配点 : 1000 点
問題文
xy 平面上の点 (0,0) を中心とする円周上に N 個の点が与えられます。 i 個目の点の座標は (\cos(\frac{2\pi T_i}{L}),\sin(\frac{2\pi T_i}{L})) です。
これら N 個の点の中から相異なる 3 点を一様ランダムに選ぶとき、 選んだ 3 点を結んでできる三角形の内接円の中心の x 座標、y 座標の期待値をそれぞれ求めてください。
制約
- 3 \leq N \leq 3000
- N \leq L \leq 10^9
- 0 \leq T_i \leq L-1
- T_i<T_{i+1}
- 入力はすべて整数である
入力
入力は以下の形式で標準入力から与えられる。
N L T_1 : T_N
出力
選んだ 3 点を結んでできる三角形の内接円の中心の x 座標、y 座標の期待値をそれぞれ出力せよ。 絶対誤差あるいは相対誤差が 10^{-9} 以下のとき正答と判定される。
入力例 1
3 4 0 1 3
出力例 1
0.414213562373095 -0.000000000000000
3 点の座標は (1,0), (0,1), (0,-1) であり、この 3 点を結んでできる三角形の内接円の中心の座標は (\sqrt{2}-1,0) です。
入力例 2
4 8 1 3 5 6
出力例 2
-0.229401949926902 -0.153281482438188
入力例 3
10 100 2 11 35 42 54 69 89 91 93 99
出力例 3
0.352886583546338 -0.109065017701873
Score : 1000 points
Problem Statement
Given are N points on the circumference of a circle centered at (0,0) in an xy-plane. The coordinates of the i-th point are (\cos(\frac{2\pi T_i}{L}),\sin(\frac{2\pi T_i}{L})).
Three distinct points will be chosen uniformly at random from these N points. Find the expected x- and y-coordinates of the center of the circle inscribed in the triangle formed by the chosen points.
Constraints
- 3 \leq N \leq 3000
- N \leq L \leq 10^9
- 0 \leq T_i \leq L-1
- T_i<T_{i+1}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N L T_1 : T_N
Output
Print the expected x- and y-coordinates of the center of the circle inscribed in the triangle formed by the chosen points. Your output will be considered correct when the absolute or relative error is at most 10^{-9}.
Sample Input 1
3 4 0 1 3
Sample Output 1
0.414213562373095 -0.000000000000000
The three points have the coordinates (1,0), (0,1), and (0,-1). The center of the circle inscribed in the triangle formed by these points is (\sqrt{2}-1,0).
Sample Input 2
4 8 1 3 5 6
Sample Output 2
-0.229401949926902 -0.153281482438188
Sample Input 3
10 100 2 11 35 42 54 69 89 91 93 99
Sample Output 3
0.352886583546338 -0.109065017701873
Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 1500 点
問題文
円周上の一般の位置に相異なる 2N 個の点が並んでおり、反時計回りに順に 1,\dots,2N の番号が付けられています。 ただし、ここでいう一般の位置とは、どの相異なる 6 点 U, V, W, X, Y, Z についても、線分 UV, WX, YZ が一点で交わらないことをいいます。 また、 2N \times 2N の行列 A が与えられます。
円周上の 2N 個の点を N 個のペアに分ける方法であって、以下の条件をみたすようなものの個数を求めてください。
- すべてのペアに対してそのペアの 2 つの点を結ぶ赤い線分をひいたとき、赤い部分が "木" になっている。
- すべてのペアについて、その端点を点 P, Q としたとき、 A_{P,Q} = A_{Q,P} = 1 である。
より厳密には、赤い部分が "木" になっているとは、赤い部分全体が連結かつ無閉路になっていることを指します。
例えば、下図において、
- 左上の例は条件を満たします。
- 右上の例は、赤い部分に閉路が存在し、条件を満たしません。
- 左下の例は、赤い部分が非連結であり、条件を満たしません。
- 右下の例は、ペアに属さない頂点やペアに複数回含まれる頂点が存在し、条件を満たしません。
図: 条件を満たす例 (左上) とそうでない例 (それ以外)
ノート
答えは、2N 個の点が一般の位置にある限り、その具体的な位置関係には依存しないことが証明できます。
制約
- 1 \leq N \leq 20
- A_{i,j} は
0
または1
である - A_{i,i} は
0
である - A_{i,j}=A_{j,i}
- N は整数である
入力
入力は以下の形式で標準入力から与えられる。
N A_{1,1}...A_{1,2N} : A_{2N,1}...A_{2N,2N}
出力
円周上の 2N 個の点を N 個のペアに分ける方法であって、条件をみたすようなものの個数を出力せよ。 この制約下で、答えが 64 ビット符号付整数の範囲内に収まることが証明できる。
入力例 1
3 011111 101111 110111 111011 111101 111110
出力例 1
3
((1,4),(2,6),(3,5)), ((1,3),(2,5),(4,6)), ((1,5),(2,4),(3,6)) の 3 つの分け方が条件を満たします。
入力例 2
4 01111100 10011111 10011100 11101111 11110111 11111011 01011101 01011110
出力例 2
6
入力例 3
8 0111101111111111 1011101111111111 1101101111011101 1110111111111111 1111011111110111 0001101111111111 1111110111011111 1111111011111111 1111111101111111 1111111110111111 1101110111011111 1111111111101111 1111011111110111 1111111111111011 1101111111111101 1111111111111110
出力例 3
4762
Score : 1500 points
Problem Statement
There are 2N points generally positioned on the circumference of a circle, numbered 1,\dots,2N in counterclockwise order. Here, a set of points is said to be generally positioned if, for any six distinct points U, V, W, X, Y, and Z among them, the segments UV, WX, and YZ do not intersect at the same point. Additionally, you will be given a 2N\times 2N matrix A.
Find the number of ways to divide the 2N points into N pairs such that all of the following are satisfied:
- Let us draw a red segment connecting the two points for each pair. Then, those red segments form a tree.
- For each pair (P, Q), A_{P,Q} = A_{Q,P} = 1 holds.
Here, a set of segments is said to form a tree if they are all connected and form no cycles.
For example, see the figure below:
- Upper left: the conditions are satisfied.
- Upper right: the red segments form a cycle, so the conditions are not satisfied.
- Lower left: the red segments are not connected, so the conditions are not satisfied.
- Lower right: some vertices belong to no pair or multiple pairs, so the conditions are not satisfied.
Figure: A division satisfying the conditions (upper left) and divisions violating them (the others)
Notes
It can be proved that, as long as the 2N points are generally positioned, the answer does not depend on their specific positions.
Constraints
- 1 \leq N \leq 20
- A_{i,j} is
0
or1
. - A_{i,i} is
0
. - A_{i,j}=A_{j,i}
- N is an integer.
Input
Input is given from Standard Input in the following format:
N A_{1,1}...A_{1,2N} : A_{2N,1}...A_{2N,2N}
Output
Print the number of ways to divide the 2N points into N pairs such that all of the conditions are satisfied. It can be proved that the answer fits into a 64-bit signed integer under the given constraints.
Sample Input 1
3 011111 101111 110111 111011 111101 111110
Sample Output 1
3
There are three possible divisions that satisfy the conditions: ((1,4),(2,6),(3,5)), ((1,3),(2,5),(4,6)), and ((1,5),(2,4),(3,6)).
Sample Input 2
4 01111100 10011111 10011100 11101111 11110111 11111011 01011101 01011110
Sample Output 2
6
Sample Input 3
8 0111101111111111 1011101111111111 1101101111011101 1110111111111111 1111011111110111 0001101111111111 1111110111011111 1111111011111111 1111111101111111 1111111110111111 1101110111011111 1111111111101111 1111011111110111 1111111111111011 1101111111111101 1111111111111110
Sample Output 3
4762
Time Limit: 6 sec / Memory Limit: 1024 MB
配点 : 2000 点
問題文
N 行 M 列のマス目の全てのマスに 1 以上 K 以下の整数を書き込む方法 K^{NM} 通りすべてに対して以下の値を求め、 それらすべての総和を D で割ったあまりを求めてください。
- NM 個の各マスに対し、それと同じ行あるいは同じ列のマス (自分自身を含む) に書かれた整数の最小値を求め、それら NM 個すべての積を取って得られる値
制約
- 1 \leq N,M,K \leq 100
- 10^8 \leq D \leq 10^9
- N,M,K,D は整数である
- D は素数である
入力
入力は以下の形式で標準入力から与えられる。
N M K D
出力
K^{NM} 個の値の総和を D で割ったあまりを出力せよ。
入力例 1
2 2 2 998244353
出力例 1
35
NM 個の値の積が 16 になる書き込み方が 1 通り、2 になる書き込み方が 4 通り、1 になる書き込み方が 11 通りあります。
入力例 2
2 3 4 998244353
出力例 2
127090
入力例 3
31 41 59 998244353
出力例 3
827794103
Score : 2000 points
Problem Statement
For each of the K^{NM} ways to write an integer between 1 and K (inclusive) in every square in a square grid with N rows and M columns, find the value defined below, then compute the sum of all those K^{NM} values, modulo D.
- For each of the NM squares, find the minimum among the N+M-1 integers written in the square's row or the square's column. The value defined for the grid is the product of all these NM values.
Constraints
- 1 \leq N,M,K \leq 100
- 10^8 \leq D \leq 10^9
- N,M,K, and D are integers.
- D is prime.
Input
Input is given from Standard Input in the following format:
N M K D
Output
Print the sum of the K^{NM} values, modulo D.
Sample Input 1
2 2 2 998244353
Sample Output 1
35
We have 1 way to write integers such that the product of the NM values is 16, 4 ways such that the product is 2, and 11 ways such that the product is 1.
Sample Input 2
2 3 4 998244353
Sample Output 2
127090
Sample Input 3
31 41 59 998244353
Sample Output 3
827794103