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. windows启动、停止和重新启动Apache服务

    启动.停止和重新启动Apache服务(1) 在Windows操作系统中,Apache一般以服务的方式运行.在安装Apache时,如果你选择了“for all users”,Apache就会自动安装为一 ...

  2. Python新手学习基础之运算符——成员运算与身份运算

    成员运算符 Python里有成员运算符,可以判断一个元素是否在某一个序列中.比如可以判断一个字符是否属于这个字符串,可以判断某个对象是否在这个列表中等等. Python中的成员操作符的使用语法是: o ...

  3. hadoop SQL使用

    转载自:http://slaytanic.blog.51cto.com/2057708/782175 Hive 是facebook开源的一个基于hadoop框架的查询工具,也就是说,需要用hive的话 ...

  4. chinaunix:腾讯面试题

    腾讯电商面试回来,发现我太菜了,缺乏技术深度. 笔试题感觉不难,但是做了之后总是发觉少点什么东西,深度!这是这场面试总结出来的. 凭记忆分享下笔试,面试题.大家一起解决,然后分享下该看什么书,可以解决 ...

  5. C++异常处理小例

      学习程序的好方法是阅读代码和改进代码.下面的程例来自<An Overview of the C++ Programming Language>(5.1 异常和错误处理)程序用途:使用C ...

  6. LeetCode_Best Time to Buy and Sell Stock III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. 采购IC应该知道的十大网站

    http://www.hcsindex.com  (华强北指数网,查价格的)http://www.hqew.com  (华强电子网,针对华强北市场)http://www.dzsc.com  (维库电子 ...

  8. QT中窗口刷新事件的学习总结

    一.主要理解一下几个方法和属性: 1.QWidget * QScrollView::viewport () const 2.void QWidget::paintEvent ( QPaintEvent ...

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

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

  10. linux vmstat使用说明

    FIELD DESCRIPTION FOR VM MODE Procs r: The number of processes waiting for run time. 表示运行队列,就是说多少个进程 ...