求点赞 qwq!
思路
题面看起来可怕,实际上问的问题很简单:给定 ,求最小的正整数 ,满足 。
解法一
我们要最小正整数 ,最小的正整数满足条件的,一定是 二进制下最低位的 ,即 。
解法二
观察样例,发现答案都是 。
我们直接枚举 即可,范围是 ,因为 。
代码
开 long long。
解法一
更推荐。
时间复杂度 。
#include<bits/stdc++.h>
#define int long long
using namespace std;
int T,x;
signed main(){
ios::sync_with_stdio(0);cin.tie(0);
cin>>T;
while(T--)cin>>x,cout<<(x&-x)<<'\n';
}解法二
时间复杂度 ,常数大。
#include<bits/stdc++.h>
#define int long long
using namespace std;
int T,x;
signed main(){
ios::sync_with_stdio(0);cin.tie(0);
cin>>T;
while(T--){
cin>>x;
for(int i=0;i<61;i++){
int y=powl(2,i);
if((x&y)==__gcd(x,y)){
cout<<y<<'\n';
break;
}
}
}
}