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的和。而由欧拉函数中的一个定理能够知道
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhvbmdzaGlqdW5hY20=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">
所以。仅仅要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问题总结的更多相关文章
- hdu 5381 The sum of gcd(线段树+gcd)
题目链接:hdu 5381 The sum of gcd 将查询离线处理,依照r排序,然后从左向右处理每一个A[i],碰到查询时处理.用线段树维护.每一个节点表示从[l,i]中以l为起始的区间gcd总 ...
- hdu 5381 The sum of gcd 2015多校联合训练赛#8莫队算法
The sum of gcd Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) T ...
- hdu 5974 A Simple Math Problem gcd(x,y)=gcd((x+y),lcm(x,y))
题目链接 题意 现有\[x+y=a\\lcm(x,y)=b\]找出满足条件的正整数\(x,y\). \(a\leq 2e5,b\leq 1e9,数据组数12W\). 思路 结论 \(gcd(x,y)= ...
- HDU 5381 The sum of gcd (技巧,莫队算法)
题意:有一个含n个元素的序列,接下来有q个询问区间,对每个询问区间输出其 f(L,R) 值. 思路: 天真单纯地以为是道超级水题,不管多少个询问,计算量顶多就是O(n2) ,就是暴力穷举每个区间,再直 ...
- 2015 Multi-University Training Contest 8 hdu 5381 The sum of gcd
The sum of gcd Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 欧几里得算法:从证明等式gcd(m, n) = gcd(n, m mod n)对每一对正整数m, n都成立说开去
写诗或者写程序的时候,我们经常要跟欧几里得算法打交道.然而有没要考虑到为什么欧几里得算法是有效且高效的,一些偏激(好吧,请允许我用这个带有浓重个人情感色彩的词汇)的计算机科学家认为,除非程序的正确性在 ...
- iOS边练边学--GCD的基本使用、GCD各种队列、GCD线程间通信、GCD常用函数、GCD迭代以及GCD队列组
一.GCD的基本使用 <1>GCD简介 什么是GCD 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 GCD的优势 G ...
- HDU 2588 思维 容斥
求满足$1<=X<=N ,(X,N)>=M$的个数,其中$N, M (2<=N<=1000000000, 1<=M<=N)$. 首先,假定$(x, n)=m$ ...
- UVA 1642 Magical GCD(经典gcd)
题意:给你n(n<=100000)个正整数,求一个连续子序列使序列的所有元素的最大公约数与个数乘积最大 题解:我们知道一个原理就是对于n+1个数与n个数的最大公约数要么相等,要么减小并且减小至少 ...
- Solve Equation gcd(x,y)=gcd(x+y,lcm(x,y)) gcd(x,y)=1 => gcd(x*y,x+y)=1
/** 题目:Solve Equation 链接:http://acm.hnust.edu.cn/JudgeOnline/problem.php?id=1643 //最终来源neu oj 2014新生 ...
随机推荐
- VirtualBox – Error In supR3HardenedWinReSpawn
Genymotion 模拟器安装好虚拟机后,启动时报错: ————————— VirtualBox – Error In supR3HardenedWinReSpawn ————————— <h ...
- selenium _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0
在关闭driver时,如果用close,而不是用quit,会出现如下错误: Exception ignored in: <bound method Popen.__del__ of <su ...
- oracle分页sql模板
select t2.* from (select t1.*,rownum rn from (select * from mytest) t1 where rownum<=860010) t2 w ...
- 2017.6.8 spring-ldap基本使用总结
之前学习过spring-ldap的官方文档:2017.4.10 spring-ldap官方文档学习 现在是对实际使用的spring-ldap及使用过程中遇到的问题,进行总结. 1.spring-lda ...
- Linux SSH和SFTP服务分离
Linux SSH和SFTP服务分离 学习了:https://www.cnblogs.com/zihanxing/articles/5665383.html 都是监听22端口:
- Jenkins 安装卡住不动的解决方案
如果在安装jenkins时卡在getting startted的界面,如下所示 jenkins在安装插件前总是尝试连接www.google.com,来判断网络是否连通.谷歌的网站在大陆是连不上的,所以 ...
- [Angular] @ViewChild read custom directive and exportAs
For example we have a component: <Card ></Card> And a driective: <Card highlighted> ...
- 【VBA】获取模板保存的路径
使用VBA如何获取模板保存的路径呢?具体代码如下: Sub 获取Excle模板保存路径() MsgBox "获取Excle模板保存路径:" & Application.Te ...
- [1-6] 把时间当做朋友(李笑来)Chapter 6 【更多思考】 摘录
记住,你不可能百分之百地有效率,至少不可能总是百分之百地有效率. 他们的效率很差.根源在于,他们其实只做简单的事情,而回避那些有难度的工作. 好像丢钱包的人都不是“故意”丢的一样,办事拖拉的人大多并非 ...
- C#制作ActiveX控件中调用海康SDK的问题
事情是这样的,有一台海康威视的摄像头,客户需要一个ActiveX控件嵌入到网页中,通过点击按钮开始录制和结束录制来进行视频的录制和保存,关于海康摄像头的二次开发在此就不多说了,可以参考SDK中的说明. ...