Uva 12716
来源:互联网 发布:cordova项目app源码 编辑:程序博客网 时间:2024/06/06 10:53
题目链接
GCD XOR
题目分析
这个题是《刘汝佳算法经典》上面的题目,一个数域筛法的经典题目.我做一个搬运工~
一个基本的想法是枚举每个公约数
如果你打印出几组结果出来看看,你可以发现,若gcd(a,b) = a^b=c,则必有
证:
因为
不过需要注意我们需要离线的计算出所有的解,因为T很大.
AC code
#include <cstdio>#include <iostream>#include <vector>#include <queue>#include <algorithm>#include<cmath>#include <cstring>#include <map>#define fi first#define se secondusing namespace std;const int MOD = 1e9+7;const int MAX_P = 2e4+10;const int maxn =3e7+10;typedef long long LL;LL gcd(LL a,LL b){ return b == 0? a : gcd(b,a%b);}int n;LL ans[maxn];LL sum[maxn];void init(){ memset(ans,0,sizeof(ans)); for(int c =1 ; c<maxn ; ++c){ for(int a = c*2 ; a<maxn ; a+=c){ int b = a-c; if((a^b) == c)ans[max(a,b)]++; } } sum[0] =0; for(int i=1 ; i<maxn ; ++i) sum[i]= sum[i-1]+ans[i];}int main(int argc, char const *argv[]) { int T; cin>>T; int kase =0; init(); while (T--) { int n; scanf("%d",&n ); printf("Case %d: %d\n",++kase,sum[n] ); } return 0;}
0 0
- Uva 12716
- UVA 12716
- Uva 12716
- UVA 12716 - GCD XOR
- uva 12716 - GCD XOR
- 【UVA】12716-GCD XOR
- UVA-12716 - GCD XOR
- UVA 12716 GCD XOR
- UVa 12716 - GCD XOR
- UVA 12716 GCD XOR
- UVA 12716 GCD XOR
- uva 12716 打表找规律
- Uva 12716 GCD XOR
- uva 12716 GCD XOR
- UVa 12716 GCD XOR
- UVa 12716 GCD XOR
- uva 12716 GCD XOR
- uva
- 数据结构 第一章 概论
- 设计模式 之 MVP
- 蓝桥杯模拟赛2017 猜算式
- 贪心算法
- 割点,割桥,双联通模板总结
- Uva 12716
- Spring学习
- 4月6日,StringBufferModify,每日20行。
- 自定义对话框Dialog
- Qt 数据库相关操作
- 【Android Sqlite】萌动的sqlite数据库,简单实现:用户增删改查
- leetcode455: Assign Cookies
- 基本数据结构(二)——树
- javaScript 中var self=this 的解释