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]$为 ...
随机推荐
- let,const
- 让webstorm支持ES6语法:file-setting-languages&frameworks-javascript-右侧选择ES6 - let定义变量没有预解释且不能重复定义,在定 ...
- linux中的一些常用命令
shutdown -h now 现在马上关机 shutdown -r now 现在重新启动 reboot 现在重新启动 su - 如果当前是普通用户,则输入这条命令切换到管理员用户(root),如果要 ...
- 88-基于FMC接口的2路CameraLink Base输入子卡模块
基于FMC接口的2路CameraLink Base输入子卡模块 1.板卡概述 FMC连接器是一种高速多pin的互连器件,广泛应用于板卡对接的设备中,特别是在xilinx公司的所有开发板中都使用.该Ca ...
- Ansible自动化运维工具(1)
1. Ansible的架构 Ansible的帮助文档: http://www.ansible.com.cn/index.html 2. YAML语言简介 基本规则 列表(list, [, , , .. ...
- ffmpeg参数
a) 通用选项 -L license-h 帮助-fromats 显示可用的格式,编解码的,协议的...-f fmt 强迫采用格式fmt-I filename 输入文件-y 覆盖输出文件-t durat ...
- docker中pull镜像,报错 pull access denied for ubantu, repository does not exist or may require 'docker login'
报错说明:拒绝获取ubantu, 仓库不存在或者需要登录docker 1.先尝试注册docker 2.在拉镜像前,先登录docker, 命令:docker login 3.然后执行 docker ...
- Centos7.5中的SElinux操作命令说明
设置Selinux模式 setenforce 0 0表示警告模式 1表示强制模式 关闭要设置/etc/sysconfig/selinux下将"SELINUX=enforcing"改 ...
- Centos添加硬盘分区
1. 查看硬盘信息 fdish -l 此处/dev/sdb为新添加硬盘 2. 格式化为ext4硬盘格式 mkfs.ext4 /dev/sdb 亦可使用其他格式 硬盘空间大于2T时,MBR分区无法识别更 ...
- 026:if标签使用详解
if标签使用详解: if 标签: if 标签相当于 Python 中的 if 语句,有 elif 和 else 相对应,但是所有的标签都需要用标签符号 {% %} 进行包裹. if 标签中可以使 ...
- basic deepwalk
Get to know How deepwalk works by this project. Two steps: 1. gen the graph, and gen the corpus on t ...