My Algorithm

I wrote a bruteForce() algorithm in a few minutes which - to my surprise - even solves the M(10000) case in less than a second.
After precomputing the first 10000 pseudo-random values it performs the following tasks for each position:

set minimum to the current value data[i]

go backwards until reaching the first position, compare minimum to data[j] and update it according

add minimum to the result

This O(n^2) algorithm repeatedly processes the same values.
It's more efficient to keep track of the positions where data[j] was smaller than minimum.
For example, if the next minimum is 5 positions away, then I can easily add 5 times the current minimum to result.

The search() function implements this idea:

it has a stack best which contains those numbers smaller than the current number ("previous ''minimum's") and their position in the stream of pseudo-random numbers

this stack is initialized with 0, a number smaller than anything generated by pseudoRandom so that the stack is never empty (prevents a few corner-cases)

whenever a new pseudo-random number called current is generated then I reduce the stack until the top-most element is smaller than current

current and its position i are pushed to the stack

for all elements of the stack I add their value and their distance to the next element of the stack to result

Alternative Approaches

My program needs just under a minute to solve the problem.
I didn't realize that pseudoRandom() has a short cycle:

there can be at most 50515093 different outputs (50515093 is the modulo)

each number depends only on the previous number

whenever the internal state becomes a number I have already seen before then a new cycle starts

if you enable FIND_CYCLE then my code will almost instantly display 6308947.

Pretty much everyone on the Project Euler forum spotted the cycle - I didn't ...
... but it can dramatically reduce execution time:

process the first cycle the same way I did

remember the value of result

process the second cycle and determine how much result changed (let's call it delta)

skip 6308947 iterations of the loop and just add delta to the result

the last cycle will not be complete, process it the same way I did

You will process less than 2 * 10^6 pseudo-random values instead of 2 * 10^9 and will find the result in less than a second (remember, my code takes a minute).
A major drawback is that much more code is required for this faster algorithm.

Note

The stack best remains quite small. It contains less than 40 values at any time.

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):

This is equivalent toecho 10000 | ./375

Output:

(please click 'Go !')

Note: the original problem's input 2000000000cannot 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.

Those links are just an unordered selection of source code I found with a semi-automatic search script on Google/Bing/GitHub/whatever.
You will probably stumble upon better solutions when searching on your own.
Maybe not all linked resources produce the correct result and/or exceed time/memory limits.

Heatmap

Please click on a problem's number to open my solution to that problem:

green

solutions solve the original Project Euler problem and have a perfect score of 100% at Hackerrank, too

yellow

solutions score less than 100% at Hackerrank (but still solve the original problem easily)

gray

problems are already solved but I haven't published my solution yet

blue

solutions are relevant for Project Euler only: there wasn't a Hackerrank version of it (at the time I solved it) or it differed too much

orange

problems are solved but exceed the time limit of one minute or the memory limit of 256 MByte

red

problems are not solved yet but I wrote a simulation to approximate the result or verified at least the given example - usually I sketched a few ideas, too

black

problems are solved but access to the solution is blocked for a few days until the next problem is published

[new]

the flashing problem is the one I solved most recently

I stopped working on Project Euler problems around the time they released 617.

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 !