Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英小文字のみからなる長さ N の文字列 S が与えられます。
S の文字を並び替えて得られる文字列(S 自身を含む)であって、長さ K の回文を部分文字列として 含まない ものの個数を求めてください。
ただし、長さ N の文字列 T が「長さ K の回文を部分文字列として含む」とは、
ある (N-K) 以下の非負整数 i が存在して、1 以上 K 以下の任意の整数 j について T_{i+j}=T_{i+K+1-j} が成り立つことをいいます。
ここで、T_k は文字列 T の k 文字目を表すものとします。
制約
- 2\leq K \leq N \leq 10
- N,K は整数
- S は英小文字のみからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
S の文字を並び替えて得られる文字列であって、長さ K の回文を部分文字列として含まないものの個数を出力せよ。
入力例 1
3 2 aab
出力例 1
1
aab
を並び替えて得られる文字列は aab
, aba
, baa
の 3 つであり、このうち aab
および baa
は長さ 2 の回文 aa
を部分文字列として含んでいます。
よって、条件をみたす文字列は aba
のみであり、1 を出力します。
入力例 2
5 3 zzyyx
出力例 2
16
zzyyx
を並べて得られる文字列は 30 個ありますが、そのうち長さ 3 の回文を含まないようなものは 16 個です。よって、16 を出力します。
入力例 3
10 5 abcwxyzyxw
出力例 3
440640
Score : 300 points
Problem Statement
You are given a string S of length N consisting only of lowercase English letters.
Find the number of strings obtained by permuting the characters of S (including the string S itself) that do not contain a palindrome of length K as a substring.
Here, a string T of length N is said to "contain a palindrome of length K as a substring" if and only if there exists a non-negative integer i not greater than (N-K) such that T_{i+j} = T_{i+K+1-j} for every integer j with 1 \leq j \leq K.
Here, T_k denotes the k-th character of the string T.
Constraints
- 2 \leq K \leq N \leq 10
- N and K are integers.
- S is a string of length N consisting only of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N K S
Output
Print the number of strings obtained by permuting S that do not contain a palindrome of length K as a substring.
Sample Input 1
3 2 aab
Sample Output 1
1
The strings obtained by permuting aab
are aab
, aba
, and baa
. Among these, aab
and baa
contain the palindrome aa
of length 2 as a substring.
Thus, the only string that satisfies the condition is aba
, so print 1.
Sample Input 2
5 3 zzyyx
Sample Output 2
16
There are 30 strings obtained by permuting zzyyx
, 16 of which do not contain a palindrome of length 3. Thus, print 16.
Sample Input 3
10 5 abcwxyzyxw
Sample Output 3
440640
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 行 N 列のマス目があり、それぞれのマスは白または黒で塗られています。
マス目の状態は N 個の文字列 S_i で表され、
S_i の j 文字目が #
であることはマス目の上から i 行目、左から j 列目のマスが黒く塗られていることを、
.
であることは白く塗られていることをさします。
高橋君はこのマス目のうち高々 2 つの白く塗られているマスを選び、黒く塗ることができます。
マス目の中に、黒く塗られたマスが縦、横、ななめのいずれかの向きに 6 つ以上連続するようにできるか判定してください。
ただし、黒く塗られたマスがななめに 6 つ以上連続するとは、N 行 N 列のマス目に完全に含まれる 6 行 6 列のマス目であって、その少なくとも一方の対角線上のマスがすべて黒く塗られているようなものが存在する事をさします。
制約
- 6 \leq N \leq 1000
- \lvert S_i\rvert =N
- S_i は
#
と.
のみからなる。
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
高々 2 つのマス目を黒く塗ることで条件をみたすようにできるなら Yes
を、そうでないならば No
を出力せよ。
入力例 1
8 ........ ........ .#.##.#. ........ ........ ........ ........ ........
出力例 1
Yes
上から 3 行目の左から 3, 6 番目のマスを塗ることで横方向に 6 つの黒く塗られたマスを連続させることができます。
入力例 2
6 ###### ###### ###### ###### ###### ######
出力例 2
Yes
高橋君はマス目を新たに黒く塗ることはできませんが、すでにこのマス目は条件をみたしています。
入力例 3
10 .......... #..##..... .......... .......... ....#..... ....#..... .#...#..#. .......... .......... ..........
出力例 3
No
Score : 300 points
Problem Statement
There is a grid with N horizontal rows and N vertical columns, where each square is painted white or black.
The state of the grid is represented by N strings, S_i.
If the j-th character of S_i is #
, the square at the i-th row from the top and the j-th column from the left is painted black.
If the character is .
, then the square is painted white.
Takahashi can choose at most two of these squares that are painted white, and paint them black.
Determine if it is possible to make the grid contain 6 or more consecutive squares painted black aligned either vertically, horizontally, or diagonally.
Here, the grid is said to be containing 6 or more consecutive squares painted black aligned diagonally if the grid with N rows and N columns completely contains a subgrid with 6 rows and 6 columns such that all the squares on at least one of its diagonals are painted black.
Constraints
- 6 \leq N \leq 1000
- \lvert S_i\rvert =N
- S_i consists of
#
and.
.
Input
Input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
If it is possible to fulfill the condition by painting at most two squares, then print Yes
; otherwise, print No
.
Sample Input 1
8 ........ ........ .#.##.#. ........ ........ ........ ........ ........
Sample Output 1
Yes
By painting the 3-rd and the 6-th squares from the left in the 3-rd row from the top, there will be 6 squares painted black aligned horizontally.
Sample Input 2
6 ###### ###### ###### ###### ###### ######
Sample Output 2
Yes
While Takahashi cannot choose a square to paint black, the grid already satisfies the condition.
Sample Input 3
10 .......... #..##..... .......... .......... ....#..... ....#..... .#...#..#. .......... .......... ..........
Sample Output 3
No
Time Limit: 2.5 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
座標平面上に 1 から N の番号がついた N 人の人がいます。
人 1 は原点にいます。
次の形式の情報が M 個与えられます。
- 人 A_i から見て、人 B_i は、x 軸正方向に X_i、y 軸正方向に Y_i 離れた位置にいる
それぞれの人がいる座標を求めてください。一意に定まらないときはそのことを報告してください。
制約
- 1 \leq N \leq 2\times 10^5
- 0 \leq M \leq 2\times 10^5
- 1\leq A_i, B_i \leq N
- A_i \neq B_i
- -10^9 \leq X_i,Y_i \leq 10^9
- 入力は全て整数である
- 与えられる情報は矛盾しない
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 X_1 Y_1 \vdots A_M B_M X_M Y_M
出力
N 行出力せよ。
人 i のいる座標が一意に定まらないとき、i 行目には undecidable
と出力せよ。
人 i のいる座標が (s_i,t_i) と一意に定まるとき、i 行目には s_i,t_i をこの順に空白区切りで出力せよ。
入力例 1
3 2 1 2 2 1 1 3 -1 -2
出力例 1
0 0 2 1 -1 -2
3 人の位置関係は下図のようになっています。
入力例 2
3 2 2 1 -2 -1 2 3 -3 -3
出力例 2
0 0 2 1 -1 -2
3 人の位置関係は下図のようになっています。
入力例 3
5 7 1 2 0 0 1 2 0 0 2 3 0 0 3 1 0 0 2 1 0 0 3 2 0 0 4 5 0 0
出力例 3
0 0 0 0 0 0 undecidable undecidable
同じ情報が複数回与えられたり、同じ座標に複数の人がいることもあります。
Score : 400 points
Problem Statement
There are N people numbered 1 to N on a coordinate plane.
Person 1 is at the origin.
You are given M pieces of information in the following form:
- From person A_i's perspective, person B_i is X_i units away in the positive x-direction and Y_i units away in the positive y-direction.
Determine the coordinates of each person. If the coordinates of a person cannot be uniquely determined, report that fact.
Constraints
- 1 \leq N \leq 2\times 10^5
- 0 \leq M \leq 2\times 10^5
- 1\leq A_i, B_i \leq N
- A_i \neq B_i
- -10^9 \leq X_i,Y_i \leq 10^9
- All input values are integers.
- The given information is consistent.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 X_1 Y_1 \vdots A_M B_M X_M Y_M
Output
Print N lines.
If the coordinates of person i cannot be uniquely determined, the i-th line should contain undecidable
.
If they can be uniquely determined as (s_i,t_i), the i-th line should contain s_i and t_i in this order, separated by a space.
Sample Input 1
3 2 1 2 2 1 1 3 -1 -2
Sample Output 1
0 0 2 1 -1 -2
The figure below shows the positional relationship of the three people.
Sample Input 2
3 2 2 1 -2 -1 2 3 -3 -3
Sample Output 2
0 0 2 1 -1 -2
The figure below shows the positional relationship of the three people.
Sample Input 3
5 7 1 2 0 0 1 2 0 0 2 3 0 0 3 1 0 0 2 1 0 0 3 2 0 0 4 5 0 0
Sample Output 3
0 0 0 0 0 0 undecidable undecidable
The same piece of information may be given multiple times, and multiple people may be at the same coordinates.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
長さ N の数列 A = (A_1, A_2, \dots, A_N) および整数 K があります。
A をいくつかの連続部分列に分割する方法は 2^{N-1} 通りありますが、そのうち分割後に要素の和が K である列が存在しない分割の方法は何通りありますか。答えを 998244353 で割った余りを求めてください。
ここで、「A をいくつかの連続部分列に分割する」とは次の手順のことを言います。
- 分割後の数列の個数 k (1 \leq k \leq N) および 1 = i_1 \lt i_2 \lt \dots \lt i_k \lt i_{k+1} = N+1 を満たす整数列 (i_1, i_2, \dots, i_k, i_{k+1}) を自由に選ぶ。
- 1 \leq n \leq k について、n 番目の数列を、A の i_n 番目から i_{n+1} - 1 番目までの要素を順番を保ったまま取り出して出来る数列とする。
A = (1, 2, 3, 4, 5) のときの分割の例を以下に挙げます。
- (1, 2, 3), (4), (5)
- (1, 2), (3, 4, 5)
- (1, 2, 3, 4, 5)
制約
- 1 \leq N \leq 2 \times 10^5
- -10^{15} \leq K \leq 10^{15}
- -10^9 \leq A_i \leq 10^9
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
答えを 998244353 で割った余りを出力せよ。
入力例 1
3 3 1 2 3
出力例 1
2
問題文の条件を満たす分割は次の 2 通りです。
- (1), (2, 3)
- (1, 2, 3)
入力例 2
5 0 0 0 0 0 0
出力例 2
0
入力例 3
10 5 -5 -1 -7 6 -6 -2 -5 10 2 -10
出力例 3
428
Score : 475 points
Problem Statement
You are given a sequence A = (A_1, A_2, \dots, A_N) of length N and an integer K.
There are 2^{N-1} ways to divide A into several contiguous subsequences. How many of these divisions have no subsequence whose elements sum to K? Find the count modulo 998244353.
Here, "to divide A into several contiguous subsequences" means the following procedure.
- Freely choose the number k (1 \leq k \leq N) of subsequences and an integer sequence (i_1, i_2, \dots, i_k, i_{k+1}) satisfying 1 = i_1 \lt i_2 \lt \dots \lt i_k \lt i_{k+1} = N+1.
- For each 1 \leq n \leq k, the n-th subsequence is formed by taking the i_n-th through (i_{n+1} - 1)-th elements of A, maintaining their order.
Here are some examples of divisions for A = (1, 2, 3, 4, 5):
- (1, 2, 3), (4), (5)
- (1, 2), (3, 4, 5)
- (1, 2, 3, 4, 5)
Constraints
- 1 \leq N \leq 2 \times 10^5
- -10^{15} \leq K \leq 10^{15}
- -10^9 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print the count modulo 998244353.
Sample Input 1
3 3 1 2 3
Sample Output 1
2
There are two divisions that satisfy the condition in the problem statement:
- (1), (2, 3)
- (1, 2, 3)
Sample Input 2
5 0 0 0 0 0 0
Sample Output 2
0
Sample Input 3
10 5 -5 -1 -7 6 -6 -2 -5 10 2 -10
Sample Output 3
428
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
N 個の整数 A_1,A_2,\dots,A_N が与えられます。
以下の条件を満たす整数の組 (i,j,k) の個数を求めてください。
- 1 \le i,j,k \le N
- A_i \times A_j = A_k
制約
- 1 \le N \le 1000
- \color{red}{1 \le A_i < 10^{1000}}
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \vdots A_N
出力
答えを整数として出力せよ。
入力例 1
5 2 3 6 12 24
出力例 1
6
問題文中の条件を満たす (i,j,k) の組は以下の 6 通りです。
- (1,2,3)
- (1,3,4)
- (1,4,5)
- (2,1,3)
- (3,1,4)
- (4,1,5)
入力例 2
11 1 2 3 4 5 6 123456789123456789 123456789123456789 987654321987654321 987654321987654321 121932631356500531347203169112635269
出力例 2
40
各整数 A_i の値が非常に大きくなりうることに注意してください。
入力例 3
9 4 4 4 2 2 2 1 1 1
出力例 3
162
A_i の値に重複がありうることに注意してください。
Score: 550 points
Problem Statement
You are given N integers A_1, A_2, \dots, A_N.
Find the number of triples of integers (i, j, k) that satisfy the following conditions:
- 1 \le i, j, k \le N
- A_i \times A_j = A_k
Constraints
- 1 \le N \le 1000
- \color{red}{1 \le A_i < 10^{1000}}
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \vdots A_N
Output
Print the answer as an integer.
Sample Input 1
5 2 3 6 12 24
Sample Output 1
6
The following six triples (i, j, k) satisfy the conditions in the problem statement:
- (1, 2, 3)
- (1, 3, 4)
- (1, 4, 5)
- (2, 1, 3)
- (3, 1, 4)
- (4, 1, 5)
Sample Input 2
11 1 2 3 4 5 6 123456789123456789 123456789123456789 987654321987654321 987654321987654321 121932631356500531347203169112635269
Sample Output 2
40
Note that the values of each integer A_i can be huge.
Sample Input 3
9 4 4 4 2 2 2 1 1 1
Sample Output 3
162
Note that there may be duplicates among the values of A_i.