A. A Serial Killer
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim.
The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim.
You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern.
Input
First line of input contains two names (length of each of them doesn’t exceed 10), the two initials potential victims. Next line contains integer n (1 ≤ n ≤ 1000), the number of days.
Next n lines contains two names (length of each of them doesn’t exceed 10), first being the person murdered on this day and the second being the one who replaced that person.
The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters.
Output
Output n + 1 lines, the i-th line should contain the two persons from which the killer selects for the i-th murder. The (n + 1)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order.
Examples
Input
ross rachel
4
ross joey
rachel phoebe
phoebe monica
monica chandler
Output
ross rachel
joey rachel
joey phoebe
joey monica
joey chandler
Input
icm codeforces
1
codeforces technex
Output
icm codeforces
icm technex
Note
In first example, the killer starts with ross and rachel.
• After day 1, ross is killed and joey appears.
• After day 2, rachel is killed and phoebe appears.
• After day 3, phoebe is killed and monica appears.
• After day 4, monica is killed and chandler appears.
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e7+10;
string a,b,c,d;
int n;
int main()
{
cin>>a>>b;
cin>>n;
cout<<a<<" "<<b<<endl;
while(n--)
{
cin>>c>>d;
if(a==c) a=d;
else b=d;
cout<<a<<" "<<b<<endl;
}
}
B. Sherlock and his girlfriend
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
Sherlock has a new girlfriend (so unlike him!). Valentine’s day is coming and he wants to gift her some jewelry.
He bought n pieces of jewelry. The i-th piece has price equal to i + 1, that is, the prices of the jewelry are 2, 3, 4, … n + 1.
Watson gave Sherlock a challenge to color these jewelry pieces such that two pieces don’t have the same color if the price of one piece is a prime divisor of the price of the other piece. Also, Watson asked him to minimize the number of different colors used.
Help Sherlock complete this trivial task.
Input
The only line contains single integer n (1 ≤ n ≤ 100000) — the number of jewelry pieces.
Output
The first line of output should contain a single integer k, the minimum number of colors that can be used to color the pieces of jewelry with the given constraints.
The next line should consist of n space-separated integers (between 1 and k) that specify the color of each piece in the order of increasing price.
If there are multiple ways to color the pieces using k colors, you can output any of them.
Examples
Input
3
Output
2
1 1 2
Input
4
Output
2
2 1 1 2
Note
In the first input, the colors for first, second and third pieces of jewelry having respective prices 2, 3 and 4 are 1, 1 and 2 respectively.
In this case, as 2 is a prime divisor of 4, colors of jewelry having prices 2 and 4 must be distinct.
题意:将2–n+1的数进行染色,要求每个数与它的质因子不能同色,
问最少需要的颜色和任意一种方案。
题解:素数没有质因子,所以将 所有素数染成1,其他染成2即可。注意n<3的情况。
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e7+10;
string a,b,c,d;
int n;
int vis[N];
int ans[N];
void solo()
{
memset(ans,0,sizeof(ans));
memset(vis,0,sizeof(vis));
for(int i=2;i<=100000;i++)
{
if(!vis[i])
{
ans[i]=1;
for(int j=2;j*i<=100000;j++)
{
vis[i*j]=1;
}
}
}
}
int main()
{
cin>>n;
if(n<3)
{
cout<<1<<endl;
for(int i=1;i<=n;i++)
cout<<1<<" ";
return 0;
}
else
{
solo();
cout<<2<<endl;
for(int i=2;i<=n+1;i++)
{
if(ans[i]==1) cout<<1<<" ";
else cout<<2<<" ";
}
}
}