-1

So, the problem is a part of a past programming competition paper I came across. Given a number N representing the number of villages around a city-center, the villagers of the first village go to the city every x1 to days to buy their supplies, the villagers of the second village every x2 days etc.. All the villagers met today in the city-center. We are asked to find when the villagers of at least N-1 villages will visit the city-center at the same day again!

EXAMPLES:

EXAMPLE 1:

INPUT 1: 1 2 3 4 5 6 7 8 9 10 -> OUTPUT:360 7 In the above example, all the villagers except those of the 7th village will meet again after 360 days.

EXAMPLE 2:

INPUT: 10 14 15 30 21 5 40 4 8 -> OUTPUT: 840 0 (all the villagers will meet again after 840 days).

EXAMPLE 3:

INPUT:25065 3575 12305 88590 1758 -> OUTPUT: 25845383485350 4 (so after 25845383485350 all the villagers except those of the 4th village will meet again).

Is there a solution approach that does not use recursion? Thanks!

dituoa
  • 65
  • 7
  • You said all the villagers will meet again..then you again said except 4th village etc. – user2736738 Mar 03 '18 at 14:51
  • There is *always* a solution that does not use recursion, because any recursive algorithm can be rewritten as a sequential algorithm. – John Bollinger Mar 03 '18 at 14:51
  • 4
    Could you explain how the solution is different from finding the smallest common multiple of the given numbers? The part with the "except village 4" confuses me. – Yunnosch Mar 03 '18 at 14:53
  • But I don't see why this particular problem would warrant recursion in the first place, or why it is tagged [graph-algorithm]. – John Bollinger Mar 03 '18 at 14:53
  • It is indeed *LCM* but except parts are something that mess with it. – user2736738 Mar 03 '18 at 14:54
  • I note that in example 3, it won’t be the same villagers who meet; the originals will all be dead! – Jonathan Leffler Mar 03 '18 at 14:56
  • Would it be to find LCM of all, execpt the one which causes the biggest growth of the result? But then how come that there is no "except" for example 2? – Yunnosch Mar 03 '18 at 14:57
  • Where's the C++ code this question seems to be about? – moooeeeep Mar 03 '18 at 14:57
  • @Yunnosch Note in example 2, on the next day at least N-1 villages are there, all N villages are there. But yeah, strange that the examples don't match the problem statement. – aschepler Mar 03 '18 at 16:18
  • i edited the post, my mistake. the required result is the day when at least N-1 villages will meet together again. – dituoa Mar 03 '18 at 17:40
  • @dituoa Updated my answer to your clarification of the question. – Aziuth Mar 03 '18 at 18:31

1 Answers1

1

Your problem is essentially the least common multiple. (If the villagers of a city visit every 5 days and they have visited on day 0, they will also visit on days 5, 10, 15, 20... those are clearly multiples.)

Therefore, a good way to solve this is to split the numbers in prime factors and of each prime factor use the maximally found potency.

With example 2:

10, 14, 15, 30, 21, 5, 40, 4, 8 = 2*5, 2*7, 3*5, 2*3*5, 3*7, 5 2^3*5, 2^2, 2^3

-> taking the maximal potencies: 2^3*3*5*7 = 840

If you alter the problem to have offsets ("the villagers of village 1 were in the city 10 tens ago"), you could use the chinese remainder theorem, by the way.

Edit:

As for the N-1 villages variant, go like that:

Build the prime factors again but this time, count which potency of which prime number is found how often. Create the maximal potencies again and then iterate through the villages to see how long it would take for the villages to meet ignoring that particular village. Having the count of potency, you can easily see the effect of removing the village.

I think knowing this idea is enough to write the code, but if you need me to elaborate just say so.

Elaborating on that one:

For every prime number, store which potency is found how often.

With example 2:

10, 14, 15, 30, 21, 5, 40, 4, 8 = 2*5, 2*7, 3*5, 2*3*5, 3*7, 5 2^3*5, 2^2, 2^3

  • For the 10 = 2*5, we store:
  • 2 has the potencies 1 (x1)
  • 5 has the potencies 1 (x1)

  • For the 14 = 2*7, we update this to:

  • 2 has the potencies 1 (x2)
  • 5 has the potencies 1 (x1)
  • 7 has the potencies 1 (x1)

-> at the end:

  • 2 has the potencies 1 (x3), 2 (x1), 3 (x2)
  • 3 has the potencies 1 (x3)
  • 5 has the potencies 1 (x5)
  • 7 has the potencies 1 (x2)

Now we go through the whole list again, take out the current potencies if they are found only once and then see their product. We keep the minimum of this product.

We initialize the minimum with the whole product, in this case 840.

  • We evaluate 10 = 2*5. Neither the potency 1 of 2 nor the potency 1 of 5 is found only once, so the product stays 840.
  • Same holds for all other numbers, except for the potency 2 of 2, as found in 4 = 2^2
  • But since we also have a potency 3 of 2, that is irrelevant and we stay with our 840.

Let's make our own example: 72, 74, 75 and 296 = 2^3*3^2 2*37 3*5^2 and 2^3*37

->

  • 2 has the potencies 1 (x1), 3 (x2)
  • 3 has the potencies 2 (x1), 5 (x1)
  • 5 has the potencies 2 (x1)
  • 37 has the potencies 1 (x2)

Until all villages meet again, we have to wait (2^3)(3^5)(5^2)*37 = 1798200 days

  • If we take out the 72, we loose one potency 3 of 2 and one 2 of 3. The first one is irrelevant since it exists more than once, the second is too since there is a higher potency of 3 (^5).
  • If we take out the 74, the same holds
  • If we take out the 75 however, we find that it's potency 2 of 5 is singular and we can take it out. (2^3)*(3^5)*37 = 71928, which is our new minimum
  • The 296 is irrelevant again, since 2^3 is found twice and 37^1 too.

Thus, we get that the next time that N-1 villages meet is after 71928 days, when everybody but 75 is present.

Did that help?

Aziuth
  • 3,652
  • 3
  • 18
  • 36
  • Is it possible that you elaborate on the second part? perhaps with some pseudocode? Because I don't exactly understand what you are suggesting.. I would be grateful! – dituoa Mar 09 '18 at 16:49
  • @dituoa Edited. Went with two examples instead of pseudo code, hope that this helps you. – Aziuth Mar 10 '18 at 13:51