这种问题一般都是给出限制条件:给你一个数N(N一般很大),使得在1~N之间能够找到X使得X满足gcd( X ,  N  ) >= M,然后求解相关的问题。

         分析:这是一种统计类型的问题。比较容易想到的解法就是枚举gcd(X,N)的值,对于枚举到的某个 gcd(X,N) 的值 d,如果令 N = p * d,X = q * d,那么如果 gcd(X,N) = d,一定有 p,q 互质,又有 X <= N,则 q <= p,而这样的 q 的个数正好对应p的欧拉函数,即满足gcd(X,N) = d 的X的个数为N/d 的欧拉函数值。
应用1:求满足条件的X的个数。http://acm.hdu.edu.cn/showproblem.php?pid=2588

GCD

                                                                             Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem 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 considering a little more difficult problem:
Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.
Input
The first line of input is an integer T(T<=100) representing the number of test cases. The following T lines each contains two numbers N and M (2<=N<=1000000000, 1<=M<=N), representing a test case.
Output
For each test case,output the answer on a single line.
Sample Input
3 1 1 10 2 10000 72
Sample Output
1 6 260
分析:对于这个问题,因为只需要求出满足题意的X的个数,所以可以枚举最大公约数d,而满足gcd(X,N) = d 的X的个数就是N/d的欧拉函数,把这些d对应的N/d的欧拉函数值求和即可。
#include<stdio.h>
#include<math.h>int euler(int n)
{int m = (int)sqrt(n+0.5);int ans = n;for(int i = 2; i <= m; i++)if(n % i  == 0){ans = ans / i * (i - 1);while(n % i == 0)n /= i;}if(n > 1)ans = ans / n * (n-1);return ans;}int main()
{int t, n, m;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);int ans = 0;for(int i = 1; i * i <= n; i++) //枚举最大公因数{if(n % i == 0){if(i >= m)ans += euler(n/i);if(i * i != n && n / i >= m)ans += euler(i);}}printf("%d\n", ans);}return 0;
}

应用2:求满足条件的gcd(X,N)的和。http://acm.nyist.net/JudgeOnline/problem.php?pid=998

Sum

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述

给你一个数N,使得在1~N之间能够找到x使得x满足gcd( x ,  N  ) >= M,

求解gcd(x,N)的和

输入
多组测试数据

每行输出两个数N,M(N,M不超int)

输出
输出sum
样例输入
5 3
样例输出
5

分析:这个题要求gcd(X,N)的和,因为上一题已经求出了满足题意的个数,所以只需要在上一题的基础上乘以最大公约数就是最终答案。

