1. 程式人生 > >C. Meaningless Operations Codeforces Global Round 1 異或與運算,思維題

C. Meaningless Operations Codeforces Global Round 1 異或與運算,思維題

positive mathjax not lock clipboard divisor etc integer pro

C. Meaningless Operations time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

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 11 to a1a−1 inclusive in such a way that the greatest common divisor (GCD) of integers aba⊕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(ab,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 (1q1031≤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 (2ai22512≤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 Copy
3
2
3
5
output Copy
3
1
7
Note

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

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

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

這個題目有1500的分數,我覺得偏高了,這個題目比較簡單,不過第一眼看上去可能會覺得還比較難,但是仔細思考一下異或運算和與運算就會發現那麽一點點的規律

就是與和異或運算是相反的,再加上第一個樣例告訴你,0和3 的最大公約數是3,就會發現一點,如果一個數轉化成二進制,且不是每一個位置都是1,那麽就可以變成全部是1的數和0求最大公約數,這個肯定是最大的,其實就是你要讓一個數是0,另一個數最大。

如果全是1,你就會發現這個數肯定是個奇數,如果他可以分成奇數倍,那一個最大公約數肯定是一倍。這個可以自己好好想清楚。

#include<iostream>
#include<cstdio>
#include<cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 100;

int main()
{
	int q;
	cin >> q;
	while(q--)
	{
		ll n, num=0,ans=0;
		int flag = 0;
		cin >> n;
		while(n)
		{
			if (n % 2 == 0) flag = 1;
			n >>= 1;
		    num++;
		}
		for (int i = 1; i <= num; i++)
		{
			ll len = 1;
			for (int j = 1; j < i; j++)
			{
				len *= 2;
			}
			ans += len;
		}
		if(flag) printf("%lld\n", ans);
		else
		{
			flag = 0;
			for(int i=3;i<10000;i+=2)
			{
				if(ans%i==0)
				{
					printf("%lld\n", ans / i);
					flag = 1;
					break;
				}
			}
			if (!flag) printf("1\n");
		}
	}
	return 0;
}

  

C. Meaningless Operations Codeforces Global Round 1 異或與運算,思維題