Educational Codeforces Round 50 (Rated for Div. 2) F - Relatively Prime Powers(数学+容斥)
题目链接:http://codeforces.com/contest/1036/problem/F
题意:
题解:求在[2,n]中,x != a ^ b(b >= 2 即为gcd)的个数,那么实际上就是要求x = a ^ b的个数,然后用总数减掉就好了,答案即为。(pow会丢失精度,学习避免精度丢失的方法)
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define LL __int128 5 #define ull unsigned long long 6 #define mst(a,b) memset((a),(b),sizeof(a)) 7 #define mp(a,b) make_pair(a,b) 8 #define pi acos(-1) 9 #define pii pair<int,int> 10 #define pb push_back 11 const int INF = 0x3f3f3f3f; 12 const double eps = 1e-6; 13 const int MAXN = 1e5 + 10; 14 const int MAXM = 2e6 + 10; 15 const ll mod = 1e9 + 7; 16 17 bool check[110]; 18 int prime[110]; 19 int mu[110]; 20 21 void Moblus(int N) { 22 mst(check, false); 23 mu[1] = 1; 24 int tot = 0; 25 for(int i = 2; i <= N; i++) { 26 if(!check[i]) { 27 prime[tot++] = i; 28 mu[i] = -1; 29 } 30 for(int j = 0; j < tot; j++) { 31 if(i * prime[j] > N) { 32 break; 33 } 34 check[i * prime[j]] = true; 35 if(i % prime[j] == 0) { 36 mu[i * prime[j]] = 0; 37 break; 38 } else { 39 mu[i * prime[j]] = -mu[i]; 40 } 41 } 42 } 43 } 44 45 int main() 46 { 47 #ifdef local 48 freopen("data.txt", "r", stdin); 49 // freopen("data.txt", "w", stdout); 50 #endif 51 Moblus(100); 52 int t; 53 scanf("%d",&t); 54 while(t--) { 55 ll n; 56 scanf("%lld",&n); 57 ll ans = 0; 58 for(int i = 2; i <= 60; i++) { 59 if(!mu[i]) continue; 60 ll raiz = round(pow(n, 1.0 / i)); 61 while(pow((long double)raiz, i) > (long double)n) raiz--; 62 while(pow((long double)raiz + 1, i) <= (long double)n) raiz++; 63 raiz--; 64 ans += (ll)mu[i] * raiz; 65 } 66 printf("%lld\n",n - 1 + ans); 67 } 68 return 0; 69 }