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新生 ...
随机推荐
- andriod 剪贴板操作
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- [置顶]
kubernetes资源类型--deployment
Deployment(中文意思为部署.调度)提供了一种更加简单的更新RC和Pod的机制,K8S版本1.2实现的.通过在Deployment中描述所期望的集群状态,Deployment Controll ...
- react数组key的唯一性
1.不要使用数组的index索引作为key 2.在相邻的元素间,一定确保key的唯一性,如果出现了相同的 key,会抛出一个 Warning,告诉相邻组件间有重复的 key 值.并且只会渲染第一个重复 ...
- Xamarin.Forms 调用腾讯地图
用Xamarin.Forms 也有一段时间了,跨平台,生成native代码的噱头 天花乱坠的, 其中的坑,只有用过的人才懂... 就拿地图来说...总不能用google地图吧 于是只能自己想办法了. ...
- python urllib2使用细节
刚好用到,这篇文章写得不错,转过来收藏. 转载自 道可道 | Python 标准库 urllib2 的使用细节 Python 标准库中有很多实用的工具类,但是在具体使用时,标准库文档上对使用细节 ...
- LLVM每日谈之二十 Everything && Clang driver
作者:史宁宁(snsn1984) 近期在读<Getting Started with LLVM Core Libraries>.这是读的第一本LLVM的书.非常多地方尽管讲的是自己知道的东 ...
- PS 不能使用移动工具 因为目标图层被隐藏怎么办
photoshop任何图层都无法移动或者修改,提示目标通道被隐藏. 在ps任何图层都无法移动并且无法使用橡皮擦等修改工具,提示目标通道被隐藏.我看了下通道里面多了一个快速蒙版层(并且是被隐藏的),我打 ...
- 一扫天下——ZXing使用全解析
一扫天下--ZXing使用全解析 二维码如今已经烂App了,无论什么App.没有二维码就好像低人一等了. 所以,在自己的项目中集成二维码功能还是非常有必要的. 网上非常多都是基于ZXing2.3的.可 ...
- 渐进式 JPEG (Progressive JPEG)来提升用户体验
1.概述 jpg格式分为:Baseline JPEG(标准型)和Progressive JPEG(渐进式).两种格式有相同尺寸以及图像数据,扩展名也是相同的,唯一的区别是二者显示的方式不同. Base ...
- 【BIEE】页面跳转以及跳转后返回
报表开发过程中,我们经常会遇到这种问题:知道统计结果,然后根据统计结果去看明细数据 很多人可能首先想到的就是钻探,钻探是一种方法,但是不是唯一的办法,可以使用页面跳转完成. 下面举个例子 页面A 现在 ...