hdu 1216 (vector || 打表)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1216

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.

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开始编号,依次为2,3,4,5,6.。。。

每次删除元素,从第一个开始删, 2,那么每隔2个删除那个元素,,于是删除了 4,6,8,10, 现在数组变成 2,3,5,7,9,11,13,15,17,19,21。。

然后从3开始删除 ,每隔3个删除删除元素 ,,于是删除了9,15,21, 现在变成 2,3,5,7,11,13,17,19...

然后从5开始删除每隔5个删除一个 于是删除了 19,35,49。。

【分析】

最终会剩下一系列数字,根据题目的数据范围,我们只要求到第3000个数就行。

【打表】

首先就想到了数组的模拟做法,经过反复尝试,确定第3000个数是33809.所以数据最大开到这里就行。

时间:140ms

#include <iostream>
#include <cstring>
using namespace std;
const int maxn=34000;
int vis[maxn];
int ans[3030];//保存结果
int main(){
	memset(vis,0,sizeof(vis));
	int cnt=1;
	for(int i=1;i<maxn;i++){
		int tmp=i+1;
		if(!vis[tmp]){
			ans[cnt++]=tmp;
			int idx=0;
			for(int j=tmp+1;j<=maxn;j++){
				if(!vis[j])
					++idx;
				if(idx==tmp)
				{	vis[j]=true;
					idx=0;
				}
			}
		}
	}
	 int t;
	while(cin>>t&&t){
		cout<<ans[t]<<endl;

         }
	return 0;
}

写出这个代码之后,我总感觉会超时,一直没敢交,后来用下面的vector方法做出来后,才交了这个代码,发现这个居然更快!

【vector】

用vector的erase操作,就可以删除指定位置的元素了

#include <iostream>
#include <vector>
using namespace std;
vector<int >vt;
int main(){
	 for(int i=2;i<=40000;i++)
		vt.push_back(i);
	for(vector<int >::iterator it=vt.begin();;it!=vt.end();)
	{
		for(vector<int >::iterator it2=it+(*it);it2<vt.end();it2+=(*it))
		{
			it2=vt.erase(it2);//erase 返回的是下一个位置
			it2--;
		}
		it++;
	}
	int t;
	while(cin>>t&&t){
		cout<<vt[t-1]<<endl;
	}
}

时间:468ms

时间: 2024-11-03 21:39:28

hdu 1216 (vector || 打表)的相关文章

hdu 4715 素数打表

先利用筛法完成素数打表 再从小到大判断即可 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> using namespace std; const int Max = 1e6 + 50; int n; int isPrime[Max]; int tblPrime[Max];

HDU 4203 博弈 打表找规律

http://acm.hdu.edu.cn/showproblem.php?pid=4203 一堆数量为s的硬币,两个人轮流从中取硬币,最后取完的人获胜,其中每次只能取k的n次方个硬币(n = 0, 1, 2, 3-),求想要取胜,当前要取走的最少硬币数. s的范围是1e9,直接储存sg函数是不现实的,所以考虑打表找找规律看. 通过打表可以得出规律: 当k为偶数时,sg函数值依次为 0 1 0 1 0 1 0 1- 当k为奇数时,sg函数值依次为 0 1 0 1 0 1-k(总长度为k + 1)

vector 邻接表的建立(好笨啊,才懂,可能太困了吧)。。

图的建立有两种,邻接矩阵和邻接表. 邻接矩阵适用于图较为密集,(稀疏图太浪费存储空间了),图如果较为稀疏,则使用邻接表为宜,dijkstra算法就是以邻接表为基础的. 有向无权图 #include<iostream> #include <vector> #include <algorithm> #include <queue> #include <stack> using namespace std; #define N 100000+5 vec

STL_稀疏图,树_使用vector邻接表存储

本文出自:http://blog.csdn.net/svitter 分析:vector是STL模板中的容器.可以利用其性质来构建邻接表. 定义: #include <vector> #define MAXN 10000 //max n of a tree or graph //if is a tree, n / 2 is OK ; using namespace std; typedef vector<int> vint; vector <vint> G(MAXN);

Hdu 5289-Assignment 贪心,ST表

题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3175    Accepted Submission(s): 1457 Problem Description Tom owns a company and he i

hdu 6253 (bfs打表)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=6253 题意: 马可以往一个方向走两步,然后转个弯走一步,这样算一次动作,求问马n次动作后,能到达多少个点,重复到达的点只算一次. 思路: 一开始完全没思路,画图找了半天把自己画崩了,后面看到数据和样例感觉这应该是一道公式题,然后打了一个表.. 打表代码: #include<bits/stdc++.h> using namespace std; #define ull unsigned long lon

HDU - 6253 Knightmare (打表+拉格朗日插值)

题目链接 题意:一个马在无限大的棋盘中跳,问跳n步能跳到多少个不同的格子. 首先写个打表程序打一下n比较小的时候的表: 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=50+10,mod=998244353; 5 const int dx[]= {-2,-1,1,2,2,1,-1,-2}; 6 const int dy[]= {-1,-2,-2,-1,1,2,2,1};

hdu 3032 sg打表找规律 *

有n堆石子,alice先取,每次可以选择拿走一堆石子中的1~x(该堆石子总数) ,也可以选择将这堆石子分成任意的两堆.alice与bob轮流取,取走最后一个石子的人胜利. 打表代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 using namespace std; 6 const int N=1000010; 7 int sg[N];

符号三角形(hdu 2510 搜索+打表)

符号三角形 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1246    Accepted Submission(s): 664 Problem Description 符号三角形的 第1行有n个由“+”和”-“组成的符号 ,以后每行符号比上行少1个,2个同号下面是”+“,2个异 号下面是”-“ .计算有多少个不同的符号三角形,使其所含