GCD

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

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
 题目大意:
数据量太大,用常规方法做是行不通的。后来看了别人的解题报告说,先找出N的约数x,

          并且gcd(x,N)>= M,结果为所有N/x的欧拉函数之和。

          因为x是N的约数,所以gcd(x,N)=x >= M;

   设y=N/x,y的欧拉函数为小于y且与y互质的数的个数。

   设与y互质的的数为p1,p2,p3,…,p4

   那么gcd(x* pi,N)= x >= M。

          也就是说只要找出所有符合要求的y的欧拉函数之和就是答案了。

至于为何用ans+=Euler(n/i0而不是直接加n/i,这便是为了查重,防止出现重复

只加满足gcd(k*x,n)==x的个数,不过如果直接遍历找约数遍历的量太大,同样会
TLE,因此可以把区间右坐标变成sqrt(n),同时判断i和n/i即可,特别注意num*num=n
的情况要单独处理。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
int kase=0;
LL Euler(LL n)
{
    LL ans=n;
    for(int i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            ans-=ans/i;
            while(n%i==0) n/=i;
        }
    }
  if(n>1) ans-=ans/n;
  return ans;
}
int main()
{
    int t;
    cin>>t;
    __int64 n,m;
    while(t--)
    {
        kase=0;
        scanf("%I64d%I64d",&n,&m);
        int num=(int)sqrt(n+0.5);
        //cout<<num<<endl;
        for(int i=1;i<num;i++)
        {
            if(n%i==0)
            {
                if(n/i>=m)
                kase+=Euler(i);
                if(i>=m)
                kase+=Euler(n/i);
            }
        }
        //cout<<kase<<endl;
       // cout<<Euler(100)<<endl;
        if(num*num==n&&num>=m) kase+=Euler(num);
        cout<<kase<<endl;
    }
    return 0;
}
 

hdu2588 gcd 欧拉函数的更多相关文章

  1. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

  2. 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 ...

  3. HDU 2588 GCD (欧拉函数)

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

  4. Bzoj-2818 Gcd 欧拉函数

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x ...

  5. BZOJ2818: Gcd 欧拉函数求前缀和

    给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 如果两个数的x,y最大公约数是z,那么x/z,y/z一定是互质的 然后找到所有的素数,然后用欧拉函数求一 ...

  6. 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 和 ...

  7. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. HDU 1695 GCD (欧拉函数,容斥原理)

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

  9. hdu 1695 GCD (欧拉函数+容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. css3画苹果logo

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. DEDE函数

    Html2text() 函数是去掉html标签代码. cn_substr(str,) 函数是截取字符串长度. 当然,他们也可以合并起来使用: [field:body function="cn ...

  3. centos上安装rabbitmq并且python测试

    把我的阿里云重置了一下,重新安装rabbitmq,看看会出现什么问题. 首先,把erlang环境安装一下,直接 yum list | grep erlang erlang.x86_64 R16B-03 ...

  4. android 集成百度地图

    一.下载百度地图为我们提供的所有DEMO. 在这里边我选的是一键下载. 二.下载后有两个项目一个是用于eclipse.另一个是android studio.我选的是android studio. 我用 ...

  5. Android学习笔记--存储方案(SharedPreference、文件IO)

    1. SharedPreference SharedPreference可以很容易的保存key-value对,通常用于保存配置信息.保存的步骤 1. 获得SharedPreferences对象 (最后 ...

  6. 【Beta】Scrum10

    Info 最后一次Scrum会议(补发) 时间:2017.01.03 21:35 时长:20min 地点:大运村1号公寓5楼楼道 类型:日常Scrum会议 NXT:-- Task Report Nam ...

  7. STL string常用操作指令

    s.insert(pos,args); 在pos之前插入args指定的字符.pos可以是一个下标或一个迭代器.接受下标的版本返回一个指向s的引用;接受迭代器的版本返回指向第一个插入字符的迭代器. s. ...

  8. 使用POI把Word Excel转为HTML

    此方法是针对Office2003的,但是word中如果有图片,图片能够解析出来但是HTML文件中不显示.也不支持excel中的图片解析. 所需jar包如下如下: 1:PoiUtil.java pack ...

  9. 使用NUnit进行项目的单元测试

    using System; using System.Collections; using System.Collections.Generic; using NUnit.Framework; nam ...

  10. ANDROID (eclipse)开发常见问题及解决办法汇总

    1.ANDROID 在eclipse中没有出现AVD的解决方法(转)如果android安装正确的话,但是eclipse里面的导航条就是没有AVD 可以通过「Window」⇒「Customize Per ...