10710 - Chinese Shuffle

All about problems in Volume 107. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

muvee
New poster
Posts: 25
Joined: Sun Sep 05, 2004 12:41 am

10710

Post by muvee »

My evidently pathetic algorithm that takes about 5 seconds on certain cases is as follows :

Let the input number be N

1) If N is odd , N=N+1
2) Set x = 2 and counter=1
While(x!=1)
{
x = (x<<1)%(n-1)
counter++
}




This is based on the observation that if you label the cards from 0 to n-1, then every card moves from k to (2*k)mod(n-1)

Firstly, is my observation correct that the number of shuffles required is equal to the number of shuffles it takes for card number 2 (having index 1 in using the labelling system I described) to return to index 1.

And if yes, since my algorithm is too slow, any hints are more than welcome!
[/code]
Abednego
A great helper
Posts: 281
Joined: Tue Sep 10, 2002 5:14 am
Location: Mountain View, CA, USA
Contact:

Post by Abednego »

I love this problem, Little Joey! It really shows you that you should think first before writing code. I submitted the Rabin-Miller test first without even thinking that numbers other than primes could be Jimmy-numbers. Of course, it turns out that the problem is much easier than primality testing. :-)
If only I had as much free time as I did in college...
Destination Goa
Experienced poster
Posts: 123
Joined: Thu Feb 10, 2005 4:46 am

Post by Destination Goa »

My solution checks that 2^(N-1) == 1 modulo N (same thoughts about repositioning gave me this fact). I think yours does the same, but it performs O(N) operations to find that power. You should use O(log(N)) algorithm to find 2^(N-1).
Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan

Post by Eduard »

Hello muvee
For Log(N) solution you can first solve problem 374(Big Mod). :roll:
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
Vexorian
Learning poster
Posts: 100
Joined: Sat Aug 26, 2006 5:50 am

10710 I/O ?

Post by Vexorian »

Anyone may assist me solving the WA with some I/O samples I might be missing?

PS: I did notice it doesn't work only on primes.
Post Reply

Return to “Volume 107 (10700-10799)”