博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 2588 GCD && GCD问题总结
阅读量:7078 次
发布时间:2019-06-28

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

                                                                                                             GCD(一)

题目:

 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 considering a little more difficult problem:
Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.

   求满足题目要求的x个数。

算法:

   直接筛选会超时,依据题目给出的不等式特点GCD(x,N) >= M 能够知道满足题目要求的一定是N的因子并且必须大于等于M(想想为什么?解体关键)。所以,仅仅要枚举N的大于等于M的因子就能够了。

由于,在10^9内最多的因子数不超过30个。

所以,总时间是O(30*loglogn)接近常数。

 

#include 
#include
#include
#include
#include
using namespace std;typedef __int64 LL;const int MOD = 1000000007;int euler_phi(int n){ int k = (int)sqrt(n + 0.5); int ans = n; for(int i = 2;i <= k;++i) if(0 == n % i){ ans = ans / i * (i - 1); while(0 == n % i) n /= i; } if(n > 1) ans = ans / n * (n - 1); return ans;}LL getFact(int n,int m){ LL res = 0; int k = sqrt(n + 0.5); int tmp; for(int i = 1;i <= k;++i){ if(0 == n % i){ tmp = n / i; if(i >= m) res += euler_phi(n / i); if(tmp >= m && i != tmp) res += euler_phi(n / tmp); } } return res;}int main(){ int T,n,m; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); if(n == 1 && m == 1){ puts("1"); continue; } printf("%I64d\n",getFact(n,m)); } return 0;}

 

  

                                GCD(二)

题目:

   给你一个数N,使得在1~N之间可以找到x使得x满足gcd( x ,  N  ) >= M,求解gcd(x,N)的和。

算法:

  由上题的知识能够知道,1...N的互质个数为欧拉函数值且其gcd仅仅能是N的因子。

所以,对于N = x * y。

我们仅仅要

求出x在y内的互质个数就好了,结果乘以x就是gcd = x的和了.

证明:

   SUM(gcd = x ) = 1*x + 2*x + 3*x ..... y*x

  所以。当gcd = x的时候仅仅要求出y的欧拉函数值就好了。

 

而一个数的因子又能够在sqrt(N)内求出。

 

#include 
#include
#include
#include
#include
using namespace std;typedef long long LL;int euler_phi(int n){ int m = sqrt(n + 0.5); int ans = n; for(int i = 2;i <= m;++i) if(0 == n % i){ ans = ans / i * (i - 1); while(0 == n % i) n /= i; } if(n > 1) ans = ans / n * (n - 1); return ans;}LL solve(int n,int m){ LL res = 0; int k = sqrt(n + 0.5); for(int i = 1;i <= k;++i){ if(0 == n % i){ if(i >= m) res += i * euler_phi(n / i); if(i != n / i && n / i >= m) res += n / i * euler_phi(i); } } return res;}int main(){ int n,m; while(~scanf("%d%d",&n,&m)){ printf("%lld\n",solve(n,m)); } return 0;}

 

                                                                                                  GCD(三)

 

题目:

    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 considering a little more difficult problem:
Given integers N and M,please answer sum of  X satisfies 1<=X<=N and (X,N)>=M.

 

算法:

   跟GCD(一)不同的是这题求得是满足gcd(x,n) >= m 。x的和。而由欧拉函数中的一个定理能够知道

 

所以。仅仅要SUM(n = x * y) = y*α(y) / 2 * x 

由于要的是x的和。而我们是在把X先进行X / x处理的所以最后要在乘回上x得到原值。

 

#include 
#include
#include
#include
#include
using namespace std;typedef long long LL;const int MOD = 1000000007;int euler_phi(int n){ int k = (int)sqrt(n + 0.5); int ans = n; for(int i = 2;i <= k;++i) if(0 == n % i){ ans = ans / i * (i - 1); while(0 == n % i) n /= i; } if(n > 1) ans = ans / n * (n - 1); return ans;}LL getFact(int n,int m){ LL res = 0; int k = sqrt(n + 0.5); LL tmp; for(int i = 1;i <= k;++i){ if(0 == n % i){ tmp = n / i; if(i >= m){ LL t1 = tmp * euler_phi(tmp) / 2 % MOD; t1 = t1 ? t1 : 1; res = (res + t1 * i) % MOD; } if(tmp >= m && i != tmp) { LL t1 = i * euler_phi(i) / 2 % MOD; t1 = t1 ?

t1 : 1; res = (res + t1 * tmp) % MOD; } } } return res >= MOD ? res%MOD : res; } int main() { int T,n,m; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); printf("%lld\n",getFact(n,m)); } return 0; }

 

你可能感兴趣的文章
微软驱动模块框架旨在简化Windows驱动开发
查看>>
2015年度最流行PHP框架调查结果出炉,Laravel居首
查看>>
WebAssembly得到了所有浏览器的支持
查看>>
Naresh Jain:合作的阴暗面
查看>>
为什么已有Elasticsearch,我们还要重造实时分析引擎AresDB?
查看>>
阿里巴巴收购以色列VR公司,大厂死磕VR为哪般?
查看>>
LLVM 4中将加入新的LLVM链接器LLD
查看>>
AWS EC2 Run Command特性新增多重云脚本
查看>>
进阶 Nginx 高手必须跨越的 5 座大山
查看>>
使用Xamarin实现跨平台移动应用开发
查看>>
webpack-dev-server 热替换配置详解
查看>>
随机森林算法4种实现方法对比测试:DolphinDB速度最快,XGBoost表现最差
查看>>
架构设计复杂度的6个来源
查看>>
如何成功地在亚洲植入敏捷和DevOps
查看>>
银行建中台跟阿里建中台有什么不同?
查看>>
实现AGI还要多久?Hinton与AlphaGo之父这样回答
查看>>
Atlassian的Stash数据中心为Git提供了高可用性及可伸缩性
查看>>
Adaptive Execution让Spark SQL更高效更好用
查看>>
Swift 烧脑体操(五)- Monad
查看>>
中国在两年内赶超美国AI?李开复:不一定
查看>>