Problem 539: Odd elimination

Start from an ordered list of all integers from 1 to n.
Going from left to right, remove the first number and every other number afterward until the end of the list.
Repeat the procedure from right to left, removing the right most number and every other number from the numbers left.
Continue removing every other numbers, alternating left to right and right to left, until a single number remains.

Starting with n = 9, we have:1 2 3 4 5 6 7 8 9
2 4 6 82 6
6

Let P(n) be the last number left starting with a list of length n.
Let S(n) = sum_{k=1}^n{P(k)}

You are given P(1)=1, P(9) = 6, P(1000)=510, S(1000)=268271.

Find S(10^18) mod 987654321.

Not solved yet

My code works for the "small" example but can't find the solution to the "big" problem.

What I've Done So Far

The brute-force code to find P(n) filters an array and returns the last number (see bruteForceP).bruteForceS is a simple loop and calls bruteForceP.
These two functions can solve S(1000) quite fast but anything beyond n = 10^5 would take forever.

Looking at the values produces by bruteForceP I spotted a pattern after a while:

two consecutive numbers P(x) and P(x+1) are always P(x)=P(x+1) if x is even (it's a multiple of 2)

two consecutive numbers P(x) and P(x+2) are always P(x)=P(x+2)-2 if x is a multiple of 4

combining both: P(x+1) = P(x+3) - 2 if x is a multiple of 4

for whatever reason P(2) is an exception

The first values of P(n) are:n12345678910111213141516P(n)1222244668866886

Even more interesting, P(x) = 4 * P(x div 4) if x-2 or x-3 is a multiple of 4.
If x or x-1 is a multiple of 4 then I have to subtract 2.

Now I can easily compute P(10^18) but S(10^18) would still require a loop with 10^18 iterations.
It's easy to compute 4 numbers at once, but it doesn't help much because S(10^18) is still out of reach.S(10^8) is found in a few seconds and OpenMP might help me to solve even S(10^10) in under a minute.

I think that computing more than four numbers at once is possible but can't deduce a nice pattern.
So far my solution was based on a "lucky" discovery and I'm afraid that I don't get lucky twice on the same problem.

Interactive test

You can submit your own input to my program and it will be instantly processed at my server:

Input data (separated by spaces or newlines):Note: I didn't solve this problem yet. The output will most likely be a little bit off or time-out.

This is equivalent toecho 1000 | ./539

Output:

(please click 'Go !')

Note: the original problem's input 1000000000000000000cannot be enteredbecause just copying results is a soft skill reserved for idiots.

(this interactive test is still under development, computations will be aborted after one second)

My code

… was written in C++11 and can be compiled with G++, Clang++, Visual C++. You can download it, too.

The 310 solved problems (that's level 12) had an average difficulty of 32.6&percnt; at Project Euler and
I scored 13526 points (out of 15700 possible points, top rank was 17 out of &approx;60000 in August 2017)
at Hackerrank's Project Euler+.

My username at Project Euler is stephanbrumme while it's stbrumme at Hackerrank.

Copyright

I hope you enjoy my code and learn something - or give me feedback how I can improve my solutions.All of my solutions can be used for any purpose and I am in no way liable for any damages caused.You can even remove my name and claim it's yours. But then you shall burn in hell.

The problems and most of the problems' images were created by Project Euler.Thanks for all their endless effort !!!

more about me can be found on my homepage,
especially in my coding blog.
some names mentioned on this site may be trademarks of their respective owners.
thanks to the KaTeX team for their great typesetting library !