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.

题意:给你当前的两个字符串 之后n组替换 输出替换之后的两个字符串

题解:map 水

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 using namespace  std;
14 string str1,str2,str3,str4;
15 map<string,string>mp;
16 int n;
17 int main()
18 {
19     cin>>str1>>str2;
20     scanf("%d",&n);
21     mp[str1]=str1;
22     mp[str2]=str2;
23     cout<<mp[str1]<<" "<<mp[str2]<<endl;
24     for(int i=1; i<=n; i++)
25     {
26         cin>>str3>>str4;
27         if(mp[str1]==str3)
28             mp[str1]=str4;
29         if(mp[str2]==str3)
30             mp[str2]=str4;
31         cout<<mp[str1]<<" "<<mp[str2]<<endl;
32     }
33     return 0;
34 }

 

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 2respectively.

In this case, as 2 is a prime divisor of 4, colors of jewelry having prices 2 and 4 must be distinct.

题意: i为2~n+1 现在为i涂色 i的颜色不能与他的质因子颜色相同 问最少需要k种颜色 输出每个的颜色 颜色用1~k表示

题解:首先要知道质数是没有质因子的  质数一定是某一个合数的质因子 所以就转变为判断质数

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 using namespace  std;
14 int n;
15 int prime[100105];
16 bool visit[100105];
17 int main()
18 {
19     int num=0;
20     memset(visit,true,sizeof(visit));
21     for(int i=2; i<=100010; ++i)
22     {
23         if(visit[i] == true)
24         {
25             num++;
26             prime[num] = i;
27         }
28         for(int j=1; ((j<=num)&&(i*prime[j]<= 100010)); ++j)
29         {
30             visit[i*prime[j]]=false;
31             if (i%prime[j]==0) break; //点睛之笔
32         }
33     }
34     scanf("%d",&n);
35     if(n<3){
36         printf("1\n");
37         for(int i=1;i<=n;i++)
38             printf("1 ");
39         printf("\n");
40         return 0;
41         }
42     printf("2\n");
43     printf("1 ");
44     for(int i=3; i<=n+1; i++)
45     {
46         if(visit[i])
47         {
48             printf("1 ");
49         }
50         else
51         {
52             printf("2 ");
53         }
54     }
55     printf("\n");
56     return 0;
57 }

 

C. Molly's Chemicals
time limit per test
2.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an affection value, The i-th of them has affection value ai.

Molly wants Sherlock to fall in love with her. She intends to do this by mixing a contiguous segment of chemicals together to make a love potion with total affection value as a non-negative integer power of k. Total affection value of a continuous segment of chemicals is the sum of affection values of each chemical in that segment.

Help her to do so in finding the total number of such segments.

Input

The first line of input contains two integers, n and k, the number of chemicals and the number, such that the total affection value is a non-negative power of this number k. (1 ≤ n ≤ 105, 1 ≤ |k| ≤ 10).

Next line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — affection values of chemicals.

Output

Output a single integer — the number of valid segments.

Examples
input
4 2
2 2 2 2
output
8
input
4 -3
3 -6 -3 12
output
3
Note

Do keep in mind that k0 = 1.

In the first sample, Molly can get following different affection values:

  • 2: segments [1, 1], [2, 2], [3, 3], [4, 4];

     

  • 4: segments [1, 2], [2, 3], [3, 4];

     

  • 6: segments [1, 3], [2, 4];

     

  • 8: segments [1, 4].

Out of these, 2, 4 and 8 are powers of k = 2. Therefore, the answer is 8.

In the second sample, Molly can choose segments [1, 2], [3, 3], [3, 4].

 题意:给你n个数  问有多少的区间的和等于 k的i次  i=0,1,2.....

 题解:枚举区间右边 记录前缀 并标记 暴力k的次幂 累加mp[sum[j]-k^i]

注意k=-1,1,0的判断 

注意k^i  i的上界我开到40是wa 101样例的 所以开到50

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 using namespace  std;
14 ll n,k;
15 ll a[100005];
16 ll sum[100005];
17 map<ll,ll> mp;
18 int main()
19 {
20     mp.clear();
21     scanf("%I64d %I64d",&n,&k);
22     sum[0]=0;
23     for(ll i=1; i<=n; i++)
24     {
25         scanf("%I64d",&a[i]);
26         sum[i]=sum[i-1]+a[i];
27     }
28     ll ans=0;
29     ll maxn=1e15;
30     ll minx=-1e15;
31     mp[0]++;
32     for(ll i=1; i<=n; i++)
33     {
34         mp[sum[i]]++;
35         ll exm=1;
36         if(k==-1)
37         {
38             ans+=mp[sum[i]-1];
39             ans+=mp[sum[i]+1];
40         }
41             else
42             {
43                 if(k==1)
44                     ans+=mp[sum[i]-1];
45                 else
46                 {
47                     ans+=mp[sum[i]-1];
48                     for(int j=0; j<=50; j++)
49                     {
50                         exm*=k;
51                         if(exm>maxn||exm<minx)
52                             break;
53                         ans+=mp[sum[i]-exm];
54                     }
55                 }
56             }
57         }
58         printf("%I64d\n",ans);
59         return 0;
60     }