Codeforces 272

来源:互联网 发布:linux 发行版 编辑:程序博客网 时间:2024/05/18 15:55
A. Factory
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to produce  (remainder after dividing x by m) more details. Unfortunately, no customer has ever bought any mythical detail, so all the details produced stay on the factory.

The board of directors are worried that the production by the given plan may eventually stop (that means that there will be а moment when the current number of details on the factory is divisible by m).

Given the number of details a on the first day and number m check if the production stops at some moment.

Input

The first line contains two integers a and m (1 ≤ a, m ≤ 105).

Output

Print "Yes" (without quotes) if the production will eventually stop, otherwise print "No".

Sample test(s)
input
1 5
output
No
input
3 6
output
Yes

暴力来一发:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=1e5+100;int hash[maxn];int main(){    LL a,m;    while(cin>>a>>m)    {        CLEAR(hash,0);        LL sum=a;        int flag=1;        for(int i=1;i<=m;i++)        {            sum+=sum%m;//            cout<<"2333  "<<sum<<endl;            if(hash[sum])                break;            if(sum%m==0)            {                flag=0;                break;            }        }        if(flag)   cout<<"No"<<endl;        else   cout<<"Yes"<<endl;    }    return 0;}

B. Valuable Resources
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems.

Let's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. The map contains mines with valuable resources, located at some points with integer coordinates. The sizes of mines are relatively small, i.e. they can be treated as points. The city should be built in such a way that all the mines are inside or on the border of the city square.

Building a city takes large amount of money depending on the size of the city, so you have to build the city with the minimum area. Given the positions of the mines find the minimum possible area of the city.

Input

The first line of the input contains number n — the number of mines on the map (2 ≤ n ≤ 1000). Each of the next n lines contains a pair of integers xi and yi — the coordinates of the corresponding mine ( - 109 ≤ xi, yi ≤ 109). All points are pairwise distinct.

Output

Print the minimum area of the city that can cover all the mines with valuable resources.

Sample test(s)
input
20 02 2
output
4
input
20 00 3
output
9

依然暴力:稍加判断一下,取最大值。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>typedef long long LL;typedef unsigned long long ULL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=1100;LL x,y;int n;int main(){    std::ios::sync_with_stdio(false);    while(cin>>n)    {        LL x1,x2,y1,y2;        x1=y1=1000000000;//一定要足够大。        x2=y2=-1000000000;        REP(i,n)        {            cin>>x>>y;            if(x<x1)  x1=x;            if(y<y1)  y1=y;            if(x>x2)  x2=x;            if(y>y2)  y2=y;        }//        cout<<"233  "<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<endl;        ULL  s=max(abs(x2-x1),abs(y2-y1));//        cout<<"233  "<<s<<endl;        cout<<s*s<<endl;    }    return 0;}

C. Bits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x.

You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and is maximum possible. If there are multiple such numbers find the smallest of them.

Input

The first line contains integer n — the number of queries (1 ≤ n ≤ 10000).

Each of the following n lines contain two integers li, ri — the arguments for the corresponding query (0 ≤ li ≤ ri ≤ 1018).

Output

For each query print the answer in a separate line.

Sample test(s)
input
31 22 41 10
output
137
大牛题解,简练

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>typedef long long LL;//typedef unsigned long long ULL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )int t;LL l,r;int main(){    std::ios::sync_with_stdio(false);    cin>>t;    while(t--)    {        cin>>l>>r;        LL p=1;        REP(i,64)        {            LL t=p|l;            if(t>r)  break;            l=t;p<<=1;        }        cout<<l<<endl;    }    return 0;}

D. Maximum Value
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a sequence a consisting of n integers. Find the maximum possible value of  (integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj.

Input

The first line contains integer n — the length of the sequence (1 ≤ n ≤ 2·105).

The second line contains n space-separated integers ai (1 ≤ ai ≤ 106).

Output

Print the answer to the problem.

Sample test(s)
input
33 4 5
output
2

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=1e5+100;int a[maxn*2];int main(){    int n;    std::ios::sync_with_stdio(false);//    cout<<(5%3)<<" "<<(3%5)<<endl;    while(cin>>n)    {        REP(i,n)  cin>>a[i];        sort(a,a+n);        int ans=0;        REP(i,n-1)           if(i==0||a[i]!=a[i-1]){              int t=a[i]+a[i];              ans=max(ans,a[n-1]%a[i]);              while(t<=a[n-1])              {                int p=lower_bound(a,a+n,t)-a;                if(p>0)  ans=max(ans,a[p-1]%a[i]);                t+=a[i];              }           }        cout<<ans<<endl;    }    return 0;}



0 0
原创粉丝点击