hdu 1695 综合数论 欧拉函数 分解质因子 容斥原理 打印素数表 帅呆了的一个题目 详解
来源:互联网 发布:中国企业家知乎 编辑:程序博客网 时间:2024/05/14 17:16
GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3112 Accepted Submission(s): 1095
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.
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.
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
21 3 1 5 11 11014 1 14409 9
Sample Output
Case 1: 9Case 2: 736427HintFor 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).
Source
2008 “Sunline Cup” National Invitational Contest
Recommend
wangye
/*题目大意:给你a, b, c, d, k; 找出这样的一队 x, y, 使得 gcd(x , y) = k, 并且x ∈[1, b], y ∈ [1, d], 问有多少对符合要求的(x, y)。------------------------------------------------------------------------------思路: gcd(x, y) == k 说明x,y都能被k整除, 但是能被k整除的未必gcd=k , 必须还要满足互质关系. 问题就转化为了求1~a/k 和 1~b/k间互质对数的问题可以把a设置为小的那个数,那么以y>x来保持唯一性(题目要求, 比如[1,3] =[3,1] )接下来份两种情况:1. y <= a , 那么对数就是 1~a的欧拉函数的累计和(容易想到)2. y >= a , 这个时候欧拉函数不能用了,怎么做? 可以用容斥原理,把y与1~a互质对数问题转换为y的质数因子在1~a范围内能整除的个数(质数分解和容斥关系),*/#include <iostream>#include <stdio.h>#include <memory.h>#include<math.h>#include <vector>using namespace std;const int N = 100005;typedef long long LL;#define maxn 100005LL phi[N];vector<LL> link[N];int vis[1000000+5],c; LL prime[79000]; void get_prime() //打印素数表模板{ int i,j,n,m; c=0; n=1000000; m=(int)sqrt(n+0.5); memset(vis,0,sizeof(vis)); for(i=2;i<=m;i++) if(!vis[i]) { for(j=i*i;j<=n;j+=i) vis[j]=1; } for(i=2;i<=n;i++) if(!vis[i]) prime[c++]=i; } void get_PHI() //模板 得到1->n之内 与n互质的数的个数 存入phi[n]{ int i,j; for (i = 1; i <= maxn; i++) phi[i] = i; for (i = 2; i <= maxn; i += 2) phi[i] /= 2; for (i = 3; i <= maxn; i += 2) if(phi[i] == i) { for (j = i; j <= maxn; j += i) phi[j] = phi[j] / i * (i - 1); } }void init() //求每一个数的质因数,vector储存 { LL i, j, k; for(i = 1; i < N; i++)//求n的质因数 也是模板 { k = i; for(j = 0; prime[j]*prime[j] <= k; j++) { if(k%prime[j] == 0) { link[i].push_back(prime[j]); while(k%prime[j] == 0) k /= prime[j]; } if(k == 1) break; } if(k > 1) link[i].push_back(k); }}LL make_ans(LL num,LL n)//1到num中的所有数与n的m个质因子不互质的数的个数 注意是不互质哦 容斥原理{ LL ans=0,tmp,i,j,flag; for(i=1;i<(LL)(1<<link[n].size());i++) { //用二进制来1,0来表示第几个素因子是否被用到,如m=3,三个因子是2,3,5,则i=3时二进制是011,表示第2、3个因子被用到 tmp=1,flag=0; for(j=0;j<link[n].size();j++) if(i&((LL)(1<<j)))//判断第几个因子目前被用到 flag++,tmp*=link[n][j];//第j个质因子link[n][j] if(flag&1)//容斥原理,奇加偶减 ans+=num/tmp; else ans-=num/tmp; } return ans; } int main(){ LL i, a, b, c, d, k, sum, t, zz = 1;//longlong型的数据 可以用%I64d 来输入输出 get_prime();get_PHI(); init(); scanf("%I64d", &t); while(t--) { scanf("%I64d %I64d %I64d %I64d %I64d", &a, &b, &c, &d, &k); if(k == 0 || k > b || k > d) { printf("Case %I64d: 0\n", zz++); continue; } if(b > d) swap(b, d);//保持d较大 b /= k; d /= k; sum = 0; for(i = 1; i <= b; i++) { sum += phi[i]; } for(i = b+1; i <= d; i++) { sum += b - make_ans(b, i); } printf("Case %I64d: %I64d\n", zz++, sum); } return 0;}
- hdu 1695 综合数论 欧拉函数 分解质因子 容斥原理 打印素数表 帅呆了的一个题目 详解
- hdu1695 综合数论 欧拉函数 分解质因子 容斥原理 打印素数表 各种模板
- hdu 1659 综合数论+ 筛选欧拉函数 +质因子 +容斥原理
- HDU 1695 GCD(素因子分解+容斥原理+欧拉函数)
- hdu 1695 GCD (欧拉函数+容斥原理+素因子分解)
- HDU 1695(容斥+欧拉函数+素数分解)
- HDU 1695 GCD (数论-整数和素数,欧拉函数,组合数学-容斥原理)
- hdu GCD 【欧拉函数,素因子分解,筛选法,容斥原理】
- HDU 1695(数论,筛选+素因子分解+容斥)
- HDU 1695(数论,筛选+素因子分解+容斥)
- HDU1695 欧拉函数+容斥原理+筛质因子
- HDU 4135&HDU 4407--容斥原理--质因子分解
- HDU 1695 GCD 欧拉函数+容斥原理+质因数分解
- 筛法求素数+分解质因子+欧拉函数+求约数
- HDU 1695 GCD 【容斥】【质因数分解】【欧拉函数】
- 【HDU】4135 Co-prime(容斥原理简单应用 质因子的分解)
- 数论之 素因子分解,素数筛选法,欧拉函数和扩展欧几里得算法 (整理)
- poj2773-容斥原理(dfs+二分+素数表+素因子分解)
- C# Color Name Table http://www.flounder.com/csharp_color_table.htm
- C#餐厅管理系统2--数据连接及登录
- mina服务端与c++客户端通信1
- 利用连结对多个数据表进行检索
- Quaternion 计算3D物体旋转角度
- hdu 1695 综合数论 欧拉函数 分解质因子 容斥原理 打印素数表 帅呆了的一个题目 详解
- LINKSYS E3000刷DD-WRT的方法
- window下如何让php支持openssl
- extern
- mina服务端与c++客户端通信2
- android实现短信监听
- 各种刷机包的区别
- 赛车游戏开发 - 漂移算法解决方案
- insight 6.8.1的安装