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.

56 lines
1.2 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e4 + 5;
// 例题HDU4135 HDU2841HDU1695HDU3501
LL n, m;
// m 比较大的话可以先打出来素数表再求
// 分解质因数
int p[N], cnt;
void divide(int x) {
cnt = 0;
for (int i = 2; i <= x / i; i++) {
if (x % i == 0) {
p[cnt++] = i;
while (x % i == 0) x /= i;
}
}
if (x > 1) p[cnt++] = x;
}
LL solve() {
LL ans = 0;
for (int i = 1; i < (1 << cnt); i++) { // 二进制枚举质因子所有组合
LL t = 1, cnt = 0;
for (int j = 0; j < cnt; j++) {
if (i >> j & 1) {
t *= p[j];
cnt++;
}
}
ans += (cnt & 1 ? n / t : -n / t); // 奇加偶减
}
return n - ans;
}
int main() {
// 求1~10中与3互质的数的个数
/*
10 3
1,2,4,5,7,8,10 7
7
*/
cin >> n >> m;
// 对m分解质因数
divide(m);
cout << solve() << endl;
return 0;
}