2 1 10 2 3 15 5
Case #1: 5 Case #2: 10HintIn the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
#include <cstdio> #include <cstring> #define ll long long int const MAX = 5e5 + 5; int fac[MAX]; int cnt; ll a, b, n, num1, num2; void get_factor(ll x) { cnt = 0; for(int i = 2; i * i < MAX; i++) { if(x % i == 0) { fac[cnt ++] = i; while(x % i == 0) x /= i; } } if(x > 1) fac[cnt ++] = x; } void DFS(int idx, ll cur, int sgin, ll s, bool f) { for(int i = idx; i < cnt; i++) { ll tmp = (ll) cur * fac[i]; if(f) num1 += (ll) sgin * (s / tmp); else num2 += (ll) sgin * (s / tmp); DFS(i + 1, tmp, -sgin, s, f); } } int main() { int T; scanf("%d", &T); for(int ca = 1; ca <= T; ca++) { scanf("%I64d %I64d %I64d", &a, &b, &n); get_factor(n); num1 = 0; DFS(0, 1, 1, b, 1); num2 = 0; DFS(0, 1, 1, a - 1, 0); ll ans = (ll)b - a + 1 - (num1 - num2); printf("Case #%d: %I64d\n", ca, ans); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 4135 Co-prime (分解质因数 + 容斥)
原文地址:http://blog.csdn.net/tc_to_top/article/details/47700291