任意门: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. js验证港澳居民通行证号码是否合规

    需求:最近要做实名验证的功能,但是验证我们要验证严谨一点,参考了网上关于验证港澳居民通行证号码的代码,总结一下. 代码: function checkHKMacao(code){ var tip = ...

  2. [转] Entity Framework添加记录时获取自增ID值

    本文转自:http://blog.csdn.net/educast/article/details/8632806 与Entity Framework相伴的日子痛并快乐着.今天和大家分享一下一个快乐, ...

  3. .Net Core GB2312编码问题

    1.今天抓取了一个网页的源代码.发现中文是乱码的,马上第一反应是编码问题..... 2.仔细一看基于WebClient写的代码,还真的是没有设置编码... /// <summary> // ...

  4. 使用多说评论&加网分享

    多说评论: <div data-thread-key=" class="ds-thread"></div><script>var du ...

  5. ajax实现菜单联动显示信息(当选择单位的时候,动态关联出人员信息)

    在jsp页面中使用onchange属性调用下面的方法: 在script中写入: function fromid(){ var from_id = $("#from_id").val ...

  6. 嵌套Golang对象的初始化

      比如有这样一个对象: type ProductConfig struct {     Site map[string]string } 对应的初始化可以如下写: var pc ProductCon ...

  7. VBA将指定Excel表数据批量生成到另一个Excel表中,每个sheet表一行数据

    Sub AutoInputValNewExcel() Dim sh1, sh2 As Worksheet Dim ws1, ws2 As Workbook ) ) ).Sheets() iRows = ...

  8. vue-cli脚手架项目实例

    看完了配置,接下来通过一个实例,更清晰地了解这些文件之间的联系,顺带练习练习vue相关知识. 1.安装 打开命令行控制器,系统自带cmd或者git bash等都可以,按照顺序输入如下指令,耐心等待每一 ...

  9. sass判断语句

    @if判断 @if可一个条件单独使用,也可以和@else结合多条件使用 scss.style css.style 三目判断 语法为:if($condition, $if_true, $if_false ...

  10. Python基础-map/reduce/filter

    一.map Python内置函数,用法及说明如下: class map(object): """ map(func, *iterables) --> map obj ...