UVa 11621 - Small Factors

时间:2023-03-09 00:52:54
UVa 11621 - Small Factors

称号:发现没有比给定数量少n的。只要2,3一个因素的数字组成。

分析:数论。贪婪,分而治之。

用两个三分球,分别代表乘法2,和繁殖3队列,队列产生的数字,原来{1}。

然后。每取两个指针相应元素*2和*3的值中最小的即为未找到的数字中最小的;

注意,可能生成反复数据。不要存进去(反复数据。一定连续产生)。

说明:打表计算。二分查询输出就可以。

#include <iostream>
#include <cstdlib>
#include <cstdio> using namespace std; int next[330]; int bs(int key, int r)
{
int l = 0,m;
while (l < r) {
m = (l+r)/2;
if (next[m] < key)
l = m+1;
else r = m;
}
return r;
} int main()
{
int two = 0,three = 0,count = 0;
next[0] = 1;
while (next[count] > next[count-1]) {
if (next[two]*2 < next[three]*3)
next[++ count] = next[two ++]*2;
else {
if (next[three]*3 == next[two]*2) two ++;
next[++ count] = next[three ++]*3;
}
} int n;
while (cin >> n && n)
cout << next[bs(n, count)] << endl; return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。