【例2-2】Blah数集

【例2-2】Blah数集

链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1333
时间限制: 1000 ms         内存限制: 65536 KB

【题目描述】

大数学家高斯小时候偶然间发现一种有趣的自然数集合Blah,对于以a为基的集合Ba定义如下:

(1)a是集合Ba的基,且a是Ba的第一个元素;

(2)如果x在集合Ba中,则2x+1和3x+1也都在集合Ba中;

(3)没有其他元素在集合Ba中了。

现在小高斯想知道如果将集合Ba中元素按照升序排列,第N个元素会是多少?

 

【输入】

输入包括很多行,每行输入包括两个数字,集合的基a(1<=a<=50))以及所求元素序号n(1<=n<=1000000)。

【输出】

对于每个输入,输出集合Ba的第n个元素值。

【输入样例】

1 100
28 5437

【输出样例】

418
900585
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include <algorithm>
using namespace std;
long long p[1000005];

int main()
{
    int a,n;
    while(cin>>a>>n)
    {
        int two=0,three=0,i=0;
        memset(p,0,sizeof(p));
        p[0]=a;
        while(i<n)
        {
            long long u=p[two]*2+1,v=p[three]*3+1;
            long long t=min(u,v);
            if(t!=p[i])p[++i]=t;
            if(u<v)two++;
            else three++;                  
        }
        cout<<p[i-1]<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/EdSheeran/p/7966244.html