Random paths P33549


Statement
 

pdf   zip

A random walk consists of a sequence of positions, each one obtained from the previous one by making a random step. In this exercise we consider random walks in the plane, starting at (0,0)(0, 0). Let kk be a strictly positive natural number. Each step will be an increment between k-k and kk, random and independent, of the two coordinates.

Hence, we need a source of randomness. The usual way to simulate it consists in generating pseudorandom numbers. These numbers are the result of an algorithm, so they are not really random, but they look random enough. The linear congruential generators are defined with four natural numbers mm (module), aa (multiplier), bb (adder) and ss (initial seed). The generated sequence is x1=(a*s+b) mod m,x2=(a*x1+b) mod m,x3=(a*x2+b) mod m,x_1 = (a*s + b) \mbox{ mod } m, \quad x_2 = (a*x_1 + b) \mbox{ mod } m, \quad x_3 = (a*x_2 + b) \mbox{ mod } m, \quad \dots

For instance, if m=9,a=2,b=7,s=3m = 9, a = 2, b = 7, s = 3, then we get x1=(2*3+7) mod 9=4,x2=(2*4+7) mod 9=6,1,0,7,3,4,6,x_1 = (2*3 + 7) \mbox{ mod } 9 = 4, \quad x_2 = (2*4 + 7) \mbox{ mod } 9 = 6, \enspace 1, \enspace 0, \enspace 7, \enspace 3, \enspace 4, \enspace 6, \enspace \dots

These numbers are between 0 and m1m - 1, but in this exercise we need numbers between k-k and kk. The easiest way to achieve this is with the following code; use it just like that:

    int random(int k, int m, int a, int b, int& s) {
        s = (a*s + b)%m;
        return s%(2*k + 1) - k;
    }

Following with the example, for k=2k = 2 the sequence of increments is 4 mod 52=2,6 mod 52=1,1 mod 52=1,2,0,1,2,1,4 \mbox{ mod } 5 \, - \, 2 = 2, \enspace 6 \mbox{ mod } 5 \, - \, 2 = -1, \enspace 1 \mbox{ mod } 5 \, - \, 2 = -1, \enspace -2, \enspace 0, \enspace 1, \enspace 2, \enspace -1, \enspace \dots and, if we increase the first coordinate before the second one, the steps are (0,0),(2,1),(1,3),(3,3),(0, 0), \enspace (2, -1), \enspace (1, -3), \enspace (3, -3), \enspace \dots

Write a program to compute the first nn steps of a sequence of random walks defined by kk, mm, aa, bb and ss.

Input

Input is all natural numbers, and consists of several cases, each one in two lines. The first line contains nn and kk. The second line contains mm, aa, bb and ss. All nn, kk and mm are strictly positive, and aa, bb and ss are less than mm.

Output

For each case of the input, first print its number starting at 1, followed by the walk of nn steps defined by kk, mm, aa, bb and ss. If some position gets repeated, indicate it and stop the walk as it is shown in the example. Print an empty line at the end of each case.

Public test cases
  • Input

    5 2
    9 2 7 3
    8 1
    7 2 0 5
    12 100
    1007 74 985 333
    

    Output

    Path #1
    (0, 0)
    (2, -1)
    (1, -3)
    (1, -2)
    (3, -3)
    
    Path #2
    (0, 0)
    (-1, -1)
    (0, -2)
    (-1, -1) repeated!
    
    Path #3
    (0, 0)
    (-50, 95)
    (-41, 156)
    (-19, 202)
    (73, 102)
    (94, 14)
    (-4, -18)
    (-16, -102)
    (11, -7)
    (-10, 79)
    (51, 73)
    (122, 1)
    
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++