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的约数,所以枚 ...
随机推荐
- java虚拟机规范(se8)——class文件格式(七)
4.7.5 Exceptions 属性 Exceptions 属性是一个变长属性,它位于 method_info(§4.6)结构的属性表中. Exceptions 属性指出了一个方法需要检查的可能抛出 ...
- Java开发中的23种设计模式详解(3)行为型
本章是关于设计模式的最后一讲,会讲到第三种设计模式--行为型模式,共11种:策略模式.模板方法模式.观察者模式.迭代子模式.责任链模式.命令模式.备忘录模式.状态模式.访问者模式.中介者模式.解释器模 ...
- 搭建git服务器遇到的问题
1.错误提示: remote: error: insufficient permission for adding an object to repository database ./objects ...
- Java8 新增BASE64加解密API
什么是Base64编码? Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法 基于64个字符A-Z,a-z,0-9,+,/ ...
- 2018-2-22-在-windows-安装-Jekyll
title author date CreateTime categories 在 windows 安装 Jekyll lindexi 2018-02-22 17:47:39 +0800 2018-2 ...
- ThinkPHP5.1x 中间件实现原理
ThinkPHP5.1x的中间件,其核心还是闭包函数的应用,来实现“责任链”模式: 模拟代码: <?php //模拟的控制器 class Controller { public function ...
- SET TRANSACTION - 设置当前事务的特性
SYNOPSIS SET TRANSACTION [ ISOLATION LEVEL { READ COMMITTED | SERIALIZABLE } ] [ READ WRITE | READ O ...
- oracle 数据恢复
闪回表删除之前 flashback table t1 to before drop; 如果彻底删除表此方法无效 若要彻底删除表,则使用语句:drop table <table_name> ...
- 记录下通过Java代码打开cmd启动appium server及在使用过程中碰到的问题
1.appium server启动后,执行测试脚本,appium日志报错,提示appium setting未安装(原因是小米手机在用appium desktop调试时总是提示是否安装appium se ...
- Python中yaml和json文件的读取和应用
Python对yaml和json文件的读取: yaml文件读取: 首先创建一个yaml文件test.yaml import yaml #引入包 f=open(path) #建立Python的文件 ...