博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
#题目:GCD XOR UVA - 12716
阅读量:7126 次
发布时间:2019-06-28

本文共 1577 字,大约阅读时间需要 5 分钟。

题目描述

Given an integer N, find how many pairs (A, B) are there such that: gcd(A, B) = A xor B where

1 ≤ B ≤ A ≤ N.
Here gcd(A, B) means the greatest common divisor of the numbers A and B. And A xor B is the
value of the bitwise xor operation on the binary representation of A and B.

input

The first line of the input contains an integer T (T ≤ 10000) denoting the >number of test cases. The
following T lines contain an integer N (1 ≤ N ≤ 30000000).
output
For each test case, print the case number first in the format, ‘Case X:’ (here, X is the serial of the
input) followed by a space and then the answer for that case. There is no new-line between cases.
Sample Input
2
7
20000000
Sample Output
Case 1: 4
Case 2: 34866117

题意:

多组数据输入,每组数据输入一个n,问在小于n的数字里面能找到多少对数满足gcd(a,b)==a^b(大于b)

解题思路:

根据异或的性质我们可以发现,a^gcd(a,b)=b;

而gcd(a,b)又是a的约数,所以我们可以枚举c,就可以像筛素数那样做,然后在暴力小数据打表的时候发现gcd(a,b)=a-b,证明详见紫书318页。所以我们在筛的时候就找a^(a-b)是否等于b就行

代码

#include 
using namespace std;int gcd (int a,int b){ if(b==0)return a; else return gcd(b,a%b);}int a[300000005];void init(){ a[1]=0; a[2]=0; for(int i=1;i<300000007;i++) { for(int j=i+i;j<30000007;j+=i) { int tmp = j-i; if((j^tmp) == i) { a[j]++; } } a[i]+=a[i-1]; }}int main(){ init(); int t; cin>>t; int flg=1; while(t--) { int n; cin>>n; int ans=a[n]; cout<<"Case "<
<<": "<
<

总结:

要多去想规律,比如说看到异或就要想到:如果a^b=c那么a^c=b,然后还要结合打表。

转载于:https://www.cnblogs.com/IAMjm/p/9429146.html

你可能感兴趣的文章
Android selector shape 无效问题
查看>>
Data Lake Analytics: 使用DataWorks来调度DLA任务
查看>>
zabbix配置web监控实现网页监控
查看>>
Postgresql lock锁等待检查
查看>>
codeforces1141D题解(暴力+贪心)
查看>>
Java Spring Boot 2.0实战MyBatis连接池阿里Druid与SQL性能监控
查看>>
信用算力基于 RocketMQ 实现金融级数据服务的实践
查看>>
基于oauth 2.0 实现第三方开放平台
查看>>
kubernetes1.4 基础篇:Learn Kubernetes 1.4 by 6 steps(1):概要
查看>>
百万下载量的 Android 应用后台收集用户信息
查看>>
SQL Server 多表数据增量获取和发布 1
查看>>
C3P0连接池
查看>>
这 25 个开源机器学习项目,一般人我不告诉 Ta
查看>>
【WePY小程序框架实战四】-使用async&await异步请求数据
查看>>
iOS UIImageView(图片)
查看>>
可折叠显示的发光搜索表单
查看>>
PostgreSQL 10.1 手册_部分 II. SQL 语言_第 12 章 全文搜索_12.2. 表和索引
查看>>
java使用正则表达式判断手机号,固定电话,身份证,邮箱,url,车牌号,日期,ip地址,mac,人名等...
查看>>
新手也能轻松掌握的分布式系统「事务」技巧
查看>>
iOS开发之使用Git的基本使用(一)
查看>>