总时间限制: 3000ms 内存限制: 65536kB
描述
大数学家高斯小时候偶然间发现一种有趣的自然数集合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<bits/stdc++.h>
using namespace std;
int q[1000011];
int main(){
int a,n,x,two,three,rear;
while(cin>>a>>n)//通过循环输入,以Ctrl+z后换行为结束
{
two=three=rear=1;
q[1]=a;
while(rear!=n)
{
if(2*q[two]+1>3*q[three]+1){
rear++;
q[rear]=3*q[three]+1;
three++;
}else if(2*q[two]+1<3*q[three]+1)
{
rear++;
q[rear]=2*q[two]+1;
two++;
}else{
rear++;
q[rear]=3*q[three]+1;
two++;
three++;
}
}
cout<<q[n]<<endl;
int a,n,x,two,three,rear;
}
return 0;
}