Codeforces 776

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 1ross is killed and joey appears.
  • After day 2rachel is killed and phoebe appears.
  • After day 3phoebe is killed and monica appears.
  • After day 4monica is killed and chandler appears.

/*
  水题 直接模拟
  给你两个字符串 然后进行n组替换 
  输出每次替换后的两个字符串
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <map>
#include <vector>
using namespace std;
string a,b,a1,b1;
int main(void)
{
    cin>>a>>b;
    int n;
    cin>>n;
    cout<<a<<" "<<b<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>a1>>b1;
        if(a==a1)
        {
            cout<<b<<" "<<b1<<endl;
            a=b1;
        }
        else if(b==a1)
        {
            cout<<a<<" "<<b1<<endl;
            b=b1;
        }
    }
    return 0;
}

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 23 and 4 are 11 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.


/*
  思维构造题
  题意:输入一个n 然后i是从2到n+1的的n个数 让你涂色 
        例如n=3 则让你给 2 3 4 这三个数涂色
  涂色规则为: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.
  一个数不能与它的质因数颜色相同
  问最少需要k种颜色 按顺序输出每个数的颜色 用1~k来表示。
  解法:利用素数筛筛出素数。因为我们知道质数是没有质因子的 只有1和它本身 但又不存在两个本身。
        所以最多只需要两种颜色 如果是素数则涂1 不是素数则涂2
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <map>
#include <vector>
using namespace std;
#define Max 100010
bool is_prime[Max];
void get_prime1()
{
    memset(is_prime,true,sizeof(is_prime));
    int tot=0;
    long long i,j;
    for(i=2;i<=(int)sqrt(Max*1.0);i++)
    {
        if(is_prime[i])
        {
            for(j=i*2;j<=Max;j+=i)
                is_prime[j]=false;
        }
    }
}
int main(void)
{
    get_prime1();
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==1)
        {
            printf("1\n");
            printf("1\n");
            continue;
        }
        else if(n==2)
        {
            printf("1\n");
            printf("1 1\n");
            continue;
        }
        printf("2\n");
        for(int i=2; i<=n+1; i++)
            if(is_prime[i]) printf("1 ");
            else printf("2 ");
        puts("");
    }
    return 0;
}

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 ≤ 1051 ≤ |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, 24 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的次方的方案数是多少。
  做法:对n个数求前缀和.
        然后可以认为是求有多少个方案满足sum[r]-sum[l]=k^t.(r>l)
        然后转换一下 sum[r]=k^t+sum[l];
        sum[r]为前缀和数组 枚举k^t 因为肯定不会超过1e5*1e9 而且是次方的情况下
        所以累乘循环的次数不是很多 直接暴力看能否取到即可
        细节是k等于-1和1的时候  1的话 你的k^t都是1 -1的话会出现循环1 -1
        所以要防止TLE 加入kk*=k;if(kk==1) break; 可以方便的避免这个情况。
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <map>
using namespace std;
const int maxn = 100000+100;
long long sum[maxn]={0};
map<long long,int>mp;
int main(void)
{
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        long long ans=0,num=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d",&num);
            sum[i]=sum[i-1]+num;
        }
        for(long long kk=1;kk<=1e15;)
        {
            mp.clear();
            for(int i=1;i<=n;i++)
            {
                mp[sum[i-1]]++;
                ans+=mp[sum[i]-kk];
            }
            kk*=k;
            if(kk==1) break;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值