GCD 与XOR
题目:UVA12716
题意: 问 gcd(i,j) = i ^ j 的对数(j <=i <= N ) N的范围为30000000,有10000组样例
分析:
有几个结论:(1)若 a xor b = c,则 a xor c = b。
(2)a - b <= a xor b,(a >= b)
(3)若 gcd(a,b)= a xor b = c ,(a >= b),由(2)得:a - b <= c。
再令 a = k1×c,b = k2 × c,(k1 >= k2),所以 a - b = (k1 - k2)× c,所以 a - b >= c。总:a - b = c
(这里若k1 = k2,那么 a = b,那么 a xor b = 0)
然后就是怎么枚举的问题了,要保证计算量尽量小,如果枚举a,就要枚举a的所有因数,有些数因为可能是多个数的因数,会被重复考虑很多次。所以这里要枚举因数 c ,a = k × c(k >= 2) 这样每个因数只枚举一遍,再检验b。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
const int maxm=3e7+;
int s[maxm];
void init()
{
for(int c=;c<maxm/;c++)
{
for(int a=c+c;a<maxm;a+=c)
{
int b=a-c;
if((a^b)==c)
s[a]++;
}
}
for(int i=;i<maxm;i++)
s[i]+=s[i-];
}
int main()
{
int t;
scanf("%d",&t);
int k=;
init();
while(t--)
{
int n;
scanf("%d",&n);
printf("Case %d: %d\n",++k,s[n]);
}
}
GCD 与XOR的更多相关文章
- D. Kuro and GCD and XOR and SUM
Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...
- CF 979D Kuro and GCD and XOR and SUM(异或 Trie)
CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x, ...
- CodeForces979D:Kuro and GCD and XOR and SUM(Trie树&指针&Xor)
Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...
- CodeForces 979 D Kuro and GCD and XOR and SUM
Kuro and GCD and XOR and SUM 题意:给你一个空数组. 然后有2个操作, 1是往这个数组里面插入某个值, 2.给你一个x, k, s.要求在数组中找到一个v,使得k|gcd( ...
- Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)
Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...
- codeforces 979D Kuro and GCD and XOR and SUM
题意: 给出两种操作: 1.添加一个数字x到数组. 2.给出s,x,k,从数组中找出一个数v满足gcd(x,k) % v == 0 && x + v <= s && ...
- Codeforces Round #482 (Div. 2) : Kuro and GCD and XOR and SUM (寻找最大异或值)
题目链接:http://codeforces.com/contest/979/problem/D 参考大神博客:https://www.cnblogs.com/kickit/p/9046953.htm ...
- 题解 UVA12716 GCD等于XOR GCD XOR
规律题,打表找规律即可发现 a xor b >= a - b >= gcd(a, b), 如果 a xor b = gcd(a, b) = c 则 c = a - b 枚举倍数c和a判断b ...
- UVA - 12716 GCD XOR(GCD等于XOR)(数论)
题意:输入整数n(1<=n<=30000000),有多少对整数(a, b)满足:1<=b<=a<=n,且gcd(a,b)=a XOR b. 分析:因为c是a的约数,所以枚 ...
随机推荐
- ajax跨域请求,状态码200,F12控制台报错
在接口的地方加上请求头.//跨域请求header('Access-Control-Allow-Origin:*'); 不要在ajax里面加!!!!!!!!!
- MySQLSyntaxErrorException: Row size too large 转摘自:https://confluence.atlassian.com/display/CONFKB/MySQLSyntaxErrorException%3A+Row+size+too+large
Symptoms The following appears in the atlassian-confluence.log: Caused by: com.mysql.jdbc.exceptions ...
- android ListView列表显示数据
item.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...
- Java中 Map用法
public static Map GetGoodTypes() { Map goodTypes=new HashMap(); goodTypes.put(1,"原材料"); go ...
- JAVA 实现Jacob语音播报
准备工作:下载Jar 链接:https://pan.baidu.com/s/1edskJjYrCiefVJ7l3Ul9kQ 提取码:6dg9 ---导入jar 解压jar包,将jacob.ja ...
- jQuery HTML-设置
例子1 html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &l ...
- 容器和Docker
一.容器 1.虚拟机和容器的区别 (1)为什么要用docker 服务器端开发/部署: 实现更轻量级的虚拟化,方便快速部署, 对于部署来说可以极大的减少部署的时间成本和人力成本 Docker支持将应用打 ...
- vue中Template 制作模版
一.直接写在选项里的模板 直接在构造器里的template选项后边编写.这种写法比较直观,但是如果模板html代码太多,不建议这么写. javascript代码: var app=new Vue({ ...
- html+jquery+php实现文件上传全过程
本例子采用html+jquery+php实现上传功能 html部分 <!DOCTYPE html> <html> <head> <meta charset=& ...
- jQuery判断checkbox是否选中的4种方法
方法一: ).checked) { // do something } 方法二: if($('#checkbox-id').is(':checked')) { // do something } 方法 ...