Blash数集
题目描述:
Blash集合对应以 a 为基数的集合 Ba 定义如下:
(1) a 是集合 Ba 的基数,且 a 是 Ba 的第一个元素。
(2) 如果 x 在集合 Ba 中,则 2x+1 和 3x+1 也都在集合Ba中。
(3) 没有其它元素在集合 Ba 中。
问将集合Ba中元素按照升序排列,第n个元素会是多少?
输入格式:
输入包含很多行,每行输入包括两个数字,集合的基数 a 以及所求元素序号 n。
输出格式:
对应每个输入,输出集合 Ba 的第n个元素值。
解析:
由于做队列的题不多,考场上浪费了太多时间。
此题主要考察基础数据结构——队列。
这道题的难点在于如何判断重复的数字,我们可以用队列维护,保证每次加进去的是当前最小的数且与已加入队列尾的数不等就可以了。
代码:
#include <bits/stdc++.h>
using namespace std;
const int Max=1001000;
int a,n,t1,t2,head,tail;
int p[Max];
inline int get_int()
{
int x=0,f=1;
char c;
for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
if(c=='-') {f=-1;c=getchar();}
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
return x*f;
}
int main()
{
//freopen("blash.in","r",stdin);
//freopen("blash.out","w",stdout);
while(scanf("%d%d",&a,&n)!=EOF)
{
t1=1;
t2=1;
head=2;
p[1]=a;
while(head<=n)
{
int x=p[t1]*2+1;
int y=p[t2]*3+1;
int minn=min(x,y);
if(x<y) t1++;
else t2++;
if(minn==p[head-1]) continue;
p[head]=minn;
head++;
}
cout<<p[n]<<"\n";
}
return 0;
}