2729:Blah数集

2729:Blah数集

总时间限制:
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
 1 #include<iostream>
 2 #include<queue>
 3 #include<bits/stdc++.h>
 4 using namespace std;
 5 int tot=1;
 6 int x;
 7 int main()
 8 {
 9     int x,n;
10     while(cin>>x&&cin>>n)
11     {
12     tot=1;
13     queue<int>a;
14     queue<int>b;    
15     while(tot<n)
16     {
17         a.push(2*x+1);
18         b.push(3*x+1);
19         if(a.front()>b.front())
20         {
21             x=b.front();
22             b.pop();
23         }
24         else if(a.front()<b.front())
25         {
26             x=a.front();
27             a.pop();
28         }
29         else 
30         {
31             x=a.front();
32             a.pop();
33             b.pop();
34         }
35         tot++;
36     }
37         cout<<x<<endl;
38     }
39     
40     return 0;
41 }
原文地址:https://www.cnblogs.com/zwfymqz/p/6636579.html