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一定是互质的 然后找到所有的素数,然后用欧拉函数求一 ...
随机推荐
- How Computers Boot Up.计算机的引导过程
原文标题:How Computers Boot Up 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高手的精彩文章翻译一下.一来自己复习 ...
- solr 忽略大小写
1.types标签下加入如下fieldType <fieldType name="str_lower" class="solr.TextField" so ...
- oracle比较一行的最大值或最小值
1. COALESCE 返回该表达式列表的第一个非空value. 格式: COALESCE(value1, value2, value3, ...) 含义: 返回value列表第一个非空的值. val ...
- 【Selenium-WebDriver自学】Selenium网格化(十六)
参考链接: http://www.yiibai.com/selenium/selenium_grids.html 命令: cd E:\Yingpu\000.SVNYP\01.个人文件夹\Renqian ...
- Ruby学习笔记2 : 一个简单的Ruby网站,搭建ruby环境
Ruby on Rails website 的基础是 请求-返回 循环. 首先是浏览器请求服务器, 第二步,Second, in our Rails application, the route ta ...
- 10.纯 CSS 创作一个同心圆弧旋转 loader 特效
原文地址:https://segmentfault.com/a/1190000014682999 想到了扇形:正方形 ->border-radius: 50%; ->取四份中的任意一份. ...
- requestAnimFrame动画兼容性封装
window.requestAnimFrame=function(){ return window.requestAnimationFrame ||window.webkitRequestAnimat ...
- div的全屏与退出全屏
div的全屏与退出全屏 作用:将div全屏与退出全屏,一般播放器使用较多. html按钮: <button onclick="showFull();"> 全屏 < ...
- 关于webstorm链接不上SVN的解决办法
使用WEBSTROM上传代码是很方便的,但是通过它调用SVN时,经常会出现问题,我在使用它调用TortoiseSVN时就出现了一些问题,好在问题已经解决,现在把解决办法分享给大家: 首先,看看,安装时 ...
- Java中,&&与&,||与|的区别
在java的逻辑运算符中,有这么四类:&&(短路与),&,|,||(短路或). &&和&都是表示与,区别是&&只要第一个条件不满足,后面 ...