C. Meaningless Operations Codeforces Global Round 1 异或与运算,思维题

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 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

Copy

3
2
3
5

output

Copy

3
1
7

Note

For the first integer the optimal choice is b=1b=1, then a⊕b=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 a⊕b=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 a⊕b=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;
}

  

原文地址:https://www.cnblogs.com/EchoZQN/p/10630172.html

时间: 2024-08-25 20:53:55

C. Meaningless Operations Codeforces Global Round 1 异或与运算,思维题的相关文章

Codeforces Global Round 1 (A-E题解)

Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^(k-1)+a2*b^(k-2)+...+ak*b^0的奇偶性. 题解: 暴力求模2意义下的值就好了. 代码如下: #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5+5; int

【手抖康复训练1 】Codeforces Global Round 6

[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思路手抖过不了样例,C题秒出思路手抖过不了样例*3 D题 手抖 过的了样例 ,调了1h,赛后发现变量名写错了,改一个字符就能AC... 题目等补完题一起放上来QAQ 原文地址:https://www.cnblogs.com/ttttttttrx/p/12110199.html

[Codeforces]Codeforces Global Round 1

A - Parity 题意 给定一个$b$进制数,要求输出它在十进制下是奇数还是偶数. 分析 花了我略多的时间,首先题目中给的数字范围很大,不能直接转化为10进制. 分析性质,发现只有奇数乘奇数还是奇数,其他都是偶数. 对奇数进制和偶数进制分类讨论. 偶数进制看最低位的奇偶性,如果是奇数那么这个数就是奇数,不然是偶数. 奇数进制看每一位上奇数的个数,如果是奇数个奇数就是奇数,不然是偶数. 代码 1 #include <bits/stdc++.h> 2 using namespace std;

Codeforces Global Round 1

A. Parity 签. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 100010 5 int b, k, a[N]; 6 7 int main() 8 { 9 while (scanf("%d%d", &b, &k) != EOF) 10 { 11 int res = 0; 12 for (int i = 1; i <= k; ++i) scanf("%d&

【Codeforces Global Round 1 C】Meaningless Operations

[链接] 我是链接,点我呀:) [题意] 给你一个a 让你从1..a-1的范围中选择一个b 使得gcd(a^b,a&b)的值最大 [题解] 显然如果a的二进制中有0的话. 那么我们就让选择的b的二进制中对应的位置为1 剩下全为0就好 这样a的二进制全都变成1之后就是答案了(gcd的右边是0). 但是如果a的二进制里面全是1的话. 就没办法这么构造了 这里有两种情况. ①.1的个数是偶数 那么就101010这样构造 另外一个数就是010101 答案就是010101转换成十进制 ②.1的个数是奇数

Codeforces Global Round 1 A~F

失踪人口回来写题了.. 写了几乎一下午.贴一贴代码以及口糊一下. A. 题意:计算一下这个多项式的和. 题解:暴力算一算对每一项异或一下. #include<bits/stdc++.h> using namespace std; int b,k; int kk[100005]; int main() { cin>>b>>k; for(int i=0;i<k;i++)cin>>kk[k-i]; int flag=0; for(int i=1;i<=

Frets On Fire --- 2019 Codeforces Global Round 2 Problem D

原题:https://codeforces.com/contest/1119/problem/D 题意大概是一个n行1e18列的矩阵,其中每行第一个数为s[i],剩下的数每行依次以1的速度递增.就是说,矩阵元素 a[i][j] = s[i] + j .有q个询问,每个询问有两个参数l,r,求矩阵第l列到第r列(所有行)一共出现了几个不同的数. 这道题首先要先想到,两个参数 [l,r] 其实等价于一个参数 [0,r-l] ,也就是说矩阵第0~r-l行出现的数字的个数其实和第l-r行出现的个数是一样

【 Codeforces Global Round 1 B】Tape

[链接] 我是链接,点我呀:) [题意] x轴上有m个连续的点,从1标号到m. 其中有n个点是特殊点. 让你用k段区间将这n个点覆盖. 要求区间的总长度最小. [题解] 一开始假设我们需要n个胶带(即包含每一个点) 然后因为k<=n 所以可能胶带不够用. 那么就得一个胶带跨过两个点. 怎么选择最好呢? 可以把b[i]-b[i-1]-1处理出来排个序. (优先取较小的花费) 然后取前n-k个累加和sum. 因为每取一个就少用一段胶带. 然后sum+n就是答案了 [代码] import java.i

【Codeforces Global Round 1 A】Parity

[链接] 我是链接,点我呀:) [题意] 给你一个k位数b进制的进制转换. 让你求出来转成10进制之后这个数字是奇数还是偶数 [题解] 模拟一下转换的过程,加乘的时候都记得对2取余就好 [代码] import java.io.*; import java.util.*; public class Main { static int N = (int)1e5; static InputReader in; static PrintWriter out; static int b,k; static