HDU 2588 GCD

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2588

解题思路:

本来的思路是枚举大于等于M的数S,若S|N,那么KS(KS<N)GCD(KS,N)>=M,

这样有重复的。

而这样有重复的:

若GCD(x,N)>=M.

设y=N/C,那么y的素因子有phi[i],phi[i+1].......,

那么GCD(x*phi[i],N)>=M,

而根据算术基本定理可知上述方法没有重复的。

那么ans=所有大于M的N的因子X的N/X的欧拉函数值

实现代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6
 7
 8 long long euler(long long n){
 9     long long res=n;
10     for(int i=2;i*i<=n;i++){
11         if(n%i==0){
12             res=res/i*(i-1);
13             while(n%i==0)
14                 n/=i;
15         }
16     }
17     if(n>1)  //剩下的数也是n的素因数
18         res=res/n*(n-1);
19     return res;
20 }
21
22
23 int solve(int N,int M){
24     long long ans=0;       //用这种方法超时。
25     /*for(int i=M;i<=N;i++){
26        if(N%i==0)
27         ans+=euler(N/i);
28     }*/
29     for(int i=1;i*i<=N;i++){
30         if(N%i==0){
31             if(i>=M)
32                 ans+=euler(N/i);
33             if((N/i)!=i&&N/i>=M)
34                 ans+=euler(i);
35         }
36
37     }
38     cout<<ans<<endl;
39 }
40
41 int main(){
42     int T;
43     scanf("%d",&T);
44     while(T--){
45         int N,M;
46         cin>>N>>M;
47         solve(N,M);
48     }
49     return 0;
50 }
时间: 2024-08-02 11:02:21

HDU 2588 GCD的相关文章

HDU 2588 GCD (欧拉函数)

GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1013    Accepted Submission(s): 457 Problem Description The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes writt

HDU 2588 GCD【欧拉函数的运用】

http://acm.hdu.edu.cn/showproblem.php?pid=2588 题意:输入s个数 输入n  m  表示从1到n的数与n的公约数大于m的数的个数 思路: 首先找出n的所有大于m的公约数k,然后求出每个对应的n/k的euler(欧拉函数)即小于n/k的数与n/k互质的个数,那么这些数与n/k互质且小于n/k,那么这些与n/k互质的数  乘以k之后那么就变成了与n公约数为k的数(k>m)   把所有的euler(n/k)相加即是答案    这是参考别人的思路的. #inc

题解报告:hdu 2588 GCD

Description The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,For example,(1,2)=1,(12,18)=6. (a,b) can be easily found by the Euclidean algorithm. Now Carp is consid

HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a <= b <= 100000, c=1, c <= d <= 100000, 0 <= k <= 100000) 思路:因为x与y的最大公约数为k,所以xx=x/k与yy=y/k一定互质.要从a/k和b/k之中选择互质的数,枚举1~b/k,当选择的yy小于等于a/k时,可以

HDU 1695 GCD (数论-整数和素数,组合数学-容斥原理)

GCD 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 t

HDU 5726 GCD 区间GCD=k的个数

GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2742    Accepted Submission(s): 980 Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There ar

hdu 4497 GCD and LCM(排列组合)

题目:hdu 4497 GCD and LCM 题目大意:给出三个数的最大公约数,和最小公倍数,问这三个数的排列组合关系. 解题思路:最小公倍数/最大公约数 ==  三个数不同部分的乘积.这样来考虑的话,三个数都要有最大公约数的部分,其余的部分就是由LCM / GCD 里面的因子构成.这里面的因子可能会有 2 2 3 这样的情况, 不同的因子之间是不会相互干扰的,但是相同的会出现问题,因为,不能同时将相同的因子都放在三个位置上,这样最大公约数就的要乘上这个因子.然后对于单种因子来考虑的话,每种因

HDU 5902 GCD is Funny DP

http://acm.hdu.edu.cn/showproblem.php?pid=5902 一眼看上去,以为是枚举两个数,然后去重gcd即可. 但是不是 6 6 10 15这样的数据 应该是1 2 3 5 6 1是从6 6 15得到gcd = 3,然后3和10搭配得到的. 那么就是其它的gcd能够作为新的数字,去枚举其他数字,得到其他gcd.其实这个应该很容易想到的,但是比赛的时候却认为只用枚举两个就够了,可能是因为1001,觉得应该是水题吧.其实是自己蔡.. 那么考虑dp. dp[i][va

hdu 4497 GCD and LCM 数论 素数分解

GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1339    Accepted Submission(s): 607 Problem Description Given two positive integers G and L, could you tell me how many solutions of