任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2588

GCD

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3608    Accepted Submission(s): 1954

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
 
Source

题意概括:

求 1~N 的范围内存在多少个 X 使得 GCD( X, N ) >= M;

解题思路:

设 s = GCD( X, N);

可知: s >= M,

且存在 a, b 使得 s*a = X, s*b = N, GCD( a, b ) = 1;

因为 X <= N 所以 a <= b;

综上所述:

N 1e9 的范围缩小一半枚举 s ,求得 b;(因为可以同时求得 i 和 N/i 的方案数)

即求满足 GCD(a, b) = 1 且 a <= b 的 a 的个数。

AC code:

 #include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std; const int MAXN = 1e9+;
LL N, M; LL Euler(LL n)
{
LL res = n;
for(LL i = ; i*i <= n; i++){
if(n%i == ) res = res/i*(i-);
while(n%i == ) n/=i;
}
if(n > ) res = res/n*(n-);
return res;
} int main()
{
int T_case;
scanf("%d", &T_case);
LL ans, b;
while(T_case--){
scanf("%lld %lld", &N, &M);
ans = ;
for(LL s = ; s*s <= N; s++){
if(N%s) continue;
if(s >= M) ans+=Euler(N/s);
if(s*s != N && N/s >= M) ans+=Euler(s);
}
printf("%lld\n", ans);
}
return ;
}

HDU 2588 GCD 【Euler + 暴力技巧】的更多相关文章

  1. HDU 2588 GCD

    题目大意:给定N,M, 求1<=X<=N 且gcd(X,N)>=M的个数. 题解:首先,我们求出数字N的约数,保存在约数表中,然后,对于大于等于M的约数p[i],求出Euler(n/ ...

  2. HDU——2588 GCD

    题目大意: 求1~N中与N的最大公约数大于M的个数 思路: 这个题是不是可以想到暴力枚举??对于每一组数据枚举与他的最大公约数大于m的数的个数. 是,这种做法没错误,但是保准你T成狗.... 我们至少 ...

  3. HDU 2588 GCD (欧拉函数)

    GCD Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

  4. HDU 2588 GCD(欧拉函数)

    GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  5. HDU 2588 GCD &amp;&amp; GCD问题总结

    GCD(一) 题目: The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written ( ...

  6. 题解报告:hdu 2588 GCD(欧拉函数)

    Description The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written ...

  7. HDU 4509 湫湫系列故事——减肥记II(线段树-区间覆盖 或者 暴力技巧)

    http://acm.hdu.edu.cn/showproblem.php?pid=4509 题目大意: 中文意义,应该能懂. 解题思路: 因为题目给的时间是一天24小时,而且还有分钟.为了解题方便, ...

  8. hdu 2824 The Euler function(欧拉函数)

    题目链接:hdu 2824 The Euler function 题意: 让你求一段区间的欧拉函数值. 题解: 直接上板子. 推导过程: 定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质 ...

  9. HDU 5726 GCD 区间GCD=k的个数

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

随机推荐

  1. WAMP环境配置-Apache服务器的安装

    一.下载 下载地址:http://httpd.apache.org/ 在这里就可以下载想下载的版本了 二.安装 我这次环境配置安装的是Apache-2.4.23版本! (最近我在反复安装PHP的时候出 ...

  2. 开窗函数over()

    使用方法 如:select name,avg(shengao)from xinxi group by name //我们都知道使用聚合函数要使用分组,如果不分组怎么办 Selct name,avg(s ...

  3. 2017年12月17日 ASP.NET 12个表单元素&&简单控件/复合控件

    12个表单元素可以分为三大类 第一类:文本类 <input type = "text" /> //普通文本框 <input type = "passwo ...

  4. child_process

    child_process const { spawn } = require('child_process'); const ls = spawn('ls', ['-lh', '/usr']); l ...

  5. sql left join 字符串

    select * FROM table1 as t1 right join (select '1,2,3,4,5'  as t) as tt on t1.Id=tt.t select * FROM t ...

  6. _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明

    原文:http://www.cnblogs.com/lidabo/archive/2012/07/10/2584706.html _itoa 功能:把一整数转换为字符串 用法:char * _itoa ...

  7. Android MediaPlayer播放音乐并实现进度条

    提前工作,往sd卡里放音乐文件 1.布局文件main.xml <?xml version="1.0" encoding="utf-8"?> < ...

  8. SVN 代码托管

    1.安装服务 使用yum安装subversion,简单.不繁琐. 1 yum install -y subversion 2.创建版本库 1 2       mkidr /svn/obj        ...

  9. input输入框数字验证

    正则限制input只能输入大于零的数字:onkeyup="this.value=this.value.replace(/\D|^0/g,'')" onafterpaste=&quo ...

  10. 【Leetcode】【Medium】Partition List

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...