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.
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 29AC代码:#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; }
阅读全文
0 0
- hdu 1216 Assistance Required
- HDU 1216 Assistance Required
- HDU 1216 Assistance Required
- hdu-1216-Assistance Required
- hdu 1216 Assistance Required
- hdu 1216 Assistance Required
- HDU 1216 Assistance Required(模拟)
- HDOJ 1216 Assistance Required
- HDUOJ 1216 - Assistance Required
- ZOJ 1978 && HDU 1216 && POJ 2552 Assistance Required (筛选)
- hdu 1216 Assistance Required(数学:打表)
- HDU1216--Assistance Required HDU(92)
- Assistance Required
- Assistance Required
- Assistance Required
- Assistance Required
- 1216Assistance Required(筛选法模拟题意)
- 杭电ACM-1216-Assistance Required
- 网易2018校招内推编程题
- struct和typedef struct
- 计算机体系结构-- 字节、字、bit、byte的关系
- SecureCRT 安装、连接虚拟机安装的Linux系统及两个系统间文件的传输
- SRILM的安装与使用
- hdu 1216 Assistance Required
- 字符细化算法
- [线性代数]行列式
- ViewStub使用
- 顺序表应用5:有序顺序表归并
- rup,xp,敏捷过程的含义
- 1014. Waiting in Line (30)
- lower_bound 与 upper_bound 返回值及用法
- Pythonday07