Codeforces Global Round 7
A. Bad Ugly Numbers
You are given a integer . Find any integer which satisfies these conditions, or report that there are no such numbers:
In the decimal representation of :
- ,
- consists of digits,
- no digit in equals ,
- is not divisible by any of it’s digits.
Input
The input consists of multiple test cases. The first line of the input contains a single integer , the number of test cases. The next lines each describe a test case.
Each test case contains one positive integer .
It is guaranteed that the sum of for all test cases does not exceed .
Output
For each test case, print an integer which satisfies the conditions described above, or “-1” (without quotes), if no such number exists. If there are multiple possible solutions for , print any solution.
Example
input
4
1
2
3
4
output
-1
57
239
6789
Note
In the first test case, there are no possible solutions for s consisting of one digit, because any such solution is divisible by itself.
For the second test case, the possible solutions are: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and .
For the third test case, one possible solution is because is not divisible by or and has three digits (none of which equals zero).
Translation
给一个整数,要求输出一个由个数字组成的正整数,要求该整数不包含,且不能被组成该整数的个数中的任意一个数整除。
Idea
显然,符合题意。
Code
1 |
|
B. Maximums
Alicia has an array, , of non-negative integers. For each , she has found a non-negative integer $x_i=\max \{0,a_1,\dots,a_{i−1}\}$. Note that for ,.
For example, if Alicia had the array $a=\{0,1,2,0,3\}$, then $x=\{0,0,1,2,2\}$.
Then, she calculated array, .
For example, if Alicia had the array $a=\{0,1,2,0,3\}, b=\{0−0,1−0,2−1,0−2,3−2\}=\{0,1,1,−2,1\}$.
Alicia gives you the values and asks you to restore the values . Can you help her solve the problem?
Input
The first line contains one integer – the number of elements in Alicia’s array.
The next line contains integers, .
It is guaranteed that for the given array there is a solution , for all elements of which the following is true: .
Output
Print n integers, , such that if you calculate according to the statement, will be equal to , will be equal to , …, and will be equal to .
It is guaranteed that there exists at least one solution for the given tests. It can be shown that the solution is unique.
Examples
input
5
0 1 1 -2 1
output
0 1 2 0 3
input
3
1000 999999000 -1000000000
output
1000 1000000000 0
input
5
2 1 2 2 3
output
2 3 5 7 10
Note
The first test was described in the problem statement.
In the second test, if Alicia had an array $a=\{1000,1000000000,0\}$, then $x=\{0,1000,1000000000\}$ and $b=\{1000−0,1000000000−1000,0−1000000000\}=\{1000,999999000,−1000000000\}$.
Translation
给一个数列$\{a_n\}$,计算数列$\{x_n\}$,$x_i=\max\{0,a_1,\dots,a_{i-1}\}$。接着,我们可以计算数列$\{b_n\}$,。
现在给出数列$\{b_n\}$,要求输出一组符合条件的$\{a_n\}$,保证$\{a_n\}$有唯一解。
Idea
$ \begin{cases} x_{i-1}=\max\{0,a_1,\dots,a_{i-2}\},i>1 \\ x_i=\max\{0,a_1,\dots,a_{i-2},a_{i-1}\} \\ \end{cases} $$\Rightarrow x_i=\begin{cases} 0,i=1\\ \max\{x_{i-1},a_{i-1}\},i>1\\ \end{cases}$$\therefore x_i=\begin{cases} 0,i=1\\ \max\{x_{i-1},b_{i-1}+x_{i-1}\},i>1\\ \end{cases}$
可以通过迭代计算出,相应的就变得非常好求。
Code
1 |
|
C. Permutation Partitions
You are given a permutation of integers from to and an integer , such that . A permutation means that every number from to is contained in exactly once.
Let’s consider all partitions of this permutation into disjoint segments. Formally, a partition is a set of segments $\{[l_1,r_1],[l_2,r_2],\dots,[l_k,r_k]\}$, such that:
for all ;
For all there exists exactly one segment , such that .
Two partitions are different if there exists a segment that lies in one partition but not the other.
Let’s calculate the partition value, defined as , for all possible partitions of the permutation into disjoint segments. Find the maximum possible partition value over all such partitions, and the number of partitions with this value. As the second value can be very large, you should find its remainder when divided by .
Input
The first line contains two integers, and — the size of the given permutation and the number of segments in a partition.
The second line contains different integers $ (1\leqslant p_i\leqslant n)$ — the given permutation.
Output
Print two integers — the maximum possible partition value over all partitions of the permutation into disjoint segments and the number of such partitions for which the partition value is equal to the maximum possible value, modulo .
Please note that you should only find the second value modulo .
Examples
input
3 2
2 1 3
output
5 2
input
5 5
2 1 5 3 4
output
15 1
input
7 3
2 7 3 1 5 4 6
output
18 6
Note
In the first test, for , there exists only two valid partitions: $\{[1,1],[2,3]\}$ and $\{[1,2],[3,3]\}$. For each partition, the partition value is equal to . So, the maximum possible value is and the number of partitions is .
In the third test, for , the partitions with the maximum possible partition value are $\{[1,2],[3,5],[6,7]\}$,$\{[1, 3], [4, 5], [6, 7]\}$,$\{[1, 4], [5, 5], [6, 7]\}$,$\{[1, 4], [5, 5], [6, 7]\}$,$\{[1, 2], [3, 6], [7, 7]\}$,$\{[1, 3], [4, 6], [7, 7]\}$,$\{[1, 4], [5, 6], [7, 7]\}$. For all of them, the partition value is equal to .
The partition $\{[1,2],[3,4],[5,7]\}$, however, has the partition value . This is not the maximum possible value, so we don’t count it.
Translation
将一个全排列$\{p_n\}$,分成个区间,定义一个和为,求这个和的最大值,并输出能够取到这个最大值的分割方式的数量。
Idea
的最大值必定是$\{p_n\}$中前个最大数字的和。因此,前个最大数字要均匀分布在个区间中。我们选中前个最大的数,只需要记录每一个被选中数字的位置,然后得到相邻两个数字的间隔,按照乘法原理计算即可。
Code
1 |
|
D1. Prefix-Suffix Palindrome (Easy version)
This is the easy version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task.
You are given a string , consisting of lowercase English letters. Find the longest string , which satisfies the following conditions:
- The length of does not exceed the length of .
- is a palindrome.
- There exists two strings and (possibly empty), such that ( “+” represents concatenation), and is prefix of while is suffix of .
Input
The input consists of multiple test cases. The first line contains a single integer , the number of test cases. The next lines each describe a test case.
Each test case is a non-empty string , consisting of lowercase English letters.
It is guaranteed that the sum of lengths of strings over all test cases does not exceed .
Output
For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them.
Example
input
5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba
output
a
abcdfdcba
xyzyx
c
abba
Note
In the first test, the string satisfies all conditions.
In the second test, the string satisfies all conditions, because:
Its length is , which does not exceed the length of the string , which equals .
It is a palindrome.
, and is a prefix of s while is a suffix of .
It can be proven that there does not exist a longer string which satisfies the conditions.
In the fourth test, the string is correct, because and a or b can be empty. The other possible solution for this test is .
Translation
给一个字符串,取的前缀,的后缀,得到新的字符串,要求的长度不大于的长度,且是回文串。输出满足条件的长度最长的字符串。
可为空串。
Idea
允许算法时间复杂度为。
先去头尾,将字符串中对称的前后缀删除并保存,在剩下的字符串中找到最长的回文前后缀,选取两者之间最长的,左右各拼接上原来的头和尾。
举例:字符串,去掉对称的前后缀后为;剩下的字符串中,最长回文前缀是,最长回文后缀是,因此选择;最终答案为 。
Code
1 |
|
D2. Prefix-Suffix Palindrome (Hard version)
This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task.
You are given a string , consisting of lowercase English letters. Find the longest string , which satisfies the following conditions:
- The length of does not exceed the length of .
- is a palindrome.
- There exists two strings and (possibly empty), such that ( “+” represents concatenation), and is prefix of while is suffix of .
Input
The input consists of multiple test cases. The first line contains a single integer , the number of test cases. The next lines each describe a test case.
Each test case is a non-empty string , consisting of lowercase English letters.
It is guaranteed that the sum of lengths of strings over all test cases does not exceed .
Output
For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them.
Example
input
5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba
output
a
abcdfdcba
xyzyx
c
abba
Note
In the first test, the string satisfies all conditions.
In the second test, the string satisfies all conditions, because:
Its length is , which does not exceed the length of the string , which equals .
It is a palindrome.
, and is a prefix of s while is a suffix of .
It can be proven that there does not exist a longer string which satisfies the conditions.
In the fourth test, the string is correct, because and a or b can be empty. The other possible solution for this test is .
Translation
给一个字符串,取的前缀,的后缀,得到新的字符串,要求的长度不大于的长度,且是回文串。输出满足条件的长度最长的字符串。
可为空串。
Idea
测试样例个数达到,每个样例的字符串总长度达到,考虑时间复杂度为的。
还是采用贪心操作,将字符串头尾截取,在剩下的字符串中找到最长的回文前后缀,选取两者之间最长的,左右各拼接上原来的头和尾。
Code
1 |
|
Postscript
扩展回文半径时,要加上条件if(i+p[i]<=cnt&&i-p[i]>=2) p[i]++
。