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. Intellij idea配置springMvc4.2.6

    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面. 环境: Intellij iead 2016.1 java version " ...

  2. Vi命令详解

    Vi有三种模式,分别为命令行模式.一般模式和编辑模式.在命令行输入“Vi 文件名”,即可进入Vi.常用命令如下:一.一般模式翻页[Ctrl]+[f]: 向下翻一页,相当于[Page Down]按键.[ ...

  3. js实现网页收藏功能,动态添加删除网址

    <html> <head> <title> 动态添加删除网址 </title> <meta charset="utf-8"&g ...

  4. 利用redis协助mysql数据库搬迁

    最近公司新项目上线,需要数据库搬迁,但新版本和老版本数据库差距比较大,关系也比较复杂.如果用传统办法,需要撰写很多mysql脚本,工程量虽然不大,但对于没有dba的公司来说,稍微有点难度.本人就勉为其 ...

  5. 参数解析argparse模块

    argparse,python的一个命令行解析模块 import argparse #创建一个命令行解析器 parser = argparse.ArgumentParser() #增添参数 parse ...

  6. Hibernate学习笔记--使用ThreadLocal

    参考资料: http://blog.sina.com.cn/s/blog_7ffb8dd5010146i3.html http://lavasoft.blog.51cto.com/62575/5192 ...

  7. iOS开发 自定义navigationleftItem 之后手势失效的问题

    @property (nonatomic, strong) UIViewController *currentShowVC; //设置代理 self.navigationController.inte ...

  8. UITableView常用属性和方法 - 永不退缩的小白菜

    UITableView常用属性和方法 - 永不退缩的小白菜 时间 2014-05-27 01:21:00  博客园精华区原文  http://www.cnblogs.com/zhaofucheng11 ...

  9. 转摘--如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等

    http://www.vaikan.com/use-multiple-cpu-cores-with-your-linux-commands/ 你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或 ...

  10. logstash match

    [elk@zjtest7-frontend config]$ cat stdin04.conf input { stdin { } } filter { # drop sleep events gro ...