You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.5 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int N = 1e6 + 10;
// 返回1-m中与n互素的数的个数
vector<int> p;
int cal(int n, int m) {
p.clear();
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
p.push_back(i);
while (n % i == 0) n /= i;
}
}
if (n > 1) p.push_back(n); // 求n的素因子
int s = 0; // 1到m中与n不互素的数的个数
// 枚举子集不能有空集所以从1开始
for (int i = 1; i < 1 << p.size(); i++) { // 从1枚举到(2^素因子个数)
int cnt = 0;
int t = 1;
for (int j = 0; j < p.size(); j++) { // 枚举每个素因子
if (i & (1 << j)) { // 有第i个因子
cnt++; // 计数
t *= p[j]; // 乘上这个质因子
}
}
// 容斥原理
if (cnt & 1) // 选取个数为奇数,加
s += m / t;
else // 选取个数为偶数,减
s -= m / t;
}
return m - s; // 返回1-m中与n互素的数的个数
}
signed main() {
int T;
cin >> T;
while (T--) {
int a, b;
cin >> a >> b;
int res = 0;
// 从1~n(b现在就是n)之间找到所有与m(m现在就是i)互质的数字个数
for (int i = 1; i <= a; i++) res += cal(i, b);
printf("%lld\n", res);
}
}