传送阵:NEFU2022-Euler's totient function - Virtual Judge
思路:
枚举因子i(1,n),对于每个因子i(i>=m),求有多少个x(1,n)使得gcd(x,n)=i
gcd(x,n)=i <=> gcd(x/i,n/i)=1 <=> 求n/i的欧拉函数的值。
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define per(i,a,b) for(int i=a;i<=b;i++)
#define ber(i,a,b) for(int i=a;i>=b;i--)
const int N = 1e5 + 3;
const long long mod = 10000000033;
LL n, m;
LL phi(LL x)
{
if (x == 1)
return 1;
LL ans = x;
for (int i = 2; i * i <= x; i++)
{
if (x % i == 0)
ans = ans / i * (i - 1);
while (x % i == 0)
x /= i;
}
if (x > 1)
ans = ans / x * (x - 1);
return ans;
}
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--)
{
LL ans = 0;
cin >> n >> m;
if (m == 1)
{
cout << n << endl;
continue;
}
for (int i = 1; i * i <= n; i++)
{
if (n % i == 0)
{
if (n / i >= m) ans += phi(i);
if (i >= m && i * i != n) ans += phi(n / i);//防止i*i==n时算两遍
}
}
cout << ans << endl;
}
return 0;
}