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的约数,所以枚 ...
随机推荐
- jQuery对于demo元素的上移、下移、删除操作等实现
今天给大家分享一个实用的jQuery技能:dom元素的操作:我们经常会去获取dom元素去实现交互效果,以及数据的操作. 首先复习一下jQuery DOM 元素方法: .get() 获得由选择器指定的D ...
- 使用TortoiseGit合并分支
1.切换到主分支 2.右击选择merge, 选择被合并的分支
- React 全新的 Context API
Context API 可以说是 React 中最有趣的一个特性了.一方面很多流行的框架(例如react-redux.mobx-react.react-router等)都在使用它:另一方面官方文档中却 ...
- 前端学习(三十)es6的一些问题(笔记)
赋值表达式 document.onclick = document.onmouseover = fn; var a = b = c = d = 5; 不推荐 逗号表 ...
- 安装JDK,并检测JDK是否安装成功
方法/步骤 首先,我们需要先安装好我们的JDK软件,安装好之后,我们需要对我们的电脑进行环境变量配置的设置,这样我们安装的JDK才能真正起到作用. 第一步:我们需要将光标放在“计算机”上面,然 ...
- 矩阵(R语言)
mymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns,byrow=logical_value,dimnam ...
- 【leetcode】756. Pyramid Transition Matrix
题目如下: We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, ...
- springcloud的config
CONFIG服务端 加入依赖: <dependency> <groupId>org.springframework.cloud</groupId> <arti ...
- jenkins安装-配置
jenkins安装-配置 注意: jenkins访问 用chrome浏览器 安装包下载:http://pkg.jenkins-ci.org/redhat/ (使用2.92版本的) 安装jdk: 1.8 ...
- 【c#技术】一篇文章搞掂:常见C#技术问题
1.事件作为参数传递 public class Para { // 定义一种委托(事件类型),可以在此定义这个事件的返回值和参数 public delegate object GetDataMetho ...