hduoj1216
来源:互联网 发布:金融数据录入 编辑:程序博客网 时间:2024/06/11 20:28
Assistance Required
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2539 Accepted Submission(s): 1332
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
题解:直接数组模拟。3000是33809.
#include<iostream>#include<cstring>#include<stack>#include<algorithm>#include<cmath>#define INF 0x3f3f3f3f#define maxn 34000using namespace std;int ans[3000],vis[maxn];int main() { int tot,tmp; int cnt = 1; for(int i = 1;i < maxn;i++) { tmp = i+1; if(!vis[tmp]) { ans[cnt++] = tmp; tot = 0; for(int j = tmp+1;j <= maxn;j++) { if(!vis[j]) tot++; if(tot == tmp) { vis[j] = 1; tot = 0; } } } } int n; while(cin >> n&&n) { cout << ans[n] << endl; } return 0;}
0 0
- hduoj1216
- SpringMVC学习笔记(一)使用IntelliJ IDEA开发Spring MVC HelloWorld
- PAT A1043. Is It a Binary Search Tree (25)
- HTML5之SVG 2D入门5—颜色的表示及定义方式
- Python的字符串的引号
- session深度解析
- hduoj1216
- HTML5之SVG 2D入门6—视窗坐标系与用户坐标系及变换概述
- HTML5之SVG 2D入门7—SVG元素的重用与引用
- nginx, apache, mysql, php源码安装
- 根据中序和前序遍历重建二叉树
- Spring-mvc json 返回前端
- HTML5之SVG 2D入门8—文档结构及相关元素总结
- Intellij IDEA 启动项目ClassNotFoundException:org.springframework.web.context.ContextLoaderListener
- 插入排序----直接插入排序算法