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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}