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.
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).
For each query print the answer in a separate line.
3 1 2 2 4 1 10
1 3 7
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;
}
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.
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).
Print the answer to the problem.
3 3 4 5
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;
}