Files
python/TangDou/AcWing_TiGao/T5/RongChi/215_0.cpp

56 lines
1.2 KiB
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e4 + 5;
// 例题:HDU4135 HDU2841,HDU1695,HDU3501
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;
}