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

相关文章

springboot3.4和mybatis plus的版本问题的解决

《springboot3.4和mybatisplus的版本问题的解决》本文主要介绍了springboot3.4和mybatisplus的版本问题的解决,主要由于SpringBoot3.4与MyBat... 报错1:spring-boot-starter/3.4.0/spring-boot-starter-

在 Spring Boot 中使用异步线程时的 HttpServletRequest 复用问题记录

《在SpringBoot中使用异步线程时的HttpServletRequest复用问题记录》文章讨论了在SpringBoot中使用异步线程时,由于HttpServletRequest复用导致... 目录一、问题描述:异步线程操作导致请求复用时 Cookie 解析失败1. 场景背景2. 问题根源二、问题详细分

解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题

《解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题》在Spring开发中,@Autowired注解常用于实现依赖注入,它可以应用于类的属性、构造器或setter方法上,然... 目录1. 为什么 @Autowired 在属性上被警告?1.1 隐式依赖注入1.2 IDE 的警告:

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

关于Nginx跨域问题及解决方案(CORS)

《关于Nginx跨域问题及解决方案(CORS)》文章主要介绍了跨域资源共享(CORS)机制及其在现代Web开发中的重要性,通过Nginx,可以简单地解决跨域问题,适合新手学习和应用,文章详细讲解了CO... 目录一、概述二、什么是 CORS?三、常见的跨域场景四、Nginx 如何解决 CORS 问题?五、基

MySQL安装时initializing database失败的问题解决

《MySQL安装时initializingdatabase失败的问题解决》本文主要介绍了MySQL安装时initializingdatabase失败的问题解决,文中通过图文介绍的非常详细,对大家的学... 目录问题页面:解决方法:问题页面:解决方法:1.勾选红框中的选项:2.将下图红框中全部改为英

Rust格式化输出方式总结

《Rust格式化输出方式总结》Rust提供了强大的格式化输出功能,通过std::fmt模块和相关的宏来实现,主要的输出宏包括println!和format!,它们支持多种格式化占位符,如{}、{:?}... 目录Rust格式化输出方式基本的格式化输出格式化占位符Format 特性总结Rust格式化输出方式

Nginx启动失败:端口80被占用问题的解决方案

《Nginx启动失败:端口80被占用问题的解决方案》在Linux服务器上部署Nginx时,可能会遇到Nginx启动失败的情况,尤其是错误提示bind()to0.0.0.0:80failed,这种问题通... 目录引言问题描述问题分析解决方案1. 检查占用端口 80 的进程使用 netstat 命令使用 ss

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作