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. ZOJ 1633

    迭代 每个数对应前面的一个数 #include<stdio.h> #include<iostream> using namespace std; #define max 88 ...

  2. .NET winform 在listview中添加progressbar

    找了好长时间没找到,后来索性自己写了一个: 首先,在往listview加载数据的事件里添加progressbar: foreach (string d in arr) { ; item = new L ...

  3. js 当前系统时间

    <script language=Javascript> function time(){ //获得显示时间的div t_div = document.getElementById('sh ...

  4. Linux的标准输出、标准错误输出、nohup

    1.在bash中标准输出可以用1来表示:通常来说这个1可以省略: 如./xxx >/dev/null 和 ./xxx 1>/dev/null 是一个意思 2.在bash中标准错误输出可以用 ...

  5. 解决ScrollView 与ListView共存显示不完全的问题

    ScrollView与ListView共存会存在滚动的问题,并且ListView只显示一个半Item. 当ListView的高度设定一定的值时,ListView同样地会显示对应的高度的Item. 因此 ...

  6. directive 指令

    参考文章 : http://www.zouyesheng.com/angular.html#toc20    18. 自定义指令directive http://blog.jobbole.com/62 ...

  7. 原型模式 - OK

    原型模式(Prototype),用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 简单说来原型模式就是从一个对象再创建另外一个可定制的对象,而且不需知道任何创建的细节. 原型模式UML ...

  8. UNIX网络编程--ioctl操作(十七)

    一.概述 在本书中有两个地方都对这个函数进行了介绍,其实还有很多地方需要这个函数.ioclt函数传统上一直作为纳西而不适合归入其他精细定义类别的特性的系统接口.网络程序(特别是服务器程序)经常在程序启 ...

  9. mysql sql优化<1>

    <pre name="code" class="html">explain SELECT t.* FROM ( SELECT t1.sn AS cl ...

  10. Android实现摇晃手机的监听

     摘自:http://blog.csdn.net/xwren362922604/article/details/8515343 监听摇晃手机的类: /**  * @author renxinwei ...