bzoj2986 Non-Squarefree Numbers

Non-Squarefree Numbers

Time Limit: 10 Sec Memory Limit: 128 MB

Description

一个正整数K被称为squarefree,如果它没有一个D^2(D>1)这样的约数。

Input

读入一个正整数N

Output

找出第N个不是squarefree的数。1<=N<=10^10

Sample Input

10

Sample Output

27

Hint

前10个非squarefree的数

4 8 9 12 16 18 20 24 25 27






好久没有做数学题了qwq.....





#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
long long N, sq, tot, ans;
int prime[maxn], mu[maxn];
bool not_prime[maxn];

inline void prepare(){
	mu[1] = 1; sq = 5e5;
	for(int i = 2; i <= sq; ++i){
		if(!not_prime[i]){prime[++tot] = i; mu[i] = -1;}
		for(int j = 1; prime[j] * i <= sq; ++j){
			int now = prime[j] * i; not_prime[now] = true;
			if(i % prime[j] == 0){mu[now] = 0; break;}
			mu[now] = -mu[i];
		}
	}	
}

inline bool check(long long n){
	ans = 0; sq = sqrt(n);
	for(long long i = 2; i <= sq; ++i)	ans -=  mu[i] * (n / (i * i));
	return ans >= N;
}

int main()
{
	prepare();
	cin >> N; long long L = 1, R = 5e10;		
	while(L < R){
		long long mid = (L + R) / 2;
		if(check(mid)) R = mid;
		else L = mid + 1;
	}
	cout << L;
	return 0;	
}

posted @ 2018-09-27 14:45  沛霖  阅读(137)  评论(0编辑  收藏  举报