描述

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

 

 posted on 2019-10-26 16:20  qmzhna  阅读(128)  评论(0编辑  收藏  举报