Xorshift32
Time limit1sMemory limit256 MB
Given a starting value x and a target t, find the index at which t first appears in the Xorshift32 pseudorandom sequence.
- Level
Hard9 of 10
- Topics
- Math, Bit manipulation, Number theory, Implementation
- Solved
- No attempts yet
Problem
Xorshift32 is a pseudorandom number algorithm that produces a sequence of natural numbers between 1 and 4,294,967,295 inclusive. The period of this sequence is 4,294,967,295, so every natural number from 1 to 4,294,967,295 appears exactly once before the first number of the sequence appears again.
Below is C code that reads the first number of the sequence and prints the sequence generated by the Xorshift32 algorithm.
#include <stdio.h>
#include <inttypes.h>
uint32_t xorshift32(uint32_t x) {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
return x;
}
int main(void) {
uint32_t x;
scanf("%" SCNu32, &x);
for (;;) {
printf("%" PRIu32 "\n", x);
x = xorshift32(x);
}
return 0;
}
For example, the sequence starting with 2,463,534,242 is as follows.
2463534242, 723471715, 2497366906, 2064144800, 2008045182, ...(4,294,967,288 terms omitted), 1232801914, 1174207936, 2463534242, 723471715, 2497366906, ...
Here you can see that 723,471,715 first appears as the 2nd term of this sequence, and 1,232,801,914 first appears as the 4,294,967,294th term.
Sait thought it would be nice to be able to predict at which position a particular number t appears in the Xorshift32 sequence starting with x. Help Sait.
Input
The first line gives two natural numbers x and t. (1 ≤ x, t ≤ 4,294,967,295)
Output
Print the position at which t first appears in the Xorshift32 sequence starting with x.