Codeforces Round #572 (Div. 2)

# Name    
A

Keanu Reeves

standard input/output

1 s, 256 MB
Submit Add to favourites  x7737
B

Number Circle

standard input/output

1 s, 256 MB
Submit Add to favourites  x6401
C

Candies!

standard input/output

2 s, 256 MB
Submit Add to favourites  x4839
D1

Add on a Tree

standard input/output

1 s, 256 MB
Submit Add to favourites  x2936
D2

Add on a Tree: Revolution

standard input/output

1 s, 256 MB
Submit Add to favourites  x219
E

Count Pairs

standard input/output

4 s, 256 MB
Submit Add to favourites  x1038
F

Array Beauty

standard input/output

5 s, 256 MB
Submit Add to favourites  x120

 

 

A. Keanu Reeves

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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, 0000are good, while 01, 1001, and 111000 are not good.

We are given a string ss of length nn consisting of only zeroes and ones. We need to cut ss into minimal possible number of substrings s1,s2,…,sks1,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,…,sks1,s2,…,sk such that their concatenation (joining) equals ss, i.e. s1+s2+⋯+sk=ss1+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 33 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 nn (1≤n≤1001≤n≤100) — the length of the string ss.

The second line contains the string ss of length nn consisting only from zeros and ones.

Output

In the first line, output a single integer kk (1≤k1≤k) — a minimal number of strings you have cut ss into.

In the second line, output kk strings s1,s2,…,sks1,s2,…,sk separated with spaces. The length of each string has to be positive. Their concatenation has to be equal to ss and all of them have to be good.

If there are multiple answers, print any.

Examples

input

Copy

1
1

output

Copy

1
1

input

Copy

2
10

output

Copy

2
1 0

input

Copy

6
100011

output

Copy

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.

 

题意:

给你长度为n的01串s,定义只有0与1的个数不相同的01串才是good串,问要把s分为几个才每一个满足good?

分析:

原本是01串 1个

不是01串 2个   s[0],s[1~n-1]

