题意:求[a,b]与n互质的个数
思路:容斥原理的运用,提取n的素因子,先算出不与n互素的个数,容斥原理奇加偶减
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define ll long long using namespace std; const int MAXN = 70; ll prime[MAXN]; ll a,b,n; ll make(ll x, int m){ ll ans = 0; ll len = 1<<m; for (ll i = 1; i < len; i++){ ll tmp = 1,flag = 0; for (ll j = 0; j < m; j++) if (i & (ll)(1<<j)){ ++flag; tmp *= prime[j]; } if (flag & 1) ans += x/tmp; else ans -= x/tmp; } return ans; } int main(){ int t,cas=1; scanf("%d",&t); while (t--){ cin>>a>>b>>n; int m = 0; for (ll i = 2; i*i <= n; i++) if (n && (n%i) == 0){ prime[m++] = i; while (n && (n%i) == 0) n /= i; } if (n > 1) prime[m++] = n; printf("Case #%d: ",cas++); cout << b-make(b,m)-(a-1-make(a-1,m)) << endl; } return 0; }
HDU - 4135 Co-prime,码迷,mamicode.com
原文地址:http://blog.csdn.net/u011345136/article/details/24726547