2013
12-04

# Assistance Required

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.

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.

For each test case specified by n output on a single line the n-th lucky number.

1
2
10
20
0

2
3
29
83

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

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define INF 0x3f3f3f3f
using namespace std;

int vis[1000009];
int prime[10000];
int cc = 0;

void init() {
int i;
mem(vis, -1);
cc = 1;
for(i = 2; ; i++) {
if(vis[i] == -1) {
prime[cc++] = i;
if(cc > 3000) {
break;
}
vis[i] = 1;
int cnt = 0;
for(int j = i + 1; j <= 100000; j++) {
cnt -= vis[j];
if(cnt == i) {
cnt = 0;
vis[j] = 0;
}
}
}
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("E:/ACMData.txt","r",stdin);
#endif
int n;
init();
while(cin>>n, n) {
cout<<prime[n]<<endl;
}
return 0;
}