HDU 2588 GCD(欧拉函数)
GCD
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3559 Accepted Submission(s): 1921
Problem Description
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.
Input
The first line of input is an integer T(T<=100) representing the number of test cases. The following T lines each contains two numbers N and M (2<=N<=1000000000, 1<=M<=N), representing a test case.
Output
For each test case,output the answer on a single line.
Sample Input
3
1 1
10 2
10000 72
Sample Output
1
6
260
题解:对于gcd(x,n)>=m;设gcd(a,b)=1,x=a*i,n=b*i,gcd(x,n)=i>=m,b=n/i,只需求小于等于b的与b互质的数的个数(也就是eular(b));
#include<iostream>
#include<algorithm>
#include<string.h>
#define ll long long
using namespace std;
ll eular(ll n)//欧拉函数模板
{
ll res=n;
for(ll i=2;i<=n;i++){
if(n%i==0){
res=res/i*(i-1);//p^i-p^(i-1)
while(n%i==0)
n/=i;
}
}
return n>1?res/n*(n-1):res;//最后不为1的情况
}
int main()
{
int T;
ll N,M;
scanf("%d",&T);
while(T--){
ll ans=0;
scanf("%lld%lld",&N,&M);
for(ll i=1;i*i<=N;i++){
if(!(N%i)){
if(i>=M)
ans+=eular(N/i);//gcd(x,m)=i>=m;
if(i*i!=N&&N/i>=M)//gcd(x,m)=n/i>=m;
ans+=eular(i);//i=N/(N/i);
}
}
printf("%lld\n",ans);
}
return 0;
}
HDU 2588 GCD(欧拉函数)的更多相关文章
- HDU 2588 GCD (欧拉函数)
GCD Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status De ...
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD 欧拉函数+容斥定理
输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...
- HDU 1695 GCD (欧拉函数,容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdu 1695 GCD (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 1695 GCD 欧拉函数 + 容斥
http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K] 和 [L ...
- HDU 1695 GCD 欧拉函数+容斥原理+质因数分解
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
- HDU 2824 简单欧拉函数
1.HDU 2824 The Euler function 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=2824 3.总结:欧拉函数 题意:求(a ...
- POJ 2773 Happy 2006【GCD/欧拉函数】
根据欧几里德算法,gcd(a,b)=gcd(a+b*t,b) 如果a和b互质,则a+b*t和b也互质,即与a互质的数对a取模具有周期性. 所以只要求出小于n且与n互质的元素即可. #include&l ...
随机推荐
- JDK源码笔记--Object
public final native Class<?> getClass(); public native int hashCode(); public boolean equals(O ...
- 鉴权完成后如何拿到SpringSocial的用户信息
⒈ package cn.coreqi.social.config; import org.springframework.beans.factory.annotation.Autowired; im ...
- 【转】Python-面向对象进阶
[转]Python-面向对象进阶 一.isinstance(obj, cls) and issubclass(sub, super) 1. isinstance(obj, cls),检查obj是否是类 ...
- 关于CaciiEZ端口流量阀值报警的设置
作者:邓聪聪 环境:CactiEZ v10.1 为了更高效的发现问题,在非工作期间,公司的网络可能会出现一些故障,为了及时解决问题,所以做了一个流量监控,并以邮件的方式发送流量异常的端口,以便及时了解 ...
- bootstrap Autocomplete
首先应用文件(已上传到文件,需要可自行下载) <link href="~/bower_components/select2/dist/css/select2.css" rel ...
- MySQL分组查询,前3条数据
SELECT tpn.news_id, tpn.title, tpn.news_group_id, tpn.send_date FROM t_pm_news tpn WHERE 3 > ( SE ...
- 11)django-ORM(操作增删改查)
ORM从增删改查等方面说明 一:创建数据 #创建数据两种方式1,推荐方式1 UserInfo.objects.create(username=") #方式1变种 user_dict={&qu ...
- 【进阶2-3期】JavaScript深入之闭包面试题解
这是我在公众号(高级前端进阶)看到的文章,现在做笔记 https://github.com/yygmind/blog/issues/19 作用域指的是一个变量和函数的作用范围,JS中函数内声明的所有变 ...
- 基于C#net4.5websocket客户端与服务端
只支持win8以上系统以及windows server2012以上系统 最近在研究视频传输给浏览器,然后使用H5标签解码.视频流采用websocket传输.所以研究了一下C#的websocket. 首 ...
- tp3.2 事务
public function exchangeTransfer($user_id, $type, $money, $config, $other_id = 0) { $r['code'] = ERR ...