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;
}
时间: 2024-10-05 11:46:02

UVa 11621 - Small Factors的相关文章

UVA - 884 Factorial Factors

题目: 这个题目,我先开始尝试直接求出每个素因子的次数,然后全部加起来. 代码: #include<iostream> using namespace std; int degree_in_fact(int m, int p) { if (m)return degree_in_fact(m / p, p) + m / p; return 0; } bool isprime(int n) { if (n == 2)return true; if (n % 2 == 0)return false;

UVA 160 - Factors and Factorials

 Factors and Factorials  The factorial of a number N (written N!) is defined as the product of all the integers from 1 to N. It is often defined recursively as follows: Factorials grow very rapidly--5! = 120, 10! = 3,628,800. One way of specifying su

UVA 1575 Factors

https://vjudge.net/problem/UVA-1575 题意: 令f(k)=n 表示 有n种方式,可以把正整数k表示成几个数的乘积的形式. 例 10=2*5=5*2,所以f(10)=2 给出n,求最小的k 搜索 从最小的质数开始枚举选几个 假设前i-1个种质数用了k个,有sum种方案,第i种质数选a个, 那么前i种质数的方案就有sum*C[k+a][a] 可以理解原来有k个位置,又加了a个位置,有a个数可以放在任意位置 所以前i种的每一种方案都变成C[k+a][a]种 枚举每个质

UVA - 10791 - Minimum Sum LCM (数论相关!)

题目链接:Minimum Sum LCM UVA - 10791 Minimum Sum LCM Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu SubmitStatus Description  Minimum Sum LCM  LCM (Least Common Multiple) of a set of integers is defined as the minimum number, whic

uva 11610 Reverse Prime

Problem FReverse Prime Input: Standard Input Output: Standard Output There are a few 7 digit positive numbers whose reverse number is a prime number and less than 10^6.  For example: 1000070, 1000090 and 1000240 are first few reverse prime numbers be

UVA 136 Ugly Numbers

原题代号:UVA 136 原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=72 题目原题: Ugly Numbers Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5,

UVA - 586 Instant Complexity

Description  Instant Complexity  Analyzing the run-time complexity of algorithms is an important tool for designing efficient programs that solve a problem. An algorithm that runs in linear time is usually much faster than analgorithm that takes quad

UVA 10168 Summation of Four Primes(数论)

Summation of Four Primes Input: standard input Output: standard output Time Limit: 4 seconds Euler proved in one of his classic theorems that prime numbers are infinite in number. But can every number be expressed as a summation of four positive prim

UVA 136 &amp; POJ1338 Ugly Numbers

题目大意: Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, - shows the first 10 ugly numbers. By convention, 1 is included. 把只含有2.3.5因数的数称为丑数,默认第一个丑数是1. POJ是有多次询问,输出第n个丑数 UVA是询问第1500个丑数是多少. 思路: