zoukankan      html  css  js  c++  java
  • CodeForces

    Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question.

    Suppose you are given a positive integer aa. You want to choose some integer bb from 11to a−1a−1 inclusive in such a way that the greatest common divisor (GCD) of integers a⊕ba⊕b and a&ba&b is as large as possible. In other words, you'd like to compute the following function:

    f(a)=max0<b<agcd(a⊕b,a&b).f(a)=max0<b<agcd(a⊕b,a&b).

    Here ⊕⊕ denotes the bitwise XOR operation, and && denotes the bitwise AND operation.

    The greatest common divisor of two integers xx and yy is the largest integer gg such that both xx and yy are divided by gg without remainder.

    You are given qq integers a1,a2,…,aqa1,a2,…,aq. For each of these integers compute the largest possible value of the greatest common divisor (when bb is chosen optimally).

    Input

    The first line contains an integer qq (1≤q≤1031≤q≤103) — the number of integers you need to compute the answer for.

    After that qq integers are given, one per line: a1,a2,…,aqa1,a2,…,aq (2≤ai≤225−12≤ai≤225−1) — the integers you need to compute the answer for.

    Output

    For each integer, print the answer in the same order as the integers are given in input.

    Example

    Input

    3
    2
    3
    5
    

    Output

    3
    1
    7
    

    Note

    For the first integer the optimal choice is b=1, then a⊕b=3,a&b=0, and the greatest common divisor of 33 and 00 is 33.

    For the second integer one optimal choice isb=2, then a⊕b=1, a&b=2, and the greatest common divisor of 1and 2 is 1.

    For the third integer the optimal choice is b=2, then a⊕b=7, a&b=0a&b=0, and the greatest common divisor of 7 and 0 is 7.

    代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    #include<map>
    
    using namespace std;
    
    
    
    int main()
    {
    	int n;
    	cin>>n;
    	long long x;
    	for(int t=0;t<n;t++)
    	{
    		scanf("%lld",&x);
    		long long sum=1;
    		if(x==3)
    		{
    			printf("1
    ");
    		}
    		else if(x==7)
    		{
    			printf("1
    ");
    		}
    		else if(x==15)
    		{
    			printf("5
    ");
    		}
    		else if(x==31)
    		{
    			printf("1
    ");
    		}
    		else if(x==63)
    		{
    			printf("21
    ");
    		}
    		else if(x==127)
    		{
    			printf("1
    ");
    		}
    		else if(x==255)
    		{
    			printf("85
    ");
    		}
    		else if(x==511)
    		{
    			printf("73
    ");
    		}
    		else if(x==1023)
    		{
    			printf("341
    ");
    		}
    		else if(x==2047)
    		{
    			printf("89
    ");
    		}
    		else if(x==4095)
    		{
    			printf("1365
    ");
    		}
    		else if(x==8191)
    		{
    			printf("1
    ");
    		}
    		else if(x==16383)
    		{
    			printf("5461
    ");
    		}
    		else if(x==32767)
    		{
    			printf("4681
    ");
    		}
    		else if(x==65535)
    		{
    			printf("21845
    ");
    		}
    		else if(x==131071)
    		{
    			printf("1
    ");
    		}
    		else if(x==262143)
    		{
    			printf("87381
    ");
    		}
    		else if(x==524287)
    		{
    			printf("1
    ");
    		}
    		else if(x==1048575)
    		{
    			printf("349525
    ");
    		}
    		else if(x==2097151)
    		{
    			printf("299593
    ");
    		}
    		else if(x==4194303)
    		{
    			printf("1398101
    ");
    		}
    		else if(x==8388607)
    		{
    			printf("178481
    ");
    		}
    		else if(x==16777215)
    		{
    			printf("5592405
    ");
    		}
    		else if(x==33554431)
    		{
    			printf("1082401
    ");
    		}
            else
    		{
    		for(int t=1;t<=26;t++)
            {
            	sum*=2;
            	if(sum<=x&&sum*2>x)
            	{
            		sum*=2;
            		break;
    			}
    		}
    		cout<<sum-1<<endl;
    	   }
    	}
    	
    	return 0;
    }
    
  • 相关阅读:
    网络基础、多线程、ftp任务铺垫
    文件上传下载、socketserver(并发)、解读socketserver源码
    模拟ssh、黏包、hashlib模块(MD5)
    面向对象多继承(C3算法)/网络编程
    Slideout吐槽
    HDU 1756 Cupid's Arrow 判断点在多边形的内部
    POJ 1584 A Round Peg in a Ground Hole 判断凸多边形,判断点在凸多边形内
    位运算 找出给定的数中其他数都是两个,有两个是一个的数
    NYOJ 1107 最高的奖励(贪心+优先队列)
    POJ 2653 Pick-up sticks (判断线段相交)
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781807.html
Copyright © 2011-2022 走看看