题目: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的更多相关文章

  1. D. Kuro and GCD and XOR and SUM

    Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...

  2. 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, ...

  3. 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 ...

  4. 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( ...

  5. 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 ...

  6. codeforces 979D Kuro and GCD and XOR and SUM

    题意: 给出两种操作: 1.添加一个数字x到数组. 2.给出s,x,k,从数组中找出一个数v满足gcd(x,k) % v == 0 && x + v <= s && ...

  7. 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 ...

  8. 题解 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 ...

  9. UVA - 12716 GCD XOR(GCD等于XOR)(数论)

    题意:输入整数n(1<=n<=30000000),有多少对整数(a, b)满足:1<=b<=a<=n,且gcd(a,b)=a XOR b. 分析:因为c是a的约数,所以枚 ...

随机推荐

  1. Scrapy框架: 通用爬虫之XMLFeedSpider

    步骤01: 创建项目 scrapy startproject xmlfeedspider 步骤02: 使用XMLFeedSpider模版创建爬虫 scrapy genspider -t xmlfeed ...

  2. HDU 2815 Mod Tree (扩展 Baby Step Giant Step )

    Mod Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  3. 54.Counting Bits( 计算1的个数)

    Level:   Medium 题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ ...

  4. Mysql语句优化建议

    一.建立索引 1)考虑在 where 及 order by 涉及的列上建立索引 2)对于模糊查询, 建立全文索引 3)对于多主键查询,建立组合索引 二.避免陷阱 然而,一些情况下可能使索引无效: 1) ...

  5. atomic 原子操作的类

    import java.util.concurrent.atomic.AtomicInteger; /** * 原子操作的类 atomic */ public class VolatileDemo { ...

  6. PHP微信红包生成算法的程序源码(用抛物线的模型实现)

    代码如下: <?php /* * 红包生成随机算法 */ header("Content-type:text/html;charset=utf-8"); date_defau ...

  7. JavaSE---多线程---线程的创建、启动

    1.概述 1.1 Java中使用Thread类表示线程:   所有的线程对象必须是Thread类 或 其子类的实例:   每条线程的作用:完成一定的任务:   Java中使用run方法来封装线程执行体 ...

  8. 生成器模式Builder

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11406502.html 1. 定义将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的 ...

  9. ContextLoaderListener vs DispatcherServlet

    In XML based Spring MVC configuration, you must have seen two declarations in web.xml file i.e. Cont ...

  10. 【InnoDB】插入缓存,两次写,自适应hash索引

    InnoDB存储引擎的关键特性包括插入缓冲.两次写(double write).自适应哈希索引(adaptive hash index).这些特性为InnoDB存储引擎带来了更好的性能和更高的可靠性. ...