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 &amp;&amp; GCD问题总结的更多相关文章

  1. hdu 5381 The sum of gcd(线段树+gcd)

    题目链接:hdu 5381 The sum of gcd 将查询离线处理,依照r排序,然后从左向右处理每一个A[i],碰到查询时处理.用线段树维护.每一个节点表示从[l,i]中以l为起始的区间gcd总 ...

  2. 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 ...

  3. 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)= ...

  4. HDU 5381 The sum of gcd (技巧,莫队算法)

    题意:有一个含n个元素的序列,接下来有q个询问区间,对每个询问区间输出其 f(L,R) 值. 思路: 天真单纯地以为是道超级水题,不管多少个询问,计算量顶多就是O(n2) ,就是暴力穷举每个区间,再直 ...

  5. 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 ...

  6. 欧几里得算法:从证明等式gcd(m, n) = gcd(n, m mod n)对每一对正整数m, n都成立说开去

    写诗或者写程序的时候,我们经常要跟欧几里得算法打交道.然而有没要考虑到为什么欧几里得算法是有效且高效的,一些偏激(好吧,请允许我用这个带有浓重个人情感色彩的词汇)的计算机科学家认为,除非程序的正确性在 ...

  7. iOS边练边学--GCD的基本使用、GCD各种队列、GCD线程间通信、GCD常用函数、GCD迭代以及GCD队列组

    一.GCD的基本使用 <1>GCD简介 什么是GCD 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数   GCD的优势 G ...

  8. HDU 2588 思维 容斥

    求满足$1<=X<=N ,(X,N)>=M$的个数,其中$N, M (2<=N<=1000000000, 1<=M<=N)$. 首先,假定$(x, n)=m$ ...

  9. UVA 1642 Magical GCD(经典gcd)

    题意:给你n(n<=100000)个正整数,求一个连续子序列使序列的所有元素的最大公约数与个数乘积最大 题解:我们知道一个原理就是对于n+1个数与n个数的最大公约数要么相等,要么减小并且减小至少 ...

  10. 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新生 ...

随机推荐

  1. XCTest(一)

    target:  + 来添加target,可以选择工程中不同的target目标来添加 class: +来添加class, 可以按照不同的测试功能来分类,比如NormalFunctionTest, UI ...

  2. Android反编译调试源码

    Android反编译调试源码 1. 反编译得到源码 直接在windows 命令行下输入命令java -jar apktool_2.0.0.jar d -d 小米运动_1.4.641_1058.apk ...

  3. linux /boot目录下的文件分析

    一. Linux 启动流程 首先说一下Linux系统大概的启动过程: 1. 主机加电后, 系统首先加载BIOS, 这个BIOS是以写在主板上的. 2. BIOS启动后,执行一些例如开机自检,硬件初始化 ...

  4. 如何用PS快速的批量制作连续号码数字编号图解

    如何用PS快速的批量制作连续号码数字编号图解 大家好,今天太原博飞设计培训小编就告诉大家如用PS快速的制作连续数字编号,在工作中尤其是大型活动的有时候制作连续的号码牌,少还好,如果上百上千个,那就辛苦 ...

  5. rational rose画UML图

    原文见:http://blog.csdn.net/cjr15233661143/article/details/8532997 UML是一种建模语言,是系统建模的标准.我们之所以建模是因为大规模的系统 ...

  6. Socket协议通讯

    Socket协议通讯   服务器端代码: using System; using System.Collections.Generic; using System.ComponentModel; us ...

  7. 使用itchat分析自己的微信(1)

    1.准备工作 安装itchat pip install itchat参考 itchat内部函数 2.个人微信男女比例分析 ''' Have fun:itchat Author:杨 景 Time:201 ...

  8. ganlia安装配置文档

    gangliaz在ubuntu中安装和配置很简单 1.  服务器端安装 sudo apt-get install ganglia-monitor ganglia-webfrontend rrdtool ...

  9. PrincetonUniversity-Coursera 算法:算法简单介绍

    Course Overview What is this course? Intermediate-level survey course. Programming and proble solvin ...

  10. java中利用WeakHashMap实现缓存

    简介 WeakHashMap是Java集合框架里的一员,从名字可以看出它是某种 Map.它的特殊之处在于 WeakHashMap 里的entry可能会被GC自动删除,即使程序员没有调用remove() ...