Codeforces Round #276 (Div. 2)(C贪心,D)

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 integerx.

You are given multiple queries consisting of pairs of integers l and r. For each query, find thex, 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 integersli, 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
3
1 2
2 4
1 10
Output
1
3
7
Note

The binary representations of numbers from 1 to 10 are listed below:

110 = 12

210 = 102

310 = 112

410 = 1002

510 = 1012

610 = 1102

710 = 1112

810 = 10002

910 = 10012

1010 = 10102


刚开始想反了,选择了从高位相低位处理,其实应该从低位向高位处理才对,这样的得到的才是最多的最小的

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
int N;
int s[100];
int main()
{
    scanf("%d",&N);
    while(N--)
    {
        LL l,r;
        scanf("%I64d%I64d",&l,&r);
        LL tmp=l,ans=l,ans1=l;
        int len=0;
        memset(s,0,sizeof(s));
        while(tmp)
        {
            s[len++]=tmp%2;
            tmp>>=1;
        }
        for(int i=0;i<64;i++)
        {
            if(!s[i])
            {
                l+=(1LL<<i);
                if(l>r){printf("%I64d\n",ans);break;}
                ans=l;
            }
        }
    }
    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
3
3 4 5
Output
2
首先预处理出里每个数最近的是谁,然后枚举每个数的倍数,更新答案

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
int n,x,min1,ans,max1;
const int INF=1000000000;
int hash[2100000], dp[2100000], a[2100000];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(hash,0,sizeof(hash));
        min1=INF;
        max1=-1;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&x);
            hash[x]=1;
            min1=min(min1,x);
            max1=max(max1,x);
        }
        for(int i=min1; i<=2000000; i++)
        {
            if(hash[i-1]) dp[i]=i-1;
            else dp[i]=dp[i-1];
        }
        ans=0;
        for(int i=1; i<=1000000; i++)
        {
            if(hash[i])
            {
                for(int j=2*i; ; j+=i)
                {
                    if(dp[j]<i) continue ;
                    ans=max(ans,dp[j]%i);
                    if(dp[j]==max1) break;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值