UVa 10015 - Joseph's Cousin

10015 - Joseph's Cousin

 * http://acm.uva.es/p/v100/10015.html

Summary
This problem involves primes and modular arithmetic, as well as a circular data structure.

Explanation
The problem is poorly specified, but what it is asking you to do is move around a circle some number of times, and kill the person you stop on. You keep passing around the circle until there is only one person left, and this is the person you output.

The number of times you move around the circle is determined by the next prime number in the sequence $${2,3,5,7,11,\ldots}$$.

What the problem intends you to do is start at the first person, and always start your count at $$1$$. Then, move around the circle, counting up for each person you get to that's still alive. So at the very beginning, if you have $$6$$ people, then your sequence is $$ 1 2 3 4 5 6$$ (realizing that it is circular, so $$6$$ is next to $$1$$). Since the first prime number is 2, then you start at person $$1$$, count up to $$2$$ by going to the next person, then kill person $$2$$. Now you're effectively at person $$3$$, because you've just killed person $$2$$. The next prime number is $$3$$, which means you're at person $$3$$, then go to person $$4$$, and end at person $$5$$ and so person $$5$$ gets killed. The next prime number is $$5$$, and the sequence of people you traverse is $$6 1 3 4 6$$. Since you end on person $$6$$, that's who gets killed.

The thing that's important to note is that you don't have to simulate traversing through the people left, but you can do it with one big jump using addition and mod. You know you're jumping p people, where p = prime[i], where i is the $$i^{th}$$ person that's being killed. Then go back one since you start counting at $$1$$, not $$0$$. Then you mod the result by the number of people left, to make sure you stay in bounds.

Make sure to keep track of your current location, starting at 0, and always make your jumps from there, as well as keeping track of which person you're killing, incrementing after each person (which will increment the prime you jump by).

The best way to store the people is as a linked list, which gives constant insertion and linear removal, and keeps the size of the list stored nicely for you to mod by every time.

Also, you should use a prime sieve at the beginning to get the first 3501 primes (ends up needing up to about 32,650).

Input
6 0

Output
4