题目链接
题目大意:
给定一个a(2 ≤ a ≤2^25−1),找到一个b(0<b<a),求得最大GCD(a^b,a&b);
看了别人的题解好久才明白过来,
果然……这个世界本来就应该是二进制的世界。
①、当 a≠ (1<<k)-1 ,我们总能找到一个 b 使得 a^b==(1<<k)-1 a&b=0 ,此时gcd为最大
②、当a==(1<<k)-1,因为b不能为0,所以找不到b凑不出来(1<<k)-1,这时候可以打表了哦~
因为 a<= 2^25 -1 则这种情况下,a只有24种 (eg(二进制形式):11,111,1111,11111,…… )
ok……
代码:
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ll long long
using namespace std;
ll q,a;
map<ll,ll> mapp;
int main()
{
//dabiao();
mapp[3]=1;
mapp[7]=1;
mapp[15]=5;
mapp[31]=1;
mapp[63]=21;
mapp[127]=1;
mapp[255]=85;
mapp[511]=73;
mapp[1023]=341;
mapp[2047]=89;
mapp[4095]=1365;
mapp[8191]=1;
mapp[16383]=5461;
mapp[32767]=4681;
mapp[65535]=21845;
mapp[131071]=1;
mapp[262143]=87381;
mapp[524287]=1;
mapp[1048575]=349525;
mapp[2097151]=299593;
mapp[4194303]=1398101;
mapp[8388607]=178481;
mapp[16777215]=5592405;
mapp[33554431]=1082401;
cin>>q;
while(q--)
{
cin>>a;
if(mapp[a])
{
cout<<mapp[a]<<endl;
}else cout<<(1<<(int(log2(a))+1))-1<<endl;
}
return 0;
}
/*
ll gcd(ll a,ll b)
{
return b?gcd(b,a%b):a;
}
void dabiao()
{
for(ll i=3;i<=(1<<25)-1;i=i<<1|1)
{
ll Max_Gcd=0;
for(ll j=1;j<i;j++)
{
Max_Gcd=max(Max_Gcd,gcd(i^j,i&j));
}
printf("mapp[%lld]=%lld;\n",i,Max_Gcd);
}
}
*/
/*
mapp[3]=1;
mapp[7]=1;
mapp[15]=5;
mapp[31]=1;
mapp[63]=21;
mapp[127]=1;
mapp[255]=85;
mapp[511]=73;
mapp[1023]=341;
mapp[2047]=89;
mapp[4095]=1365;
mapp[8191]=1;
mapp[16383]=5461;
mapp[32767]=4681;
mapp[65535]=21845;
mapp[131071]=1;
mapp[262143]=87381;
mapp[524287]=1;
mapp[1048575]=349525;
mapp[2097151]=299593;
mapp[4194303]=1398101;
mapp[8388607]=178481;
mapp[16777215]=5592405;
mapp[33554431]=1082401;
*/