HDU 1216 Assistance Required
来源:互联网 发布:政务大数据解决方案 编辑:程序博客网 时间:2024/05/16 11:43
Assistance Required
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1700 Accepted Submission(s): 900
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源代码:#include<iostream>//--------1216 HDU 埃拉托色尼筛选法#include<stdio.h>#include<stdlib.h>#include<algorithm>#include<math.h>#include<string.h>#include<vector>using namespace std;#define Max 40000bool b[Max];__int64 a[Max], c;void fun(){__int64 i, j, count;c = 0;memset(b, true, sizeof(b));b[1] = 0;for (i = 2; i < Max; ++i){if (b[i]){a[++c] = i;count = i;for (j = i+1; j < Max; ++j){if (b[j] == true)count--;if (count == 0){b[j] = false;count = i;}}}}}int main(){fun();int n;while (scanf("%d", &n) != EOF){if (n == 0)break;elseprintf("%d\n", a[n]);}return 0;}基本类似的另一种:#include<iostream>#include<stdio.h>#include<cstdlib>#include<string.h>#include<algorithm>#include<math.h>using namespace std;#define Max 40000__int64 ans[Max];__int64 pop[3001];void dfs(){ __int64 i, j, count; for (i = 2; i < Max; i++) { ans[i] = i; } for (i = 2; i < Max; i++) { if (ans[i] != 0) { count = i; for (j = i + 1; j < Max; j++) { if (ans[j] != 0) count--; if (count == 0) { ans[j] = 0; count = i; } } } } j = 2; for (i = 1; i <= 3000; i++) { while (ans[j] == 0) { j += 1; } if (ans[j] != 0) { pop[i] = ans[j]; j += 1; } }}int main(){ dfs(); int n; while (scanf("%d", &n)&&n) { printf("%d\n", pop[n]); } return 0;}
1 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
- 支付宝的应用+代码
- 时间换算
- 第九周项目2- 对称矩阵压缩存储的实现与应用
- multithreading--演示中断线程Thread的异常
- 经典SQL语句大全
- HDU 1216 Assistance Required
- VoIPER测试工具的使用
- 求两个数最大公约数
- IOS的APP结构、生命周期、main
- Linux kvm 虚拟机介绍
- SPDY 是什么?如何部署 SPDY?
- 高精度加法
- IOS开发笔记23-Foundation框架(上)
- iPhone开发 判断当前的网络是3g还是wifi