问1到N之间有多少个可以表示成M^K(K>1)的数,M^K <= N的个数有
M−−√K
个,如果K是合数,那么M^(a*b)可以表示成(M^a)^b和(M^b)^a,这就出现重复了,所以K一定要是素数,K即使是素数,也会出现重复,比如比如9^3==3^6==27^2,所以还要去掉指数是素数的积(3^6)的情况。因为2^60>1e18,2*3*5*7>60,所以最多枚举三个因子(K的三个因子)就可以了。
参考:http://fudq.blog.163.com/blog/static/1913502382013720515337/
本来是写了几个循环做的,但是怎么都过不去,也不知道精度哪里出问题了。。。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int prime[17] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59};
LL res,N;
void dfs(int cnt, int num, int t, int tot)
{
if(t == tot)
{
LL temp = (LL)pow(N,1.0/num);
temp--;
if(tot&1) res += temp;
else res -= temp;
return;
}
if(cnt >= 17) return;
if(num*prime[cnt] < 60)
dfs(cnt+1,num*prime[cnt],t+1,tot);
dfs(cnt+1,num,t,tot);
}
int main()
{
while(cin >> N)
{
res = 1;
for(int i = 1; i <= 3; ++i)
dfs(0,1,0,i);
cout << res << endl;
}
return 0;
}