复习题之Blah数集

题目描述:

  大数学家高斯小时候偶然间发现一种有趣的自然数集合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<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL long long
LL q[1000100];
int a,n;
void work(int a,int n)
{
    int tail=2,head1=1,head2=1;
    q[1]=a;
    while(tail<=n)
    {
        LL x=q[head1]*2+1,y=q[head2]*3+1;
        int t=min(x,y);
        if(x<y)head1++;
        else head2++;
        if(t==q[tail-1])continue;
        q[tail++]=t;
    }
    printf("%lld
",q[n]);
}
int main()
{
    while(scanf("%d%d",&a,&n))work(a,n);
    return 0;
}
原文地址:https://www.cnblogs.com/EvilEC/p/6854898.html