标签:
求A到B之间有多少个数能与N互质。
学了下容斥原理的写法, 想将N分解质因数,然后容斥原理,N - 单个质因数倍数个数+2个质因数倍数的个数-3个质因数的个数......
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<iostream> #include<string> #include<queue> #include<stack> #include<list> #include<stdlib.h> #include<algorithm> #include<vector> #include<map> #include<cstring> #include<set> using namespace std; typedef long long LL; vector<int> q; LL ans; void dfs(LL x, LL now, LL flag,LL key) { if (x == q.size()) { if (flag == 0) ans -= key / now; else ans += key / now; return; } dfs(x + 1, now, flag, key); dfs(x + 1, now*q[x], flag ^ 1, key); } LL solve(LL x) { ans = 0; dfs(0, 1, 1, x); return ans; } int main() { int Icase = 0; int T, n; LL a, b; cin >> T; while (T--){ ans = 0; cin >> a >> b >> n; q.clear(); int t = n; for (int i = 2; i*i <= t; i++) { if (t%i) continue; while (t%i == 0) t /= i; q.push_back(i); } if (t > 1) q.push_back(t); printf("Case #%d: ", ++Icase); cout << solve(b) - solve(a - 1) << endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/yigexigua/p/4722179.html