Codeforces Round #572 (Div. 2)(ABCD1D2E)
Keanu Reeves CodeForces - 1189A
After playing Neo in the legendary “Matrix” trilogy, Keanu Reeves started doubting himself: maybe we really live in virtual reality? To find if this is true, he needs to solve the following problem.
Let’s call a string consisting of only zeroes and ones good if it contains different numbers of zeroes and ones. For example, 1, 101, 0000 are good, while 01, 1001, and 111000 are not good.
We are given a string s of length n consisting of only zeroes and ones. We need to cut s into minimal possible number of substrings s1,s2,…,sk such that all of them are good. More formally, we have to find minimal by number of strings sequence of good strings s1,s2,…,sk such that their concatenation (joining) equals s, i.e. s1+s2+?+sk=s.
For example, cuttings 110010 into 110 and 010 or into 11 and 0010 are valid, as 110, 010, 11, 0010 are all good, and we can’t cut 110010 to the smaller number of substrings as 110010 isn’t good itself. At the same time, cutting of 110010 into 1100 and 10 isn’t valid as both strings aren’t good. Also, cutting of 110010 into 1, 1, 0010 isn’t valid, as it isn’t minimal, even though all 3 strings are good.
Can you help Keanu? We can show that the solution always exists. If there are multiple optimal answers, print any.
Input
The first line of the input contains a single integer n (1≤n≤100) — the length of the string s.
The second line contains the string s of length n consisting only from zeros and ones.
Output
In the first line, output a single integer k (1≤k) — a minimal number of strings you have cut s into.
In the second line, output k strings s1,s2,…,sk separated with spaces. The length of each string has to be positive. Their concatenation has to be equal to s and all of them have to be good.
If there are multiple answers, print any.
Examples
Input
1
1
Output
1
1
Input
2
10
Output
2
1 0
Input
6
100011
Output
2
100 011
Note
In the first example, the string 1 wasn’t cut at all. As it is good, the condition is satisfied.
In the second example, 1 and 0 both are good. As 10 isn’t good, the answer is indeed minimal.
In the third example, 100 and 011 both are good. As 100011 isn’t good, the answer is indeed minimal.
如果一開始1和0的個數就不一樣的話,就直接輸出就好了。如果一樣的話,就直接將第一個字母分離出來。
代碼如下:
Number Circle CodeForces - 1189B
You are given n numbers a1,a2,…,an. Is it possible to arrange them in a circle in such a way that every number is strictly less than the sum of its neighbors?
For example, for the array [1,4,5,6,7,8], the arrangement on the left is valid, while arrangement on the right is not, as 5≥4+1 and 8>1+6.
Input
The first line contains a single integer n (3≤n≤105) — the number of numbers.
The second line contains n integers a1,a2,…,an (1≤ai≤109) — the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).
Output
If there is no solution, output “NO” in the first line.
If there is a solution, output “YES” in the first line. In the second line output n numbers — elements of the array in the order they will stay in the circle. The first and the last element you output are considered neighbors in the circle. If there are multiple solutions, output any of them. You can print the circle starting with any element.
Examples
Input
3
2 4 3
Output
YES
4 2 3
Input
5
1 2 3 4 4
Output
YES
4 4 2 1 3
Input
3
13 8 5
Output
NO
Input
4
1 10 100 1000
Output
NO
Note
One of the possible arrangements is shown in the first example:
4<2+3;
2<4+3;
3<4+2.
One of the possible arrangements is shown in the second example.
No matter how we arrange 13,8,5 in a circle in the third example, 13 will have 8 and 5 as neighbors, but 13≥8+5.
There is no solution in the fourth example.
按著最優的來,應該是由小到大的時候,符合的數量最多。這種情況下,也就最后一個數字有可能不符合情況。如果最后一個數字不符合情況,我們把最后一個數字和倒數第二個數字交換位置,這樣就是最優的了。如果這樣還是不符合題意,就沒有符合的了。
代碼如下:
Candies! CodeForces - 1189C
Consider a sequence of digits of length 2k [a1,a2,…,a2k]. We perform the following operation with it: replace pairs (a2i+1,a2i+2) with (a2i+1+a2i+2)mod10 for 0≤i<2k?1. For every i where a2i+1+a2i+2≥10 we get a candy! As a result, we will get a sequence of length 2k?1.
Less formally, we partition sequence of length 2k into 2k?1 pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth …, the last pair consists of the (2k?1)-th and (2k)-th numbers. For every pair such that sum of numbers in it is at least 10, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by 10 (and don’t change the order of the numbers).
Perform this operation with a resulting array until it becomes of length 1. Let f([a1,a2,…,a2k]) denote the number of candies we get in this process.
For example: if the starting sequence is [8,7,3,1,7,0,9,4] then:
After the first operation the sequence becomes [(8+7)mod10,(3+1)mod10,(7+0)mod10,(9+4)mod10] = [5,4,7,3], and we get 2 candies as 8+7≥10 and 9+4≥10.
After the second operation the sequence becomes [(5+4)mod10,(7+3)mod10] = [9,0], and we get one more candy as 7+3≥10.
After the final operation sequence becomes [(9+0)mod10] = [9].
Therefore, f([8,7,3,1,7,0,9,4])=3 as we got 3 candies in total.
You are given a sequence of digits of length n s1,s2,…sn. You have to answer q queries of the form (li,ri), where for i-th query you have to output f([sli,sli+1,…,sri]). It is guaranteed that ri?li+1 is of form 2k for some nonnegative integer k.
Input
The first line contains a single integer n (1≤n≤105) — the length of the sequence.
The second line contains n digits s1,s2,…,sn (0≤si≤9).
The third line contains a single integer q (1≤q≤105) — the number of queries.
Each of the next q lines contains two integers li, ri (1≤li≤ri≤n) — i-th query. It is guaranteed that ri?li+1 is a nonnegative integer power of 2.
Output
Output q lines, in i-th line output single integer — f([sli,sli+1,…,sri]), answer to the i-th query.
Examples
Input
8
8 7 3 1 7 0 9 4
3
1 8
2 5
7 7
Output
3
1
0
Input
6
0 1 2 3 3 5
3
1 2
1 4
3 6
Output
0
0
1
Note
The first example illustrates an example from the statement.
f([7,3,1,7])=1: sequence of operations is [7,3,1,7]→[(7+3)mod10,(1+7)mod10] = [0,8] and one candy as 7+3≥10 → [(0+8)mod10] = [8], so we get only 1 candy.
f([9])=0 as we don’t perform operations with it.
前綴和的考察,讀懂題意就行。
代碼如下:
Add on a Tree CodeForces - 1189D1
Note that this is the first problem of the two similar problems. You can hack this problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any real number x and add x to values written on all edges on the simple path between u and v.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding ?0.5 on the path from 4 to 5.
Is it true that for any configuration of real numbers written on edges, we can achieve it with a finite number of operations?
Leaf is a node of a tree of degree 1. Simple path is a path that doesn’t contain any node twice.
Input
The first line contains a single integer n (2≤n≤105) — the number of nodes.
Each of the next n?1 lines contains two integers u and v (1≤u,v≤n, u≠v), meaning that there is an edge between nodes u and v. It is guaranteed that these edges form a tree.
Output
If there is a configuration of real numbers written on edges of the tree that we can’t achieve by performing the operations, output “NO”.
Otherwise, output “YES”.
You can print each letter in any case (upper or lower).
Examples
Input
2
1 2
Output
YES
Input
3
1 2
2 3
Output
NO
Input
5
1 2
1 3
1 4
2 5
Output
NO
Input
6
1 2
1 3
1 4
2 5
2 6
Output
YES
Note
In the first example, we can add any real x to the value written on the only edge (1,2).
In the second example, one of configurations that we can’t reach is 0 written on (1,2) and 1 written on (2,3).
Below you can see graphs from examples 3, 4:
給你一棵樹,問能不能在樹的邊上實現任意實數的組合。
例如有x1,x2,x3三個葉子節點,x1的父節點為u,現在我們想讓x1-u邊上權值變為v。
現在我們讓x1-x3都增加v/2,x1-x2都增加v/2.然后x2-x3都減少v/2。這樣就達到了我們的目標。如果一個點的度數為2,無論我們怎么操作,這個節點的鏈接的兩邊都會同時操作,就不會實現任意組合了。只要樹中無2度節點就好了。代碼如下:
Add on a Tree: Revolution CodeForces - 1189D2
Note that this is the second problem of the two similar problems. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.
You are given a tree with n nodes. In the beginning, 0 is written on all edges. In one operation, you can choose any 2 distinct leaves u, v and any integer number x and add x to values written on all edges on the simple path between u and v. Note that in previous subtask x was allowed to be any real, here it has to be integer.
For example, on the picture below you can see the result of applying two operations to the graph: adding 2 on the path from 7 to 6, and then adding ?1 on the path from 4 to 5.
You are given some configuration of nonnegative integer pairwise different even numbers, written on the edges. For a given configuration determine if it is possible to achieve it with these operations, and, if it is possible, output the sequence of operations that leads to the given configuration. Constraints on the operations are listed in the output format section.
Leave is a node of a tree of degree 1. Simple path is a path that doesn’t contain any node twice.
Input
The first line contains a single integer n (2≤n≤1000) — the number of nodes in a tree.
Each of the next n?1 lines contains three integers u, v, val (1≤u,v≤n, u≠v, 0≤val≤10000), meaning that there is an edge between nodes u and v with val written on it. It is guaranteed that these edges form a tree. It is guaranteed that all val numbers are pairwise different and even.
Output
If there aren’t any sequences of operations which lead to the given configuration, output “NO”.
If it exists, output “YES” in the first line. In the second line output m — number of operations you are going to apply (0≤m≤105). Note that you don’t have to minimize the number of the operations!
In the next m lines output the operations in the following format:
u,v,x (1≤u,v≤n, u≠v, x — integer, ?109≤x≤109), where u,v — leaves, x — number we are adding.
It is guaranteed that if there exists a sequence of operations producing given configuration, then there exists a sequence of operations producing given configuration, satisfying all the conditions above.
Examples
Input
5
1 2 2
2 3 4
3 4 10
3 5 18
Output
NO
Input
6
1 2 6
1 3 8
1 4 12
2 5 2
2 6 4
Output
YES
4
3 6 1
4 6 3
3 4 7
4 5 2
Note
The configuration from the first sample is drawn below, and it is impossible to achieve.
The sequence of operations from the second sample is illustrated below.
有了上一題的鋪墊,我們可以直接找出不可能的時候(有2度節點)。
在有可能的時候,我們應該怎么做。
現在我們要把u-v邊上設置為權值W。我們應該在u的子樹中找一個或者兩個葉子節點,在v子樹中找到一個或者兩個葉子節點。按著上一題的操作去判斷如何操作。因為邊權肯定是偶數,所以一定會整除。找葉子節點的個數,和u或者v是否為葉子節點有關。
代碼如下:
Count Pairs CodeForces - 1189E
You are given a prime number p, n integers a1,a2,…,an, and an integer k.
Find the number of pairs of indexes (i,j) (1≤i<j≤n) for which (ai+aj)(a2i+a2j)≡kmodp.
Input
The first line contains integers n,p,k (2≤n≤3?105, 2≤p≤109, 0≤k≤p?1). p is guaranteed to be prime.
The second line contains n integers a1,a2,…,an (0≤ai≤p?1). It is guaranteed that all elements are different.
Output
Output a single integer — answer to the problem.
Examples
Input
3 3 0
0 1 2
Output
1
Input
6 7 2
1 2 3 4 5 6
Output
3
Note
In the first example:
(0+1)(02+12)=1≡1mod3.
(0+2)(02+22)=8≡2mod3.
(1+2)(12+22)=15≡0mod3.
So only 1 pair satisfies the condition.
In the second example, there are 3 such pairs: (1,5), (2,3), (4,6).
平方差。
(ai+aj)(ai^2+ aj^2)=k------>( ai^4 - aj^4)=k(ai-aj)--------> ai^4-kai= aj^4-kaj;注意取余的操作。
代碼如下:
努力加油a啊,(o)/~
總結
以上是生活随笔為你收集整理的Codeforces Round #572 (Div. 2)(ABCD1D2E)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2019CCPC-江西省赛(重现赛)-
- 下一篇: Codeforces Round #57