Lucky Tickets 109

Back to Problem Solutions forum

Igor Shapovalov     2020-11-13 23:21:44

Can someone explain me what do I have to do in this task please? In example we have N=4 B=3. Like I understood

it means that we have the ticket with 4 digits and in any digits we can put only 0-2 digit because base is 3?

Igor Shapovalov     2020-11-14 00:35:17

My recursive take a lot of time, how I can optimize it?

qwerty     2020-11-14 19:42:41

Yeah, you are right. With N = 4 and B = 3 you have 4 digits in range 0..2.

And lucky tickets for this input are:

0000

0101
0110
1001
1010

0202
0211
0220
1102
1111
1120
2002
2011
2020

1212
1221
2112
2121

2222

(19 tickets in total)

qwerty     2020-11-14 19:48:59

Sadly, I don't remember the algorithm I used to solve this task. Gotta investigate my source code for this task with pencil and paper and find out what the hell I was doing in my program :).

qwerty     2020-11-14 21:10:16

Wrote a lengthy description of my algorithm, and suddenly the light turned off. Sigh.

To be very short:

F(2k + 1) = B * F(2k)
F(2k) = P(C(k))
And I'm not going to write a description for P() and C(k) second time.

Time complexity of my algorithm is very bad, it is: O(math.power(B, math.floor(N / 2))).

Igor Shapovalov     2020-11-14 22:14:04

Okay, I did it, thank you)

Please login and solve 5 problems to be able to post at forum