#include<stdio.h>
#include<math.h>
typedef long long LL;LL euler(LL n) //n的欧拉函数值
{LL ans = n;for(LL i = 2; i * i <= n; i++)if(n % i  == 0){ans = ans / i * (i - 1);while(n % i == 0)n /= i;}if(n > 1)ans = ans / n * (n-1);return ans;}int main()
{LL n, m;while(~scanf("%lld%lld",&n,&m)){LL ans = 0;LL tmp = (LL)sqrt(n+0.5);for(LL i = 1; i <= tmp; i++) //枚举最大公因数{if(n % i == 0){if(i >= m)ans += euler(n/i) * i;if(i * i != n && n / i >= m)ans += euler(i) * (n / i);}}printf("%lld\n", ans);}return 0;
}

应用3:求所有满足条件的X的和。http://acm.nyist.net/JudgeOnline/problem.php?pid=1007

GCD

时间限制:1000 ms  |  内存限制:65535 KB
难度:3

描述
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.

输入
The first line of input is an integer T(T<=100) representing the number of test cases. The following T lines each contains two numbers N and M (1<=N<=10^9, 1<=M<=10^9), representing a test case.
输出
Output the answer mod 1000000007
样例输入
3
1 1
10 2
10000 72
样例输出
1
35
1305000

分析:这个题和前两个题基本上一样,只需要在枚举最大公约数d时,求出gcd(X,N) = d 的所有X的和即可。根据上面可以得出:满足条件的X个数有euler(N/d)个,所以只需要求出不超过N/d且与N/d互素的那些数的和,然后乘以d就是最大公约数为d时对应的部分结果。而不超过N/d且与N/d互素的那些数的和 = N/d * euler(N/d) / 2,注意当N/d = 1时,结果是1而不是0。了解了这些,就可以解决这个题了。

除了1、2以外,所有数的欧拉函数都是偶数。

如果k <= n 并且 (k,n) = 1, 那么(n-k, n) = 1;

#include<stdio.h>
#define mod 1000000007
typedef long long LL;LL euler(LL n) //n的欧拉函数值
{LL ans = n;for(LL i = 2; i * i <= n; i++)if(n % i  == 0){ans = ans / i * (i - 1);while(n % i == 0)n /= i;}if(n > 1)ans = ans / n * (n-1);return ans;}LL euler_sum(LL n) //求和n互素的数的和
{if(n == 1) return 1;return n * euler(n) / 2;
}int main()
{int t;LL n, m;scanf("%d",&t);while(t--){scanf("%lld%lld",&n,&m);LL ans = 0;for(LL i = 1; i * i <= n; i++) //枚举最大公因数{if(n % i == 0){if(i >= m)ans = (ans + euler_sum(n/i) * i) % mod;if(i * i != n && n / i >= m)ans = (ans + euler_sum(i) * (n / i)) % mod;}}printf("%lld\n", ans);}return 0;
}

欧拉函数和最大公约数的组合应用相关推荐

  1. 51nod 1040最大公约数和(欧拉函数)

    1040 最大公约数之和 题目来源: rihkddd 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 给出一个n,求1-n这n个数,同n的最大公约数 ...

  2. 51nod 1188 最大公约数之和 V2(欧拉函数)

    1188 最大公约数之和 V2 思路 用欧拉函数可以化简式子如下 ∑i=1n∑j=1i−1gcd(i,j)\sum_{i = 1} ^{n} \sum _{j = 1} ^{i - 1} gcd(i, ...

  3. 51nod1040 最大公约数之和,欧拉函数或积性函数

    1040 最大公约数之和 给出一个n,求1-n这n个数,同n的最大公约数的和.比如:n = 6时,1,2,3,4,5,6 同6的最大公约数分别为1,2,3,2,1,6,加在一起 = 15 看起来很简单 ...

  4. 数论 GCD 最大公约数 欧拉函数经典题 洛谷 CF1295D Same GCDs Codeforces1295D

    ​前言 两个月了,我终于更了-- 这两个月忙(chen)于(mi)内(xiang)卷(le),现在终于出新文章啦,(也算兑现了当初的出数论题文章的承诺)~ 不说废话了,今天给大家介绍一道CF/洛谷上的 ...

  5. AcWing 220. 最大公约数 (欧拉函数)

    AcWing 220. 最大公约数 题意 给一个整数 n(1≤n≤107)n(1\le n \le 10^7)n(1≤n≤107) ,问 1≤x,y≤n1\le x,y\le n1≤x,y≤n 且 g ...

  6. 欧拉函数的相关应用 noj欧拉函数求和+noj 最大公约数求和

    注意求欧拉函数之和是每个因子的欧拉函数之和不是质因子.而欧拉函数的值是它本身与它的因子件事互质的关系,这样的因子有多少个.点击打开链接 #include<stdio.h> #include ...

  7. BZOJ 2818: Gcd区间内最大公约数 为素数的对数(欧拉函数的应用)

    传送门 2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB Submit: 3649 Solved: 1605 [Submit][Status][Dis ...

  8. bzoj 2818 欧拉函数

    思路:就是对于某个数q,跟他互质的数p,kp和kq的最大公约数是k,那么这个数能组成的答案的数量就是phi[i]乘以某个质数,且乘积小于n 基于这种思路写下这个代码 #include <cstd ...

  9. 【数学专题】约数个数与欧拉函数

    整理的算法模板合集: ACM模板 目录 一.约数个数 1. AcWing 1291. 轻拍牛头 2. AcWing 1294. 樱花 2.1 AcWing 197. 阶乘分解 3. AcWing 19 ...

最新文章

  1. 什么!在CSS中的重要意义? [重复]
  2. targetcli读取prefs.bin出错的问题的解决方法
  3. 安装Synchronization service (Project Server 2007) 时出现 MSMQ 错误的解决
  4. 1642: [Usaco2007 Nov]Milking Time 挤奶时间(dp)
  5. Chrome调试大全--转载
  6. SQL语言之DQL语言学习(八)多表查询/链接查询 SQL92学习
  7. python给定一个整数n、判断n是否为素数_输入一个大于3的整数n,判断它是否为素数...
  8. 【原创】关于移动铁通某些网站打不开的问题
  9. Web之CSS开发技巧: CSS @media
  10. 2014年3月新鲜出炉的最佳 JavaScript 工具库
  11. 理解OpenCL数据类型
  12. 强大的视频格式转换工具——iSkysoft iMedia Converter Deluxe Mac
  13. matlab $r$n$m,维纳滤波器推导以及MATLAB代码(Wiener Filter)
  14. 网易评论盖楼的数据结构
  15. rem布局百分比与rem的比例换算
  16. android 直播推流sdk,Android——直播推流SDK
  17. 人工智能有哪些方向?什么方向有前景?
  18. html添加外链图片代码,web 外链图片 403 解决方案(http referrer)
  19. Unity3D 2021.1.1F1。更新与下载。
  20. 第一次.......

热门文章

  1. Bitmap之位图采样和内存计算详解
  2. virtualbox+vagrant学习-2(command cli)-20-vagrant suspend命令
  3. 【VMCloud云平台进阶篇】Monitor监控(一)
  4. Gartner:移动设备在工作场所中的使用尚未成熟
  5. centos 下载为firefox安装flash插件
  6. 算了一挂,也不知准不准
  7. JAVA基础知识(6)
  8. JAVA基础知识(3)
  9. sainsbury newspaper collection time
  10. how to become an expert in excel