#include
using namespace std;
typedef long long ll;
const int N=50005;
const int MOD=1e9+7;
const double PI = acos(-1.0);
string s;
int main()
{
 
    ll n;
    scanf("%lld",&n);
    cin>>s;
    int cnt1=0,cnt0=0;
    for(int i=0;i
n = int(input())
s = input()
bal = 0
for c in s:
    if c == '0':
        bal += 1
    else:
        bal -= 1
if bal != 0:
    print('1' + '\n' + s)
else:
    print('2' + "\n" + s[0:-1] + " " + s[-1])

 

B. Number Circle

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given nn numbers a1,a2,…,ana1,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][1,4,5,6,7,8], the arrangement on the left is valid, while arrangement on the right is not, as 5≥4+15≥4+1 and 8>1+68>1+6.

Codeforces Round #572 (Div. 2)_第1张图片

Input

The first line contains a single integer nn (3≤n≤1053≤n≤105) — the number of numbers.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤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 nn 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

Copy

3
2 4 3

output

Copy

YES
4 2 3 

input

Copy

5
1 2 3 4 4

output

Copy

YES
4 4 2 1 3

input

Copy

3
13 8 5

output

Copy

NO

input

Copy

4
1 10 100 1000

output

Copy

NO

Note

One of the possible arrangements is shown in the first example:

4<2+34<2+3;

2<4+32<4+3;

3<4+23<4+2.

One of the possible arrangements is shown in the second example.

No matter how we arrange 13,8,513,8,5 in a circle in the third example, 1313 will have 88 and 55 as neighbors, but 13≥8+513≥8+5.

There is no solution in the fourth example.

题意:

给你n个数,让你排成一个圆圈,要满足a[i-1]+a[i+1]>a[i],输出最后序列?

分析:

又读错题意了,天哪!

对数组排序。 首先,如果a[n]≥a[n-1] + a[n-2]则答案是 NO(因为否则an不小于邻居的总和。

其他情况下,答案是 YES。

其中一个结构肯定可以(如果数组已经排序)是:a[n] a[n-2]  ......  a[n-1]


#include
#include
#include
using namespace std;
const int maxn=2e5+5;
int num[maxn];
int main ()
{
	int n;
	cin>>n;
	for(int i=0;i>num[i];
	sort(num,num+n);
	if(num[n-1]>=num[n-2]+num[n-3]) cout<<"NO";
	else {
			cout<<"YES"<=0;i--) cout<

 

n = int(input())
a = list(map(int, input().split()))
a.sort()
if a[n-1]>=a[n-2] + a[n-3]:
	print("NO")
else:
    print("YES")
    for i in range(n-1, -1, -2):
	    print(a[i], end = " ")
    for i in range(n%2, n, 2):
	    print(a[i], end = " ")

Codeforces (c) Copyright 2010

 

C. Candies!

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Consider a sequence of digits of length 2k2k [a1,a2,…,a2k][a1,a2,…,a2k]. We perform the following operation with it: replace pairs (a2i+1,a2i+2)(a2i+1,a2i+2) with (a2i+1+a2i+2)mod10(a2i+1+a2i+2)mod10 for 0≤i<2k−10≤i<2k−1. For every ii where a2i+1+a2i+2≥10a2i+1+a2i+2≥10 we get a candy! As a result, we will get a sequence of length 2k−12k−1.

Less formally, we partition sequence of length 2k2k into 2k−12k−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−12k−1)-th and (2k2k)-th numbers. For every pair such that sum of numbers in it is at least 1010, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by 1010 (and don't change the order of the numbers).

Perform this operation with a resulting array until it becomes of length 11. Let f([a1,a2,…,a2k])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][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][(8+7)mod10,(3+1)mod10,(7+0)mod10,(9+4)mod10] == [5,4,7,3][5,4,7,3], and we get 22 candies as 8+7≥108+7≥10 and 9+4≥109+4≥10.

After the second operation the sequence becomes [(5+4)mod10,(7+3)mod10][(5+4)mod10,(7+3)mod10] == [9,0][9,0], and we get one more candy as 7+3≥107+3≥10.

After the final operation sequence becomes [(9+0)mod10][(9+0)mod10] == [9][9].

Therefore, f([8,7,3,1,7,0,9,4])=3f([8,7,3,1,7,0,9,4])=3 as we got 33 candies in total.

You are given a sequence of digits of length nn s1,s2,…sns1,s2,…sn. You have to answer qq queries of the form (li,ri)(li,ri), where for ii-th query you have to output f([sli,sli+1,…,sri])f([sli,sli+1,…,sri]). It is guaranteed that ri−li+1ri−li+1 is of form 2k2k for some nonnegative integer kk.

Input

The first line contains a single integer nn (1≤n≤1051≤n≤105) — the length of the sequence.

The second line contains nn digits s1,s2,…,sns1,s2,…,sn (0≤si≤90≤si≤9).

The third line contains a single integer qq (1≤q≤1051≤q≤105) — the number of queries.

Each of the next qq lines contains two integers lili, riri (1≤li≤ri≤n1≤li≤ri≤n) — ii-th query. It is guaranteed that ri−li+1ri−li+1 is a nonnegative integer power of 22.

Output

Output qq lines, in ii-th line output single integer — f([sli,sli+1,…,sri])f([sli,sli+1,…,sri]), answer to the ii-th query.

Examples

input

Copy

8
8 7 3 1 7 0 9 4
3
1 8
2 5
7 7

output

Copy

3
1
0

input

Copy

6
0 1 2 3 3 5
3
1 2
1 4
3 6

output

Copy

0
0
1

Note

The first example illustrates an example from the statement.

f([7,3,1,7])=1f([7,3,1,7])=1: sequence of operations is [7,3,1,7]→[(7+3)mod10,(1+7)mod10][7,3,1,7]→[(7+3)mod10,(1+7)mod10] == [0,8][0,8] and one candy as 7+3≥107+3≥10→→ [(0+8)mod10][(0+8)mod10] == [8][8], so we get only 11 candy.

f([9])=0f([9])=0 as we don't perform operations with it.

题意:

Codeforces Round #572 (Div. 2)_第2张图片

n个数,q个询问,每一次询问l,r表示询问 a[l~r]之间上述图片的个数

分析:

前缀和之后,so easy

#include
using namespace std;
typedef long long ll;
const int N=500005;
const int MOD=1e9+7;
const double PI = acos(-1.0);
string s;
vectorbig,small;
ll a[N],sum[N];
int main()
{
 
    ll n;
    scanf("%lld",&n);
 
    for (int i=1; i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    for(int i=1; i<=n; i++)
    {
         sum[i]=sum[i-1]+a[i];
    }
    int q;
    scanf("%d",&q);
    for(int i=1;i<=q;i++)
	{
		ll l,r;
		scanf("%lld%lld",&l,&r);
		cout<<(sum[r]-sum[l-1])/10<

 

D2. Add on a Tree: Revolution

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nn nodes. In the beginning, 00 is written on all edges. In one operation, you can choose any 22 distinct leaves uu, vvand any integer number xx and add xx to values written on all edges on the simple path between uu and vv. Note that in previous subtask xxwas 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 22 on the path from 77 to 66, and then adding −1−1 on the path from 44 to 55.

Codeforces Round #572 (Div. 2)_第3张图片

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 11. Simple path is a path that doesn't contain any node twice.

Input

The first line contains a single integer nn (2≤n≤10002≤n≤1000) — the number of nodes in a tree.

Each of the next n−1n−1 lines contains three integers uu, vv, valval (1≤u,v≤n1≤u,v≤n, u≠vu≠v, 0≤val≤100000≤val≤10000), meaning that there is an edge between nodes uu and vv with valval written on it. It is guaranteed that these edges form a tree. It is guaranteed that all valval 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 mm — number of operations you are going to apply (0≤m≤1050≤m≤105). Note that you don't have to minimize the number of the operations!

In the next mm lines output the operations in the following format:

u,v,xu,v,x (1≤u,v≤n1≤u,v≤n, u≠vu≠v, xx — integer, −109≤x≤109−109≤x≤109), where u,vu,v — leaves, xx — 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

Copy

5
1 2 2
2 3 4
3 4 10
3 5 18

output

Copy

NO

input

Copy

6
1 2 6
1 3 8
1 4 12
2 5 2
2 6 4

output

Copy

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.

Codeforces Round #572 (Div. 2)_第4张图片

The sequence of operations from the second sample is illustrated below.

Codeforces Round #572 (Div. 2)_第5张图片

 

题意:

给你一棵树,你可以在两个叶子之间的路径上增添任何值,问是否可以实现所有的边可以变成任何值?

分析:

很简单,度为2不能变

 

#include
using namespace std;
typedef long long ll;
const int N=500005;
const int MOD=1e9+7;

int n;

int degree[N];
int main()
{
    int u,v;
    scanf("%d",&n);
    for(int i = 0; i

 

 

E. Count Pairs

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a prime number pp, nn integers a1,a2,…,ana1,a2,…,an, and an integer kk.

Find the number of pairs of indexes (i,j)(i,j) (1≤i

Input

The first line contains integers n,p,kn,p,k (2≤n≤3⋅1052≤n≤3⋅105, 2≤p≤1092≤p≤109, 0≤k≤p−10≤k≤p−1). pp is guaranteed to be prime.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤p−10≤ai≤p−1). It is guaranteed that all elements are different.

Output

Output a single integer — answer to the problem.

Examples

input

Copy

3 3 0
0 1 2

output

Copy

1

input

Copy

6 7 2
1 2 3 4 5 6

output

Copy

3

Note

In the first example:

(0+1)(02+12)=1≡1mod3(0+1)(02+12)=1≡1mod3.

(0+2)(02+22)=8≡2mod3(0+2)(02+22)=8≡2mod3.

(1+2)(12+22)=15≡0mod3(1+2)(12+22)=15≡0mod3.

So only 11 pair satisfies the condition.

In the second example, there are 33 such pairs: (1,5)(1,5), (2,3)(2,3), (4,6)(4,6).

题意:
长度为n的数组a[1~n],问有多少对满足i

分析:

(a[i]+a[j])*(a[i]*a[i]+a[j]*a[j])mod p=k

(a[i]+a[j])*(a[i]-a[j])*(a[i]*a[i]+a[j]*a[j])mod p  =(a[i]-a[j])*k mod p

(a[i]^4-k*a[i])%p=(a[j]^4-k*a[j])%p

统计一下a[i]^4-k*a[i]的个数就行

#include
using namespace std;
typedef long long ll;
const int N=500005;
const int MOD=1e9+7;
sets;
map mp;
int main(){
	ll n,p,x,k;
	cin>>n>>p>>k;
	for(int i=1;i<=n;i++){
		cin>>x;
		mp[((x%p*x%p*x%p*x%p)%p-(k%p*x%p)+p)%p]++;
		s.insert(((x%p*x%p*x%p*x%p)%p-(k%p*x%p)%p+p)%p);
	}
	ll ans=0;
	for(ll v:s)//get新技能
	{
		ans+=(mp[v]*(mp[v]-1)/2);
	}
	cout<

 

你可能感兴趣的:(好题,比赛题解)