hdu2588-GCD-(欧拉函数+分解因子)
(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.
InputThe 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.OutputFor each test case,output the answer on a single line.Sample Input
3
1 1
10 2
10000 72
Sample Output
1
6
260
翻译:给出n和m,1<=x<=n,求x符合gcd(x,n)>=m有多少个。
解题过程:
令d=gcd(x,n),显然d是n的因子,并且是x和n的最大公因子,则gcd(x/d,n/d)=1
对于每个d,令y=n/d,找有多少个x/d满足gcd(x/d,y)=1。
欧拉函数登场,累加y的欧拉函数值。
#include <iostream>
#include<stdio.h>
#include <algorithm>
#include<string.h>
#include<cstring>
#include<math.h>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std; ll euler(ll x)
{
ll res=x;
for(ll i=;i*i<=x;i++)
{
if(x%i==)
{
res=res/i*(i-);
while(x%i==)
x=x/i;
}
}
if(x>)
res=res/x*(x-);
return res;
} int main()
{ ll t,n,m,sum;
scanf("%lld",&t);
while(t--)
{
sum=;
scanf("%lld%lld",&n,&m);
int q=sqrt(n);
ll i;
for(i=;i*i<=n;i++)
{
if(n%i==)
{
if(i>=m)
sum=sum+euler(n/i);
if((n/i)>=m)
sum=sum+euler(i);
}
}
i--;
if(i*i==n && i>=m)
sum=sum-euler(i);
printf("%lld\n",sum);
}
return ;
}
hdu2588-GCD-(欧拉函数+分解因子)的更多相关文章
- hdu2588 gcd 欧拉函数
GCD Time Limit: 2000/1000 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 ...
- BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 4436 Solved: 1957[Submit][Status][Discuss ...
- 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 ...
- 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 ...
- Bzoj-2818 Gcd 欧拉函数
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x ...
- BZOJ2818: Gcd 欧拉函数求前缀和
给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 如果两个数的x,y最大公约数是z,那么x/z,y/z一定是互质的 然后找到所有的素数,然后用欧拉函数求一 ...
随机推荐
- [Ahoi2013]差异(后缀自动机)
/* 前面的那一坨是可以O1计算的 后面那个显然后缀数组单调栈比较好写??? 两个后缀的lcp长度相当于他们在后缀树上的lca的深度 那么我们就能够反向用后缀自动机构造出后缀树然后统计每个点作为lca ...
- MySQL ERROR 1300 (HY000): Invalid utf8 character string
load data报错 原因:原始数据含有 中文\中文 这样的带斜线的字符串. 解决方案:导出的时候替换 replace(d.role_name,'\\','.' ),这样导入时候就不用处理了
- django-BaseCommand自带的权限分组
note: 应该是这样的结构,并且commands名字是固定的. 执行: python manage.py initgroup initgroup.py # -*- coding: utf-8 - ...
- 22.天眼查cookie模拟登陆采集数据
通过账号登录获取cookies,模拟登录(前提有天眼查账号),会员账号可查看5000家,普通只是100家,同时也要设置一定的反爬措施以防账号被封.拿有权限的账号去获取cookies,去访问页面信息,不 ...
- Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
转自:https://www.zhihu.com/question/20948649?sort=created 我最近也遇到这个问题了,用传统的快捷方式加参数并没有用,不知道是不是和chrome版本有 ...
- selenium报错
Python 2.7.15 selenium 2.53.6 Firefox 47.0.1 pycharm 2017.3.7 # coding:utf-8 from selenium import ...
- mysql root密码修改
1.假如之前没设置密码,现在想成test mysqladmin -u root password test 2.假如之前密码是1234,现在想改成test mysqladmin -u root -p1 ...
- 电脑组装DIY
技嘉主板:B150M-D3H 网卡驱动: CPU: 风扇: 机箱: 显示器:
- 浅谈transient关键字
1,用途 当一个对象实现了Serilizable接口,这个对象就可以被序列化.而有时候我们可能要求:当对象被序列化时(写入字节序列到目标文件)时,有些属性需要序列化,而其他属性不需要被序列化,打个比方 ...
- day06-三元表达式
python中没有其他语言中的三元表达式,不过有类似的实现方法 其他语言中,例如java的三元表达式是这样int a = 1;String b = "";b = a > 1? ...