Educational Codeforces Round 50: F. Relatively Prime Powers(莫比乌斯函数)

 

F. Relatively Prime Powers

Educational Codeforces Round 50: F. Relatively Prime Powers(莫比乌斯函数)_第1张图片

 

题意:

给你一个n,问满足在[2,n]范围内有多少个数是非次方数(也就是不是x^k(k>1)这样的)

 

思路:

答案就是

原理是利用容斥,注意n开i次根是向下取整(这题巨卡精度)

 

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define LL long long
#define LD long double
#define mod 1000000007
int cnt, flag[105] = {1,1}, pri[105], mu[105] = {0,1};
LL sum[105];
void Mulset()
{
	int i, j;
	for(i=2;i<=105;i++)
	{
		if(flag[i]==0)
		{
			pri[++cnt] = i;
			mu[i] = -1;
		}
		for(j=1;j<=cnt&&i*pri[j]<=105;j++)
		{
			flag[i*pri[j]] = 1;
			if(i%pri[j]==0)
			{
				mu[i*pri[j]] = 0;
				break;
			}
			mu[i*pri[j]] = -mu[i];
		}
	}
}
int main(void)
{
	LL ans, n;
	int T, i;
	Mulset();
	scanf("%d", &T);
	while(T--)
	{
		ans = 0;
		scanf("%lld", &n);
		for(i=2;i<=60;i++)
		{
			sum[i] = (LL)powl((LD)n+0.1, (LD)1.0/i)-1;
			ans += sum[i]*mu[i];
		}
		printf("%lld\n", n-1+ans);
	}
	return 0;
}

 

你可能感兴趣的:(#,数学or几何)