Codeforces 1110C (思维+数论)
题面
分析
这种数据范围比较大的题最好的方法是先暴力打表找规律
通过打表,可以发现规律如下:
定义\(x=2^{log_2a+1}\) (注意,cf官方题解这里写错了,官方题解中定义\(x=2^{log_2a}\)是有问题的
(1) 若\(a \neq 2^x-1\)
则当\(b=(2^x-1)\) xor a时a xor b=b=\(2^x-1\) ,a and b=0
gcd(a xor b,a and b)=\(2^x-1\)有最大值
(异或的性质,若a xor c =b ,则a xor b=c)
举个例就很显然了:(注意,例子中数均用二进制表示)
a=101,2^x-1=111
b=101 xor 111 =010
a xor b =111
a and b =000
gcd(111,000)=111
(2) 若\(a=2^x-1\)
gcd(a xor b ,a and b)=\(gcd(2^x-1-b,b)\)
由于\(gcd(x,y)=gcd(x-y,y)\)
\(gcd(2^x-1-b,b)=gcd(2^x-1,b)\)
所以我们只要找出\(2^x-1\)的最大且不为\(2^x-1\)的因数即可,可以直接试除法
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
inline int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
int divide(int x){
int sq=sqrt(x);
int ans=1;
for(int i=2;i<=sq;i++){
if(x%i==0){
ans=max(ans,i);
ans=max(ans,x/i);
}
}
return ans;
}
int count(int x){
int ans=0;
while(x>0){
x>>=1;
ans++;
}
return ans;
}
int solve(int x){
int l=count(x);
if((1<<l)-1==x) return divide(x);
else return (1<<l)-1;
}
int main(){
int q,x;
scanf("%d",&q);
while(q--){
scanf("%d",&x);
printf("%d\n",solve(x));
}
}
Codeforces 1110C (思维+数论)的更多相关文章
- Maximal GCD CodeForces - 803C (数论+思维优化)
C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- codeforces 798 C. Mike and gcd problem(贪心+思维+数论)
题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...
- Mike and gcd problem CodeForces - 798C (贪心思维+数论)
题目链接 比较棒的一道题, 题意: 给你一个N个数的数组,让你用尽量少的操作使整个数组的gcd大于1,即gcd(a1 ,a2,,,,an) > 1 如果可以输出YES和最小的次数,否则输出NO ...
- Codeforces 963A Alternating Sum ( 思维 && 数论 )
题意 : 题目链接 分析 : Tutorial 讲的很清楚 至于为什么这样去考虑 算是一个经验问题吧 如果一个问题要你给出模意义下的答案 就多考虑一下答案是要用逆元构造出来 也就说明有除法的存在 那么 ...
- Codeforces Round #578 (Div. 2) C. Round Corridor (思维,数论)
题意: 有一个分两层的圆盘,每层从12点方向均分插入\(n\)和\(m\)个隔板,当内层和外层的隔板相连时是不能通过的,有\(q\)个询问,每次给你内层或外层的两个点,判断是否能从一个点走到另外一个点 ...
- codeforces 735D Taxes(数论)
Maximal GCD 题目链接:http://codeforces.com/problemset/problem/735/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个n(2≤n≤2e9) ...
- Codeforces 424A (思维题)
Squats Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- Codeforces 1060E(思维+贡献法)
https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...
- Queue CodeForces - 353D (思维dp)
https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为 ...
随机推荐
- V8引擎回收机制、 内存泄露
一.垃圾回收:将内存不在使用的数据进行清理,释放内存空间 v8将内存分为新生代空间和老生代的空间 新生代空间:用于存活较短的对象 :又分为二个空间:from空间和to空间 :Scav ...
- map集合中取出分类优先级最高的类别名称
import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Map ...
- [书接上一回]在Oracle Enterprise Linux (v5.7) 中安装DB - (1/4)
在上一回中,我们安装了OEL了,现在就要安装Oracle数据. 首先登录root用户,输入账号密码或,输入命令行:startx,启动图形界面. 先将虚拟机中插入光碟(Enterprise-R5-U7- ...
- MongoDB的使用学习之(三)安装MongoDB以及一些基础操作
原文链接:http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html 此博主的 8天学通MongoDB 系列还是不错的,本 ...
- 2018-11-15-UWP-how-to-get-the-touch-width
title author date CreateTime categories UWP how to get the touch width lindexi 2018-11-15 18:49:12 + ...
- 2018-8-10-win10-uwp-读写XML
title author date CreateTime categories win10 uwp 读写XML lindexi 2018-08-10 19:16:51 +0800 2018-2-13 ...
- 二、MyBatis-HelloWorld
环境准备 1.创建数据库表 create table tbl_employee ( id ) primary key AUTO_INCREMENT comment "ID", la ...
- windows openssh安装
下载地址:https://github.com/PowerShell/Win32-OpenSSH/releases 解压好后打开目录,执行以下命令: powershell.exe -Execution ...
- Graph Convolutional Network
How to do Deep Learning on Graphs with Graph Convolutional Networks https://towardsdatascience.com/h ...
- python类对象属性查找原理
class Foo(object): def __init__(self): # 这是一个对象属性 self.obj_pro = 12 # 这是一类属性 c_pro = 11 # 这是一个静态方法 @ ...