20 years ago, you outsmarted your friends into paying a 1000 dollar slurpee bill at the convenience store with a simple game of mods. They are still bitter about that, so they challenge you to a rematch with much higher stakes. After everyone buys their own convenience store, the total comes out to 10 million and 1 dollars. So to decide who pays, everyone plays a game with the following rules:
Everyone picks a different integer N i from 1 to 1 0 , 0 0 0 , 0 0 0 , inclusive
At each round, everyone replaces their number N i with N i ′ , where N i ′ ≡ 1 0 , 0 0 0 , 0 0 1 ( m o d N i ) and 0 ≤ N i ′ < N i
A player loses when they reach 0 (i.e. when 1 0 , 0 0 0 , 0 0 0 ( m o d N i ) ≡ 0 )
Since your mother would get much angrier if she found out that you had to pay the $10,000,001 bill, you decide to choose the unique integer from 0 to 1 0 , 0 0 0 , 0 0 0 that lasts the highest number of rounds. What are the last 3 digits of the integer you choose?
Details and assumptions
As an explicit example, if you chose the number 2 3 , on the first round you would replace 2 3 with 1 0 , 0 0 0 , 0 0 1 ( m o d 2 3 ) ≡ 1 5 , on the second round you would replace 1 5 with 1 0 , 0 0 0 , 0 0 1 ( m o d 1 5 ) ≡ 1 1 , and on the third round you would replace 1 1 with 1 0 , 0 0 0 , 0 0 1 ( m o d 1 1 ) ≡ 0 , so 2 3 would last 3 rounds.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
You are the only one to solve my problem!
Well, my script should be a lot faster than going through the entire game for each number up to 1 0 , 0 0 0 , 0 0 1 . The trick is to store the length of the sequence in a dictionary so when you reach a number twice from 2 different numbers, you don't have to go through the sequence again. For example, after checking the sequence for 1 5 you find that 1 5 lasts for 2 rounds. When you check 2 3 , you see that your next number is 1 5 , and you already found that 1 5 lasts for 2 rounds, so 2 3 lasts for 2 + 1 = 3 rounds. You found this number without checking the sequence for 1 5 twice. Likewise, when you check 1 4 9 , you get 1 5 again so you know immediately 1 4 9 returns 3 .
Here's my code:
mods = {0:0}
def GenerateSequence(n,k):
if n in mods:
return mods[n]
else:
mods[n] = GenerateSequence(k%n,k)+1
return mods[n]
def LongestSequence(k):
for i in range(k):
GenerateSequence(i,k)
maxm = 0
for key in mods:
if mods[key] > maxm:
maxm = mods[key]
allmaxes = []
for key in mods:
if mods[key] == maxm:
allmaxes.append(key)
return allmaxes
print LongestSequence(10**7+1)
runs in ~ 7 seconds
Log in to reply
Also in my script my Longest Sequence function finds the max number of rounds and then goes back and finds all numbers that last that many rounds. That's because when I was designing the problem I wasn't guaranteed to get a unique answer depending on the bound (for example, had I used 1 0 7 rather than 1 0 7 + 1 , there would have been 19 answers).
Rather than doing what I did in my script, if you knew there was a unique answer you could just keep track of the number that returned the highest sequence and overwrite it when you found a number that returned a longer sequence.
Log in to reply
Very nice! I definitely learnt a lot from you. By the way, this problem rocked! Keep posting your fantastic problems.
Problem Loading...
Note Loading...
Set Loading...
This is the Python programme that I wrote. It takes a bit of time before it returns an answer, but it works.
The programme returns 6320726 (last three digits are 726) as the number that will last the highest number of rounds. If anybody has a way of optimising this programme, with regards to speed, please share your knowledge.