tma2002's blog

By tma2002, history, 11 years ago, In English

Given N ( 2 < N < 1000 ) is a positive integer.

Jack had written a program to find k numbers a1,a2,a3,...ak, so as:

(a1+1) * ( a2+1 ) * .... * (ak+1) = n*a1*a2*...*ak Find the minimum k.

Example: INP: 4 OUT: 2

a1=a2=1, (1+1) * (1+1) = 2*2 = 4 = 4*a1*a2 = 4*1*1 = 4

  • Vote: I like it
  • +7
  • Vote: I do not like it

| Write comment?
»
11 years ago, hide # |
← Rev. 6  
Vote: I like it 0 Vote: I do not like it

From the expression:
(a1 + 1)(a2 + 1)..(ak + 1) = n * a1 * a2 * .. * ak

<=>

Consider :

  • is positive integer
  • gcd(ai, ai + 1) = 1
    =>

So we will create a directed graph that vertex ith has value ni. Two vertexs ni and nj are adjacent if there is exist a positive integer am that and . Then minimum value of k is the shortest path between vertex value n and vertex value 1. We can you Breadth-first search to find the shortest path.

P/s: I remember that this is PTNK problem for grade 9 right?