HDU 2588 GCD GCD问题总结

2024-05-03 19:08
文章标签 问题 总结 hdu gcd 2588

本文主要是介绍HDU 2588 GCD GCD问题总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

                                                                                                             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 <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
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 <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
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 <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
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;
}

 

这篇关于HDU 2588 GCD GCD问题总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/957430

相关文章

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

Web服务器-Nginx-高并发问题

《Web服务器-Nginx-高并发问题》Nginx通过事件驱动、I/O多路复用和异步非阻塞技术高效处理高并发,结合动静分离和限流策略,提升性能与稳定性... 目录前言一、架构1. 原生多进程架构2. 事件驱动模型3. IO多路复用4. 异步非阻塞 I/O5. Nginx高并发配置实战二、动静分离1. 职责2

解决升级JDK报错:module java.base does not“opens java.lang.reflect“to unnamed module问题

《解决升级JDK报错:modulejava.basedoesnot“opensjava.lang.reflect“tounnamedmodule问题》SpringBoot启动错误源于Jav... 目录问题描述原因分析解决方案总结问题描述启动sprintboot时报以下错误原因分析编程异js常是由Ja

MySQL 表空却 ibd 文件过大的问题及解决方法

《MySQL表空却ibd文件过大的问题及解决方法》本文给大家介绍MySQL表空却ibd文件过大的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录一、问题背景:表空却 “吃满” 磁盘的怪事二、问题复现:一步步编程还原异常场景1. 准备测试源表与数据

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec

SysMain服务可以关吗? 解决SysMain服务导致的高CPU使用率问题

《SysMain服务可以关吗?解决SysMain服务导致的高CPU使用率问题》SysMain服务是超级预读取,该服务会记录您打开应用程序的模式,并预先将它们加载到内存中以节省时间,但它可能占用大量... 在使用电脑的过程中,CPU使用率居高不下是许多用户都遇到过的问题,其中名为SysMain的服务往往是罪魁

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十

MySQ中出现幻读问题的解决过程

《MySQ中出现幻读问题的解决过程》文章解析MySQLInnoDB通过MVCC与间隙锁机制在可重复读隔离级别下解决幻读,确保事务一致性,同时指出性能影响及乐观锁等替代方案,帮助开发者优化数据库应用... 目录一、幻读的准确定义与核心特征幻读 vs 不可重复读二、mysql隔离级别深度解析各隔离级别的实现差异