大佬的板子:偷来用了
转:http://blog.csdn.net/cillyb/article/details/72823907
题意
L先生想求出第n个质数(素数)是多少,你能帮助他吗?
数据
T < 70, 0 < n <= 3e6, 输入0表示结束。
输入
1
2
3
4
10
100
0
输出
Case 1: 2
Case 2: 3
Case 3: 5
Case 4: 7
Case 5: 29
Case 6: 541
HDU5901是个求1-1e11内素数个数的模板题,这题可以利用二分+那个模板求解。(用那的第二个板子应该会更快些,但不知道为什么迷之RE,所以只能将就套第一个)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 100
#define MAXM 10001
#define MAXP 40000
#define MAX 400000
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)
#define dbg(x) cout << #x << " = " << x << endl
#define chkbit(ar, i) (((ar[(i) >> 6]) & (1 << (((i) >> 1) & 31))))
#define setbit(ar, i) (((ar[(i) >> 6]) |= (1 << (((i) >> 1) & 31))))
#define isprime(x) (( (x) && ((x)&1) && (!chkbit(ar, (x)))) || ((x) == 2))
using namespace std;
typedef long long ll;
namespace pcf{
long long dp[MAXN][MAXM];
unsigned int ar[(MAX >> 6) + 5] = {0};
int len = 0, primes[MAXP], counter[MAX];
void Sieve(){
setbit(ar, 0), setbit(ar, 1);
for (int i = 3; (i * i) < MAX; i++, i++){
if (!chkbit(ar, i)){
int k = i << 1;
for (int j = (i * i); j < MAX; j += k) setbit(ar, j);
}
}
for (int i = 1; i < MAX; i++){
counter[i] = counter[i - 1];
if (isprime(i)) primes[len++] = i, counter[i]++;
}
}
void init(){
Sieve();
for (int n = 0; n < MAXN; n++){
for (int m = 0; m < MAXM; m++){
if (!n) dp[n][m] = m;
else dp[n][m] = dp[n - 1][m] - dp[n - 1][m / primes[n - 1]];
}
}
}
long long phi(long long m, int n){
if (n == 0) return m;
if (primes[n - 1] >= m) return 1;
if (m < MAXM && n < MAXN) return dp[n][m];
return phi(m, n - 1) - phi(m / primes[n - 1], n - 1);
}
long long Lehmer(long long m){
if (m < MAX) return counter[m];
long long w, res = 0;
int i, a, s, c, x, y;
s = sqrt(0.9 + m), y = c = cbrt(0.9 + m);
a = counter[y], res = phi(m, a) + a - 1;
for (i = a; primes[i] <= s; i++) res = res - Lehmer(m / primes[i]) + Lehmer(primes[i]) - 1;
return res;
}
}
long long solve(long long n){
int i, j, k, l;
long long x, y, res = 0;
for (i = 0; i < pcf::len; i++){
x = pcf::primes[i], y = n / x;
if ((x * x) > n) break;
res += (pcf::Lehmer(y) - pcf::Lehmer(x));
}
for (i = 0; i < pcf::len; i++){
x = pcf::primes[i];
if ((x * x * x) > n) break;
res++;
}
return res;
}
int main()
{
pcf::init();
ll n, ca = 1;
while(cin >> n && n)
{
ll l = 2, r = 1e8, ans;
while(l <= r)
{
ll mid = (l+r)/2;
if(pcf::Lehmer(mid) >= n)
r = mid-1, ans = mid;
else l = mid+1;
}
printf("Case %lld: %lld\n", ca++, ans);
}
return 0;
}