# UVa 10015

Jump to navigation Jump to search

## 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 ${\displaystyle {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 ${\displaystyle 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 ${\displaystyle 6}$ people, then your sequence is ${\displaystyle 123456}$ (realizing that it is circular, so ${\displaystyle 6}$ is next to ${\displaystyle 1}$). Since the first prime number is 2, then you start at person ${\displaystyle 1}$, count up to ${\displaystyle 2}$ by going to the next person, then kill person ${\displaystyle 2}$. Now you're effectively at person ${\displaystyle 3}$, because you've just killed person ${\displaystyle 2}$. The next prime number is ${\displaystyle 3}$, which means you're at person ${\displaystyle 3}$, then go to person ${\displaystyle 4}$, and end at person ${\displaystyle 5}$ and so person ${\displaystyle 5}$ gets killed. The next prime number is ${\displaystyle 5}$, and the sequence of people you traverse is ${\displaystyle 61346}$. Since you end on person ${\displaystyle 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 ${\displaystyle i^{th}}$ person that's being killed. Then go back one since you start counting at ${\displaystyle 1}$, not ${\displaystyle 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).

```6
0
```

```4
```