Codeforces Beta Round #67 (Div. 2)——A,B,C

来源:互联网 发布:asp.net php 编辑:程序博客网 时间:2024/05/16 08:19
A. Life Without Zeros
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Can you imagine our life if we removed all zeros from it? For sure we will have many problems.

In this problem we will have a simple example if we removed all zeros from our life, it's the addition operation. Let's assume you are given this equation a + b = c, where a and b are positive integers, and c is the sum of a and b. Now let's remove all zeros from this equation. Will the equation remain correct after removing all zeros?

For example if the equation is 101 + 102 = 203, if we removed all zeros it will be 11 + 12 = 23 which is still a correct equation.

But if the equation is 105 + 106 = 211, if we removed all zeros it will be 15 + 16 = 211 which is not a correct equation.

Input

The input will consist of two lines, the first line will contain the integer a, and the second line will contain the integer b which are in the equation as described above (1 ≤ a, b ≤ 109). There won't be any leading zeros in both. The value of c should be calculated as c = a + b.

Output

The output will be just one line, you should print "YES" if the equation will remain correct after removing all zeros, and print "NO" otherwise.

#include <iostream>#include <cstdio>#include <cstring>#include <string>using namespace std;int a[20];int change(int x){    int sum=0,k=0;    while(x)    {        a[k++]=x%10;         x/=10;    }    for(int i=k-1;i>=0;i--)        if(a[i]) sum=sum*10+a[i];    return sum;}int main(){    int a,b;    while(cin>>a>>b)    {        int t=a+b;        t=change(t);        int w=change(a)+change(b);        if(t==w) cout<<"YES"<<endl;        else cout<<"NO"<<endl;    }    return 0;}

B. Facetook Priority Wall
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described).

This priority factor will be affected by three types of actions:

  • 1. "X posted on Y's wall" (15 points),
  • 2. "X commented on Y's post" (10 points),
  • 3. "X likes Y's post" (5 points).

X and Y will be two distinct names. And each action will increase the priority factor between X and Y (and vice versa) by the above value of points (the priority factor between X and Y is the same as the priority factor between Y and X).

You will be given n actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you.

Input

The first line contains your name. The second line contains an integer n, which is the number of actions (1 ≤ n ≤ 100). Then n lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters.

Output

Print m lines, where m is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order.

Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor.

The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line a is lexicographically smaller than the line b, if either a is the prefix of b, or if exists such an i (1 ≤ i ≤ min(|a|, |b|)), that ai < bi, and for anyj (1 ≤ j < iaj = bj, where |a| and |b| stand for the lengths of strings a and b correspondently.

#include <iostream>#include <string>#include <algorithm>#include <vector>#include <map>using namespace std;string me;map<string,int>m;map<string,int>p;int main(){    p["posted"]=15;    p["commented"]=10;    p["likes"]=5;    cin>>me;    int t;    cin>>t;    while(t--)    {        string ss,tt,gg,ww;        cin>>ss>>tt;        if(tt[0]=='l')            cin>>ww;        else cin>>ww>>ww;        gg=ww.substr(0,ww.length()-2);        m[ss]+=0;        m[gg]+=0;        if(ss==me||gg==me){            m[ss]+=p[tt];            m[gg]+=p[tt];        }        cin>>ww;    }    vector<pair<int, string> > ans;    for(map<string,int>::iterator itr = m.begin(); itr != m.end(); itr++)        if(itr->first != me)            ans.push_back( make_pair(-(itr->second), itr->first));    sort(ans.begin(), ans.end());    for(int i=0; i<ans.size(); i++)        cout << ans[i].second << endl;    return 0;}

C. Modified GCD
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.

A common divisor for two positive numbers is a number which both numbers are divisible by.

But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a andb that is in a given range from low to high (inclusive), i.e. low ≤ d ≤ high. It is possible that there is no common divisor in the given range.

You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.

Input

The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integern, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).

Output

Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.

#include <iostream>#include <cstdio>#include <set>using namespace std;int gcd(int a,int b){    return b==0?a:gcd(b,a%b);}int main(){    set<int> s;    set<int>::iterator p;    int a,b,n;    cin>>a>>b;    int g=gcd(a,b);//既然是a,b的公共因子,一定是最大公约数的约数    for(int i=1;i*i<=g;i++)//将所有约数加入集合        if(g%i==0)        {            s.insert(i);            s.insert(g/i);        }    cin>>n;    while(n--)    {        cin>>a>>b;        p=s.upper_bound(b);//找到大于b的第一个元素        if(p==s.begin()||*--p<a)//向前移动一位判断与a的大小关系           puts("-1");        else            printf("%d\n",*p);    }    return 0;}