hdu 1216 Assistance Required

来源:互联网 发布:虚拟机怎么配置网络 编辑:程序博客网 时间:2024/05/17 04:43

Assistance Required

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1468    Accepted Submission(s): 764

Problem Description
After the 1997/1998 Southwestern European Regional Contest (which was held in Ulm) a large contest party took place. The organization team invented a special mode of choosing those participants that were to assist with washing the dirty dishes. The contestants would line up in a queue, one behind the other. Each contestant got a number starting with 2 for the first one, 3 for the second one, 4 for the third one, and so on, consecutively. 
The first contestant in the queue was asked for his number (which was 2). He was freed from the washing up and could party on, but every second contestant behind him had to go to the kitchen (those with numbers 4, 6, 8, etc). Then the next contestant in the remaining queue had to tell his number. He answered 3 and was freed from assisting, but every third contestant behind him was to help (those with numbers 9, 15, 21, etc). The next in the remaining queue had number 5 and was free, but every fifth contestant behind him was selected (those with numbers 19, 35, 49, etc). The next had number 7 and was free, but every seventh behind him had to assist, and so on. 

Let us call the number of a contestant who does not need to assist with washing up a lucky number. Continuing the selection scheme, the lucky numbers are the ordered sequence 2, 3, 5, 7, 11, 13, 17, etc. Find out the lucky numbers to be prepared for the next contest party. 
 
Input
The input contains several test cases. Each test case consists of an integer n. You may assume that 1 <= n <= 3000. A zero follows the input for the last test case. 
 
Output
For each test case specified by n output on a single line the n-th lucky number. 
 
Sample Input
1210200
 

Sample Output
232983
题目大意:
说明一点,不是素数打表,不是素数打表。 切记! 有一个打表的思想在里面
比如说有一系列数字从2开始,2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30,
然后每隔2个数删掉一个数,数组就变成了 2 3 5 7 9 11 13 15 17 19 21 23 25 27 29,
接着从3开始,每隔3个数删掉一个数,变成 2 3 5 7 11 13 17 19 23 25 29,
再从5开始,每隔5个数删掉一个数,变成 2 3 5 7 11 13 17 23 25 29
AC代码:
#include <cstdio>  #include <cstring>  using namespace std;  #define maxn 50000 bool judge[maxn];  int ans[3005];//保存结果void solve(){    memset(judge, true, sizeof(judge));      int cnt = 1;      for(int i = 2; i < maxn; i++){            if(judge[i] == true){              ans[cnt++] = i;              int idx = 0;              for(int j = i + 1;j <= maxn; j++){                  if(judge[j] == true)                       ++idx;                  if(idx == i)                  {   judge[j] = false;                      idx = 0;                  }              }          }      } }  int main(){       int n;     solve();     while(scanf("%d", &n) && n != 0){          printf("%d\n", ans[n]);     }      return 0;  }