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的个数就不一样的话,就直接输出就好了。如果一样的话,就直接将第一个字母分离出来。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;

int n;
string s;

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		cin>>s;
		int len=s.length();
		int cnt1=0;
		int cnt2=0;
		for(int i=0;i<len;i++)
		{
			if(s[i]=='0') cnt1++;
			if(s[i]=='1') cnt2++;
		}
		if(cnt1!=cnt2) 
		{
			cout<<1<<endl<<s<<endl;
		}
		else
		{
			cout<<2<<endl;
			cout<<s[0]<<" ";
			for(int i=1;i<len;i++) cout<<s[i];
			cout<<endl;
		}
	}
}

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.
按着最优的来,应该是由小到大的时候,符合的数量最多。这种情况下,也就最后一个数字有可能不符合情况。如果最后一个数字不符合情况,我们把最后一个数字和倒数第二个数字交换位置,这样就是最优的了。如果这样还是不符合题意,就没有符合的了。
代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#define ll long long
using namespace std;

const int maxx=1e5+100;
int a[maxx];
int n;

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++) scanf("%d",&a[i]);
		sort(a,a+n);
		if(a[n-1]<a[n-2]+a[0]) 
		{
			puts("YES");
			for(int i=0;i<n;i++) cout<<a[i]<<" ";
			cout<<endl;
		}
		else
		{
			if(a[n-1]<a[n-2]+a[n-3]) 
			{
				swap(a[n-1],a[n-2]);
				puts("YES");
				for(int i=0;i<n;i++) cout<<a[i]<<" ";
				cout<<endl;
			}
			else puts("NO");
		}
	}
	return 0;
}

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.
前缀和的考察,读懂题意就行。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

const int maxx=1e5+100;
int a[maxx];
int n,m;

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			a[i]+=a[i-1];
		}
		scanf("%d",&m);
		int x,y;
		while(m--)
		{
			scanf("%d%d",&x,&y);
			printf("%d\n",(a[y]-a[x-1])/10);
		}
	}
	return 0;
}

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度节点就好了。代码如下:

#include<bits/stdc++.h>
using namespace std;

const int maxx=1e5+100;
int in[maxx];
int n;

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		int x,y;
		memset(in,0,sizeof(in));
		for(int i=1;i<n;i++)
		{
			scanf("%d%d",&x,&y);
			in[x]++;
			in[y]++;
		}
		int flag=0;
		for(int i=1;i<=n;i++)
		{
			if(in[i]==2)
			{
				flag=1;
				break;
			}
		}
		if(flag) puts("NO");
		else puts("YES");
	}
}

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是否为叶子节点有关。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define ll long long
using namespace std;

const int maxx=1e5+100;
int head[maxx];int a[maxx],b[maxx],c[maxx];
struct edge{
	int to;
	int next;
	int v;
}e[maxx*2];
struct node{
	int u;
	int v;
	int w;
	node(int x,int y,int z)
	{
		u=x,v=y,w=z;
	}
};
vector<node> ans;
int in[maxx];
int n,tot;

void init()
{
	tot=0;
	ans.clear(); 
	memset(head,-1,sizeof(head));
	memset(in,0,sizeof(in));
}
void add(int u,int v,int w)
{
	e[tot].to=v,e[tot].next=head[u],head[u]=tot++;
}
void dfs(int u,int f,vector<int> &x)//找以u为根的树中的叶子节点并且存起来。
{
	if(in[u]==1) 
	{
		x.push_back(u);
		return ;
	} 
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		if(to==f) continue;
		dfs(to,u,x);
	}
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		int u,v,w;
		init();
		for(int i=1;i<n;i++)
		{
			scanf("%d%d%d",&a[i],&b[i],&c[i]);
			u=a[i],v=b[i],w=c[i];
			add(u,v,w);
			add(v,u,w);
			in[u]++;in[v]++;
		}
		int flag=0;
		for(int i=1;i<=n;i++)
		{
			if(in[i]==2)
			{
				flag=1;
				break;
			}
		}
		if(flag) puts("NO");
		else{
			puts("YES");
			for(int i=1;i<n;i++)
			{
				vector<int> p1[2];
				dfs(a[i],b[i],p1[0]);
				dfs(b[i],a[i],p1[1]);
				ans.push_back(node(p1[0].front(),p1[1].front(),c[i]/2));
				ans.push_back(node(p1[0].back(),p1[1].back(),c[i]/2));
				if(p1[0].front()!=p1[0].back()) ans.push_back(node(p1[0].front(),p1[0].back(),-c[i]/2));
				if(p1[1].front()!=p1[1].back()) ans.push_back(node(p1[1].front(),p1[1].back(),-c[i]/2));
			}
			printf("%d\n",ans.size());
			for(int i=0;i<ans.size();i++)
			{
				printf("%d %d %d\n",ans[i].u,ans[i].v,ans[i].w);
			}
		}
	}
}

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;注意取余的操作。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;

const int maxx=3e5+100;
int n;
ll p,k;
ll a[maxx];

int main()
{
	while(scanf("%d%lld%lld",&n,&p,&k)!=EOF)
	{
		memset(a,-1,sizeof(a));
		ll x;
		for(int i=1;i<=n;i++)
		{
			scanf("%lld",&x);
			a[i]=(x%p*x%p*x%p*x%p-k*x%p+p)%p;
		}
		ll ans=0;
		ll cnt=0;
		sort(a+1,a+1+n);
		for(int i=1;i<=n+1;i++)
		{
			if(a[i]==a[i-1]) cnt++;
			else cnt=0;
			ans+=cnt;
		}
		printf("%I64d\n",ans);
	}
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值