codeforces-755【C思维、并查集】

来源:互联网 发布:网络主播琪琪 编辑:程序博客网 时间:2024/06/05 10:50

题目链接:点击打开链接

A. PolandBall and Hypothesis
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

PolandBall is a young, clever Ball. He is interested in prime numbers. He has stated a following hypothesis: "There exists such a positive integer n that for each positive integer m number n·m + 1 is a prime number".

Unfortunately, PolandBall is not experienced yet and doesn't know that his hypothesis is incorrect. Could you prove it wrong? Write a program that finds a counterexample for any n.

Input

The only number in the input is n (1 ≤ n ≤ 1000) — number from the PolandBall's hypothesis.

Output

Output such m that n·m + 1 is not a prime number. Your answer will be considered correct if you output any suitable m such that1 ≤ m ≤ 103. It is guaranteed the the answer exists.

Examples
input
3
output
1
input
4
output
2
Note

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

For the first sample testcase, 3·1 + 1 = 4. We can output 1.

In the second sample testcase, 4·1 + 1 = 5. We cannot output 1 because 5 is prime. However, m = 2 is okay since 4·2 + 1 = 9, which is not a prime number.


#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int n;bool shu[1000020]={1,1};void get(){for(int i=2;i<=1000010;i++){if(shu[i])continue;for(int j=i*2;j<=1000010;j+=i){shu[j]=1;}}}int main(){get();while(~scanf("%d",&n)){int i;for(i=1;i<=1000;i++){if(shu[n*i+1])break;}printf("%d\n",i);}return 0;}

题目链接:点击打开链接

B. PolandBall and Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

PolandBall is playing a game with EnemyBall. The rules are simple. Players have to say words in turns. You cannot say a word which was already said. PolandBall starts. The Ball which can't say a new word loses.

You're given two lists of words familiar to PolandBall and EnemyBall. Can you determine who wins the game, if both play optimally?

Input

The first input line contains two integers n and m (1 ≤ n, m ≤ 103) — number of words PolandBall and EnemyBall know, respectively.

Then n strings follow, one per line — words familiar to PolandBall.

Then m strings follow, one per line — words familiar to EnemyBall.

Note that one Ball cannot know a word more than once (strings are unique), but some words can be known by both players.

Each word is non-empty and consists of no more than 500 lowercase English alphabet letters.

Output

In a single line of print the answer — "YES" if PolandBall wins and "NO" otherwise. Both Balls play optimally.

Examples
input
5 1polandballisacoolcharacternope
output
YES
input
2 2kremowkawadowickakremowkawiedenska
output
YES
input
1 2aab
output
NO
Note

In the first example PolandBall knows much more words and wins effortlessly.

In the second example if PolandBall says kremowka first, then EnemyBall cannot use that word anymore. EnemyBall can only saywiedenska. PolandBall says wadowicka and wins.


大意:两个人说单词,给出每个人会背的单词数和单词。规则是:不能说已经说过的单词,如果有一方不能说出单词了,那么对方获胜。

#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<map>#include<iostream>using namespace std;int n,m;string str;map<string,int> M;int main(){while(~scanf("%d%d",&n,&m)){M.clear();for(int i=1;i<=n;i++){cin>>str;M[str]=1;}int cnt=0;for(int i=1;i<=m;i++){cin>>str;if(M[str])cnt++;}if(n>m)puts("YES");else if(n<m)puts("NO");else{if(cnt&1)puts("YES");elseputs("NO");}}return 0;}

题目链接:点击打开链接

C. PolandBall and Forest
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirected acyclic graphs with k vertices andk - 1 edges, where k is some integer. Note that one vertex is a valid tree.

There is exactly one relative living in each vertex of each tree, they have unique ids from 1 to n. For each Ball i we know the id of its most distant relative living on the same tree. If there are several such vertices, we only know the value of the one with smallest id among those.

How many trees are there in the forest?

Input

The first line contains single integer n (1 ≤ n ≤ 104) — the number of Balls living in the forest.

The second line contains a sequence p1, p2, ..., pn of length n, where (1 ≤ pi ≤ n) holds and pi denotes the most distant from Ball irelative living on the same tree. If there are several most distant relatives living on the same tree, pi is the id of one with the smallest id.

It's guaranteed that the sequence p corresponds to some valid forest.

Hacking: To hack someone, you should provide a correct forest as a test. The sequence p will be calculated according to the forest and given to the solution you try to hack as input. Use the following format:

In the first line, output the integer n (1 ≤ n ≤ 104) — the number of Balls and the integer m (0 ≤ m < n) — the total number of edges in the forest. Then m lines should follow. The i-th of them should contain two integers ai and bi and represent an edge between vertices in which relatives ai and bi live. For example, the first sample is written as follows:

5 31 23 44 5
Output

You should output the number of trees in the forest where PolandBall lives.

Interaction

From the technical side, this problem is interactive. However, it should not affect you (except hacking) since there is no interaction.

Examples
input
52 1 5 3 3
output
2
input
11
output
1
Note

In the first sample testcase, possible forest is: 1-2 3-4-5.

There are 2 trees overall.

In the second sample testcase, the only possible graph is one vertex and no edges. Therefore, there is only one tree.


~~英语太渣,没读明白意思 orz

大意:有 n 个点,从 1 ~ n,先将每个点与其距离最远的点的标号给出,然后要我们求出这 n 个点有多少棵树形成的,比如第一个例子,1-2,3-4-5 有两棵树。

思路:

1、直接的,可以用并查集处理。

2、我们可以发现,与每个点距离最远的点一定是树的某个端点,题中已经规定,如果存在多个距离相同远的端点,那么取编号最小的端点。也就是说,只要某些点存在于同一棵树上,那么这棵树上的给出的最远的点只有两个。当然还有特例,就是一棵树上只有一个点


还有就是碰见了这个 Idleness limit exceeded on test1

在使用多个输出函数连续进行多次输出时,有可能发现输出错误。因为下一个数据再上一个数据还没输出完毕,还在输出缓冲区中时,下一个 printf 就把另一个数据加入输出缓冲区,结果冲掉了原来的数据,出现输出错误。 在  prinf();后加上 fflush(stdout); 强制马上输出,避免错误。当然也可以换种输出:cout 就不需要这个了

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n;bool vis[10010];int main(){while(~scanf("%d",&n)){memset(vis,0,sizeof(vis));int cnt=0;for(int i=1;i<=n;i++){int x;scanf("%d",&x);if(i==x)cnt++;elsevis[x]=1;}int ans=0;for(int i=1;i<=n;i++){if(vis[i])ans++;}printf("%d\n",ans/2+cnt);fflush(stdout); // 用 cout 输出就不需要这一句了 }return 0;}/* 并查集实现 #include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;int n;int a[10010];int fa[10010];int find(int x){if(x==fa[x])return x;return fa[x]=find(fa[x]);}int main(){while(~scanf("%d",&n)){for(int i=1;i<=n;i++){fa[i]=i;scanf("%d",&a[i]);}for(int i=1;i<=n;i++){int nx=find(i);int ny=find(a[i]);if(nx!=ny)fa[ny]=nx;}int ans=0;for(int i=1;i<=n;i++){if(fa[i]==i)ans++;}cout<<ans<<endl;}return 0;}*/ 



0 0