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)/~

Codeforces Round #572 (Div. 2)(ABCD1D2E)相关推荐

  1. Codeforces Round #249 (Div. 2) (模拟)

    Codeforces Round #249 (Div. 2) (模拟) C. Cardiogram time limit per test 1 second memory limit per test ...

  2. Codeforces Round #791 (Div. 2)(A-D)

    Codeforces Round #791 (Div. 2)(A-D) A. AvtoBus 题意: 给你 n, 问满足 4x+6y=n4x+6y=n4x+6y=n 的 x+yx+yx+y的最小值和最 ...

  3. Codeforces Round #613 (Div. 2)(B-D)

    Codeforces Round #613 (Div. 2) B -抢时间&手感生疏->(第n + 1次)被long long卡掉- C 又一次败给了常识-EC的M题也是一个关于素数的暴 ...

  4. Codeforces Round #736 (Div. 2)(B-C)

    Codeforces Round #736 (Div. 2) 花了十分钟帮朋友写了B,C就睡觉了,自己没打 B 先看上,再看左后看右 #include <iostream> #includ ...

  5. Codeforces Round #735 (Div. 2)(A-D)没有B

    Codeforces Round #735 (Div. 2) A 一道小思维题 #include <bits/stdc++.h> using namespace std; #define ...

  6. Codeforces Round #644 (Div. 3)(A-E)

    这场的A-E都是水题,就简单记录一下吧. Minimal Square CodeForces - 1360A 思路:我们令b=max(a,b),a=min(a,b). 如果b>=2*a的话,最终 ...

  7. Codeforces Round #573 (Div. 2)(ABCD)

    Tokitsukaze and Enhancement CodeForces - 1191A Tokitsukaze is one of the characters in the game &quo ...

  8. Codeforces Round #640 (Div. 4)(ABCDE)

    Sum of Round Numbers CodeForces - 1352A 思路:按照题意模拟即可. 代码如下: #include<bits/stdc++.h> #define ll ...

  9. Codeforces Round #731 (Div. 3)(ABCDEFG)

    A. Shortest Path with Obstacle 当两个点的x坐标或者y坐标相同的时候,需要多花2费绕开 #include<bits/stdc++.h> using names ...

最新文章

  1. SourceTree的安装
  2. web.config/app.config敏感数据加/解密的二种方法
  3. linux ssh 连接错误 WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
  4. linux chkconfig 和 systemctl 区别 对比
  5. PHP对二维数组中的某个字段的值进行排序
  6. mysql存储表情测试_Mysql正确的储存处emoji表情
  7. why in GM0 the extAddTo Controller is not loaded
  8. android 广告设置秒数,Android动态显示具体到秒的相聚时间
  9. php 打印 wap,PHP 输出简单动态WAP页面
  10. RabbitMQ指南之三:发布/订阅模式(Publish/Subscribe)
  11. 数据库期末总结笔记( 零基础 )
  12. 第一次作业 四班15
  13. 中国计量大学matlab,计量经济学及matlab.pdf
  14. 闺蜜生日c语言代码,祝闺蜜生日快乐的说说大全 2018最新祝朋友生日快乐经典说说...
  15. 深度学习论文: Avoiding Overfitting: A Survey on Regularization Methods for Convolutional Neural Networks
  16. 外贸找客户软件-Bitrecover Email Extractor
  17. 排序函数qsort和sort那点事
  18. 人体感应模块stm32驱动
  19. 公司的报表工具太难用,我三天撸了个Excel工具,运营小姐姐直呼太好用了,现已开源!!(建议收藏)
  20. AJAX框架简笔画风景简单,好看简单的简笔画风景

热门文章

  1. ccd相机好修吗_「CCD购买指南 」CCD废片大公开
  2. hive 结构metastone_基于MySQL元数据的Hive的安装和简单测试
  3. 神经网络与深度学习笔记
  4. flask mysql环境配置_Flask干货:访问数据库——Flask-SQLAlchemy初始化
  5. python数据分析准备_使用Python进行数据分析I 环境准备
  6. HTML与CSS基础之伪元素(五)
  7. php text留言本,PHP+TEXT留言本(五)
  8. Swift--数组和字典(二)
  9. Android Asynchronous Http Client-Android异步网络请求客户端接口
  10. layoutSubviews 详解