CF Codeforces Global Round 1 C Meaningless Operations /*^,&,gcd*/

 

 CF Codeforces Global Round 1 C Meaningless Operations /*^,&,gcd*/CF Codeforces Global Round 1 C Meaningless Operations /*^,&,gcd*/

一开始打表,发了半天呆。。。。

后面手推了异或和与的性质,,发现

当a!=2^n-1(2^n-1刚好比a大时候取到n),只要选一个b,b满足,a的二进制是1时,b的二进制是0,a的二进制是0时,b的二进制是1,这样gcd(a^b,a&b)==2^n-1。所以当a!=2^n-1,答案为2^n-1。

当a==2^n-1时,如果a除1和他本身,无因子,那么答案为1,否则答案为最大因子。。

浪费好多时间在发呆,,,,

这种题目,以后先手推,再打表,总结之后发现规律。。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;
#define LL long long
int gcd(int x,int y){
    if(y==0) return x;
    else return(gcd(y,x%y));
}
int main (){
	
	int q;
	cin>>q;
	int p=-1;
	int two[50];
	for(int i=1;i<=INT_MAX/2;i=i*2){
		two[++p]=i-1;
	}
	while(q--){
		int a;
		cin>>a;
		int k;
		for(int i=1;i<=p;i++){
			if(two[i]>=a){
				k=two[i];
				break;
			}		
		}
		if(a==k){
			int x=sqrt(a),kk=0;
			for(int i=2;i<=x;i++){
				if(a%i==0){
					kk=1;
					cout<<a/i<<endl;
					break;
				}
			}
			if(kk==0) cout<<1<<endl;
		}
		else {
			cout<<k<<endl;
		}
	}
	return 0;
}

CF Codeforces Global Round 1 C Meaningless Operations /*^,&,gcd*/