您的位置:首页 > 其它

UVALive 6657

2015-08-25 17:35 267 查看
这道题开始想了好久都没想到 后来无意间看白皮书 突然发现居然是书里面的样题 以前都没发现

题意

给定一个整数n 问有多少对整数对(a,b)满足gcd(a,b) = a ^b

说一下思路吧 首先 a^b = c 则a^c = b 第一种办法就是枚举a 和 c然后算b = a^c最后验证一下是否有gcd(a,b) = c 白皮书上还给了一个证明 a-b <= a^b a-b >= c 假设存在c使得a-b > c则c < a-b <= a^b 与c = a^b矛盾 有了这个结论以后 可以在时间上进一步的优化 直接枚举a和c省略了gcd

代码如下

#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
#define maxn 30000010
int ans[maxn];
int main (){
int cas = 0,t;
cin >> t;
int sum = 0;
for(int c = 1; c <= maxn/2; c++){
for(int a = c+c ; a <= maxn ; a += c){
int b = a - c;
if((a^b) == c)
ans[a]++;
}
}
for(int i = 2; i <= maxn; i++)
ans[i] += ans[i-1];
while(t--){
int n;
cin >> n;
printf("Case %d: %d\n",++cas,ans
);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: