C. Meaningless Operations Codeforces Global Round 1 异或与运算,思维题
1 second
256 megabytes
standard input
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:
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).
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.
For each integer, print the answer in the same order as the integers are given in input.
3
2
3
5
3
1
7
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;
}
C. Meaningless Operations Codeforces Global Round 1 异或与运算,思维题的更多相关文章
- CodeForces Global Round 1
CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...
- Codeforces Global Round 1 (A-E题解)
Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...
- Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)
Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...
- Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...
- Codeforces Global Round 2 题解
Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...
- Codeforces Global Round 3
Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...
- 【手抖康复训练1 】Codeforces Global Round 6
[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...
- Codeforces Global Round 11 个人题解(B题)
Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...
- 【Codeforces Global Round 1 C】Meaningless Operations
[链接] 我是链接,点我呀:) [题意] 给你一个a 让你从1..a-1的范围中选择一个b 使得gcd(a^b,a&b)的值最大 [题解] 显然如果a的二进制中有0的话. 那么我们就让选择的b ...
随机推荐
- WPF 列表虚拟化时的滚动方式
ListBox的滚动方式 分为像素滚动和列表项滚动 通过ListBox的附加属性ScrollViewer.CanContentScroll来设置.因此ListBox的默认模板中,含有ScrollVie ...
- C#基础知识总结(七)
摘要 C#最简单的两个算法:输入数字打印出菱形星星.冒泡排序1.输入数字打印出菱形星星 static void Main(string[] args) { //输入一个数,表示菱形星号,数字必须是一个 ...
- [日常] PHP库函数fgetss的BUG
1. fgetss函数php官网的解释是: (PHP 4, PHP 5, PHP 7) fgetss — 从文件指针中读取一行并过滤掉 HTML 标记 2. 测试后出现的问题是: 当文本中有一行数据 ...
- php opcodes运行原理
谈下php的运行原理--Opcodes. 比如以下一段简单的代码: <?php echo '周伯通‘: ?> 执行这段代码会经过如下4个步骤(确切的来说,应该是PHP的语言引擎Zend) ...
- Java_文件夹分割与合并
一.思路: 1.文件切割: 使用类RandomAccessFile ,其中方法seek可以自定义读取位置,读一段,通过字节输出流(我使用BufferedOutputStream)写一段 2.文件合并 ...
- Docker介绍基本概念(一)
Docker介绍基本概念 1.什么是Docker? 说实话关于Docker是什么并太好说,下面我通过四点向你说明Docker到底是个什么东西. Docker是世界领先的软件容器平台. Docker使用 ...
- 9.并发_EJ
第66条: 同步访问共享可变的数据 所谓同步指的发出一个调用时,如果没有得到结果就不返回,直到有结果后再返回.另外相对应的是异步,指的是发出一个调用时就立即返回而不在乎此时有没有结果. 同步和异步关注 ...
- 你试过不用if撸代码吗?
译者按: 试着不用if撸代码,是件很有趣的事,而且,万一你领会了什么是“数据即代码,代码即数据”呢? 原文: Coding Tip: Try to Code Without If-statements ...
- 输出映射resultMap
①:编写接口方法 /** * 根据id查询用户 * @param id * @return */ public User queryUserById3(Integer id); ②:编写映射文件 1: ...
- JS函数浅析(一)
1.万物皆为对象 提起函数首先要知道对象,而对象又是js数据类型中的一种,所以我们从头开始. 1)javascript数据类型有哪些? 原始数据类型:number,undefined ...