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. SQL Server 系统函数

    一组内置函数,对 SQL Server 中的值.对象和设置执行操作,并返回有关它们的信息.   系统函数 功能 APP_NAME() 返回当前会话的应用程序名称(如果应用程序进行了设置) CASE表达 ...

  2. JSON-JSON 百科

    1,百度百科 http://baike.baidu.com/view/136475.htm?fr=aladdin 2,JSON教程 http://www.w3school.com.cn/json/

  3. CSU - 1542 Flipping Parentheses (线段树)

    CSU - 1542 Flipping Parentheses Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld ...

  4. java gc log

    java full gc 经常带来延迟, 导致性能问题 如下命令使java虚拟机记录gc的log到文件, 帮助分析定位问题. java -Xloggc:./a.log -jar a.jar    // ...

  5. java-selenium(二)富文本编辑框的处理

    首先先看一下什么是富文本编辑框 HTML源码 思路:首先先进入到iframe中,再用js写,最后切出iframe 如果想要换行可以在换行的地方加上<br> 如果添加的文本中包含单引号.双引 ...

  6. 转:大数据 2016 landscape

    如图:

  7. PS如何拉倒影效果

    1 复制图形(一般是文字)并垂直翻转得到倒影的初步样子(最好倾斜一下,看起来逼真一些)就像下面的迅雷的样子.记住要栅格化文字. 2 用魔棒工具抠除原来的颜色,只剩下空的选区.   3 拉渐变

  8. 使用LBS(地理位置)

    一.使用之前的简单封装的OkHttpUtil http://www.cnblogs.com/itfenqing/p/6758909.html 二.关键代码: public class MainActi ...

  9. Activity的启动流程分析

    Activity是Android应用程序的四大组件之中的一个,负责管理Android应用程序的用户界面,一般一个应用程序中包括非常多个Activity,他们可能执行在一个进程中.也可能执行在不同的进程 ...

  10. STL学习笔记(迭代器相关辅助函数)

    advance()可令迭代器前进 #include <iterator> void advance(InputIterator& pos,Dist n); 面对Random Acc ...