hdu 1216 Assistance Required

17 篇文章 0 订阅

传送门

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

1
2
10
20
0

Sample Output

2
3
29
83

【题意】

开始有一个从2开始的自然数序列,开始如下操作,取出该序列中的第一个数字,作为新序列中的数字,假设该数字是a,然后删除原序列中第a个数字,第2a个数字……一直这样操作,这样会生成一个新序列,然后每次询问你新序列中某一项是多少。

【分析】

直接模拟即可,也可以提前将表打出来,然后把表交上去,前者耗时240Ms,后者代码长度大。

【代码】

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<cmath>
#include<map>
#include<time.h>
#include<bits/stdc++.h>
using namespace std;
const int maxm = 4e4;
bool num[maxm];
int ans[maxm];
int main()
{
//    freopen("in.txt","w",stdout);
    memset(num,false,sizeof(num));
    int flag = 1;
    for(int i = 2;i<maxm;i++)
    {
        if(!num[i])
        {
            ans[flag++] = i;
            int x = 0;
            int now = i+1;
            while(now<maxm)
            {
                if(!num[now])
                {
                    x++;
                    if(x==i)
                    {
                        num[now] = true;
                        x = 0;
                    }
                }
                now++;
            }
        }
    }
    int a;
    while(scanf("%d",&a),a)
    {
        printf("%d\n",ans[a]);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值