【数论】(欧拉函数)GCD HDU2588


描述

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.

输入

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.

输出

For each test case,output the answer on a single line.

样例输入

3
1 1
10 2
10000 72

样例输出

1
6
260

分析思路:首先,我们假设X是满足gcd(X,N)=a并且a>=m,则gcd(X/a,N/a)=1。也就是说,找到多少个X/a与N/a互质(典型的欧拉函数应用),就找到多少个X满足题目要求。因为a是不确定的,但是可以确定a是N的因子,所以我们枚举所有因子,然后加上这些因子的欧拉函数即可。

#include <iostream>
#include <cstdio>
using namespace std;
int phi(int n){
    int rea=n;
    for(int i=2;i<=n;i++){
        if(n%i==0){
            rea=rea-rea/i;
            do
            n/=i;
            while(n%i==0);
        }
    }
    return rea;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        if(m==1){
            cout<<"1"<<endl;
        }
        else{
            int ans=0;
            for(int i=2;i*i<=n;i++){
                if(n%i==0){
                    if(i>=m&&i*i!=n)
                    ans+=phi(n/i);
                    if(n/i>=m)
                    ans+=phi(i);
                }
            }
            cout<<ans+1<<endl;
        }
    }
}
View Code

优质内容筛选与推荐>>
1、Java线程—中断技术
2、LeetCode 700 Search in a Binary Search Tree 解题报告
3、实验三实验报告
4、经典SQL语句大全
5、平凡的人,不一样的幸福


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn