HDU 1216 Assistance Required

本文详细介绍了如何通过素数筛法找到第3000个素数,包括算法实现和复杂度分析。文章还讨论了预估素数数量的方法,并通过代码实现展示了求解过程。

http://acm.hdu.edu.cn/showproblem.php?pid=1216

最开始题意理解错误,以为是求第n个素数,结果没通过样例。

理解题意后模拟就可以了,但是需要估计第3000个数有多大。

百度发现小于10e5的素数有9592个,所以猜测这道题要求的第3000个数不超过10e5。

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 #include<math.h>
 6 #include<map>
 7 #include<stack>
 8 #include<queue>
 9 #include<algorithm>
10 #define mem(a,b) memset(a,b,sizeof(a))
11 #define MAX(a,b) ((a)>(b)?(a):(b))
12 #define MIN(a,b) ((a)<(b)?(a):(b))
13 #define INF 0x3f3f3f3f
14 #pragma comment(linker, "/STACK:102400000,102400000")
15 using namespace std;
16 
17 int vis[1000009];
18 int prime[10000];
19 int cc = 0;
20 
21 void init() {
22     int i;
23     mem(vis, -1);
24     cc = 1;
25     for(i = 2; ; i++) {
26         if(vis[i] == -1) {
27             prime[cc++] = i;
28             if(cc > 3000) {
29                 break;
30             }
31             vis[i] = 1;
32             int cnt = 0;
33             for(int j = i + 1; j <= 100000; j++) {
34                 cnt -= vis[j];
35                 if(cnt == i) {
36                     cnt = 0;
37                     vis[j] = 0;
38                 }
39             }
40         }
41     }
42 }
43 
44 int main() {
45 #ifndef ONLINE_JUDGE
46     freopen("E:/ACMData.txt","r",stdin);
47 #endif
48     int n;
49     init();
50     while(cin>>n, n) {
51         cout<<prime[n]<<endl;
52     }
53     return 0;
54 }

转载于:https://www.cnblogs.com/soenlit/archive/2012/11/23/2784289.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值