POJ 2591 Set Definition

来源:互联网 发布:女网络歌手好听的歌 编辑:程序博客网 时间:2024/05/14 23:53

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

Set Definition
Time Limit: 1000MS Memory Limit: 65536KTotal 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

100254

Sample Output

4181461
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;}


原创粉丝点击