Блог пользователя Igor.Ivanich

Автор Igor.Ivanich, 15 лет назад, По-английски

Hi everyone,

Well the problem which I'm going to discuss here is from USACO Chapter 3.

The problem description is given below:

For a given set of K prime numbers S = {p1, p2, ..., pK}, consider the set of all numbers whose prime factors are a subset of S. This set contains, for example, p1, p1p2, p1p1, and p1p2p3 (among others). This is the set of `humble numbers' for the input set S. Note: The number 1 is explicitly declared not to be a humble number.

Your job is to find the Nth humble number for a given set S. Long integers (signed 32-bit) will be adequate for all solutions.

K and N, 1 <= K <=100 and 1 <= N <= 100,000.

 I'm getting TLE due to running O(N ^ 2 * K) algorithm (Simply I'm building the humble number sequence
starting at 1 then at each iteration find the minimum number which is bigger than the last found humble number).

Am i missing something? Or is there a better algorithm for solving this problem?


Thanks in advance.


Igor.
  • Проголосовать: нравится
  • +7
  • Проголосовать: не нравится

»
15 лет назад, скрыть # |
 
Проголосовать: нравится -10 Проголосовать: не нравится
»
15 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
Use priority queue to optimise your solution.
»
15 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
http://hi.baidu.com/leokan/blog/item/1867a4ec32d4052363d09fe0.html

The algorithm described at the bottom of this link runs in O(K * N) time, and I think it can be further improved to O(NlogK) with a priority queue.