题目描述
大数学家高斯小时候偶然间发现一种有趣的自然数集合Blah,对于以 a 为基的集合 Ba 定义如下:
(1) a 是集合 Ba 的基,且 a 是 Ba 的第一个元素;
(2) 如果 x 在集合 Ba 中,则 2x+1 和 3x+1 也都在集合 Ba 中;
(3) 没有其他元素在集合 Ba 中了。
现在小高斯想知道如果将集合 Ba 中元素按照升序排列,第 N 个元素会是多少?
输入
输入包括很多行,每行输入包括两个数字,集合的基 a 以及所求元素序号 n。
输出
对于每个输入,输出集合 Ba 的第 n 个元素值
样例输入输出
样例输入 #1复制
1 100
28 5437
样例输出 #1复制
418
900585
数据范围
70% 的数据,1≤n≤100000。
100% 的数据,1≤n≤1000000,1≤a≤50。
(主要就是用些基础语句,关键是逻辑)
AC代码:
#include<bits/stdc++.h>
using namespace std;
int a,n,b[1000005],head1,head2,rear;
int main()
{
while(cin>>a>>n)
{
head1=head2=1;
rear=1;
b[head1]=a;
while(rear<n){
int x=b[head1]*2+1,y=b[head2]*3+1;
if(x<y){
b[++rear]=x;
head1++;
}
else
if(y<x){
b[++rear]=y;
head2++;
}
else
{
b[++rear]=x;
head1++;
head2++;
}
}
cout<<b[n]<<endl;
}
return 0;
}
欢迎在评论区提建议!
点个赞加关注吧,原力不够了
这道题还算简单吧……