Iarcs problem archive- Find the Numbers

link: http://opc.iarcs.org.in/index.php/problems/FINDNUMS

My approach was to factorize ‘P’ and as it is small to brute force over all combinations of divisors.
For example for S=11, P=48 and k=3 (Shown example) I would check (1,1,48),(1,2,24),(1,3,16) and so on till i reached (3,4,4) which was the answer. Could someone suggest some method to enumerate all the sets of the divisors of the number?

Any other solution too would be of great help.

Simple brute force works: http://pastebin.com/M4CPvVZ6

Note: don’t worry if you get wrong answer when submitting on the server, the test data is wrong.

1 Like

You could also do it recursively.

The base case being, if K = 1, then if S = P, the answer is yes, otherwise no.

Basically you are reducing the actual problem to the following sub problem.

You’re given S, P, K.

if K=1
   if S=P
     return true i.e. answer is yes
   else
     return false i.e. answer is no

For i = 1 to N

If P is divisible by i, 

Include i in list of numbers

then solve for S-i, P/i and K-1 recursively

About the test data, there’s extra information missing in the problem statement. Nothing about the ordering of the numbers N1, N2, …, NK has been explicitly mentioned in the problem statement. That is:

If one of N1, N2, …, NK is 1, print in descending order. Otherwise, print the numbers in ascending order.

2 Likes

Have you deduced by looking at the testdata? :stuck_out_tongue:

Yeah I got 70 points initially, then looked at the test data :stuck_out_tongue:

I don’t think they actually follow this rule when printing output, it’s just coincidence probably.

The order matters. In your code, if you intentionally print i before j and k, it won’t give correct answer.

Yeah I was initially printing i, j, k in that order but I changed it around to check if the order mattered. (it shouldn’t if the testdata is correct)

2 Likes