代码改变世界

POJ 2591 Set Definition

2012-01-05 14:49  javaspring  阅读(129)  评论(0编辑  收藏  举报

    一道poj以前月赛的题,数据超大,用long long  超内存,换成int 3万多K水过。。。。。。。。。这道题和上午那道纠结了一天的题是一样的,所以这道题很快就A了。水同样类型的题,很有成就感,有木有!有木有!!!!!!题目:

Set Definition
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8248   Accepted: 3765

Description

Set S is defined as follows: 
(1) 1 is in S; 
(2) If x is in S, then 2x + 1 and 3x + 1 are also in S; 
(3) No other element belongs to S. 

Find the N-th element of set S, if we sort the elements in S by increasing order.

Input

Input will contain several test cases; each contains a single positive integer N (1 <= N <= 10000000), which has been described above.

Output

For each test case, output the corresponding element in S.

Sample Input

100
254

Sample Output

418
1461
ac代码:

#include <iostream>
#include <cstdio>
using namespace std;
const int N=100005*100;
int num[N];
int min(int a,int b){
  return a<b?a:b;
}
int main(){
  //freopen("4.txt","r",stdin);
  num[1]=1;
  int p2,p3;
  p2=p3=1;
  int i=1;
  while(i<=10000001){
    num[++i]=min(2*num[p2]+1,3*num[p3]+1);
	if(num[i]==2*num[p2]+1)
		p2++;
	if(num[i]==3*num[p3]+1)
		p3++;
  }
  int n;
  while(~scanf("%d",&n)){
    printf("%d\n",num[n]);
  }
  return 0;
}