博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1695-GCD(数论-欧拉函数-容斥)
阅读量:6141 次
发布时间:2019-06-21

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

GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5454    Accepted Submission(s): 1957


Problem Description
Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output the total number of different number pairs.
Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.
Yoiu can assume that a = c = 1 in all test cases.
 

Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases.
Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above.
 

Output
For each test case, print the number of choices. Use the format in the example.
 

Sample Input
 
2 1 3 1 5 1 1 11014 1 14409 9
 

Sample Output
 
Case 1: 9 Case 2: 736427
Hint
For the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).
 
题意: 求(1,a) 和(1,b) 两个区间 公约数为k的对数的个数
思路:将a,b分别处以k,就能够转化为(1,a/k)和(1,b/k)两个区间两两互质的个数,能够先用欧拉函数求出(1,a)两两互质的个数,(a+1,b) 能够分解质因数,由于质因数的个数最多为7能够用容斥原理计算。
#include 
#include
#include
#include
#include
#include
#include
using namespace std;const int maxn = 10000+10;const int maxxn = 100000+10;typedef long long ll;int a,b,gcd;ll ans;bool isPrime[maxn];ll minDiv[maxxn],phi[maxxn],sum[maxxn];vector
prime,cnt[maxxn],digit[maxxn];void getPrime(){ prime.clear(); memset(isPrime,1,sizeof isPrime); for(int i = 2;i < maxn; i++){ if(isPrime[i]){ prime.push_back(i); for(int j = i*i; j < maxn; j+=i){ isPrime[j] = 0; } } }}void getPhi(){ for(ll i = 1; i < maxxn; i++){ minDiv[i] = i; } for(ll i = 2; i*i < maxxn; i++){ if(minDiv[i]==i){ for(int j = i*i; j < maxxn; j += i){ minDiv[j] = i; } } } phi[1] = 1; sum[1] = 1; for(ll i = 2; i < maxxn; i++){ phi[i] = phi[i/minDiv[i]]; if((i/minDiv[i])%minDiv[i]==0){ phi[i] *= minDiv[i]; }else{ phi[i] *= minDiv[i]-1; } sum[i] = phi[i]+sum[i-1]; }}void getDigit(){ for(ll i = 1; i < maxxn; i++){ int x = i; for(int j = 0; j < prime.size()&&x >= prime[j]; j++){ if(x%prime[j]==0){ digit[i].push_back(prime[j]); int t = 0; while(x%prime[j]==0){ t++; x /= prime[j]; } cnt[i].push_back(t); } } if(x!=1){ digit[i].push_back(x); cnt[i].push_back(1); } }}int main(){ getPrime(); getPhi(); getDigit(); int ncase,T=1; cin >> ncase; while(ncase--){ int t1,t2; scanf("%d%d%d%d%d",&t1,&a,&t2,&b,&gcd); if(gcd==0){ printf("Case %d: 0\n",T++,ans); continue; }else{ if(a > b) swap(a,b); a /= gcd,b /= gcd; ans = sum[a]; for(ll i = a+1; i <= b; i++){ int d = digit[i].size(); int t = 0; vector
di; for(int k = 1; k < (1<

转载地址:http://jvkya.baihongyu.com/

你可能感兴趣的文章
建筑电气暖通给排水协作流程
查看>>
JavaScript面向对象编程深入分析(2)
查看>>
linux 编码转换
查看>>
POJ-2287 Tian Ji -- The Horse Racing 贪心规则在动态规划中的应用 Or 纯贪心
查看>>
Windows8/Silverlight/WPF/WP7/HTML5周学习导读(1月7日-1月14日)
查看>>
关于C#导出 文本文件
查看>>
使用native 查询时,对特殊字符的处理。
查看>>
maclean liu的oracle学习经历--长篇连载
查看>>
ECSHOP调用指定分类的文章列表
查看>>
分享:动态库的链接和链接选项-L,-rpath-link,-rpath
查看>>
阿里云企业邮箱 在Foxmail 7.0上POP3/IMAP协议设置方法
查看>>
Javascript一些小细节
查看>>
canvas学习总结
查看>>
Javascript的if判断
查看>>
spring cloud gateway 源码解析(3)记录请求参数及返回的json
查看>>
阿里云ECS数据盘格式化与挂载图文教程
查看>>
Flexbox响应式网页布局 - W3Schools视频02
查看>>
【手牵手】搭建前端组件库(二)
查看>>
怎么给视频添加音频或配乐
查看>>
怎么转换音乐格式
查看>>