题面
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
思路
由题意我们可以设gcd(x,n)=s,那么sa=x,sb=n,因为n>=x,所以b>=a,我们要求的就相当于是b的一个欧拉函数。不过要注意,这个m的数据范围有点大,我们去根号去进行gcd的枚举,然后当它不是平方数的时候我们对n/(n/i)进行一个欧拉函数的求解并计入ans中就可以了。
代码实现
#include<cstdio>
#include<queue>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
ll euler_phi (ll n) { //单个欧拉函数求法
ll m=(ll ) sqrt(n+0.5);
ll ans=n;
for (ll i=2;i<=m;i++) if (n%i==0) {
ans=ans/i*(i-1);
while (n%i==0) n/=i; //直接缩小范围确定质因子
}
if (n>1) ans=ans/n*(n-1);
return ans;
}
int main () {
ll t,n,m,ans;
cin>>t;;
while (t--) {
ans=0;
cin>>n>>m;
for (int i=1;i*i<=n;i++) {
if (n%i==0) {
if (i>=m) ans+=euler_phi(n/i);
if (i*i!=n&&(n/i>=m)) {
ans+=euler_phi(i);
}
}
}
cout<<ans<<endl;
}
return 0;
}