SGU 455 Sequence Analysis (Cycle Detection,floyd judgment Ring algorithm)

Source: Internet
Author: User

Title Link: http://acm.sgu.ru/problem.php?contest=0&problem=455

Due to the slow ' mod ' and ' div ' operations with int64 type, all Delphi solutions for the problem 455 (Sequence analysis) r UN much slower than the same code written in C + + or Java. We don't guarantee that Delphi solution exists.


You are given a sequence of signed 64-bit integers defined as follows:

    • x0 = 1,
    • ,

where

MoD

is a remainder operator. All arithmetic operations is evaluated without overflow checking. Use standard ' remainder ' operator for programming languages (it differs from the mathematical version; for example in pro Gramming, while in mathematics). Use "

Long Long

"Type in C + +,"

Long

"In Java and"

Int64

"In the Delphi to store xi and any other values.

Let's call a sequence element xp Repeatable If it occurs later in the sequence-meaning that there exis ts such q, q > p, that xQ = xp. The first REPEATABLE element M of the sequence is such a element xm that x m is repeatable, and none of the x p where p < m are repeatable.

Given A, B and C, your task is to find the index of the second occurence of the first rep eatable element M in the sequence if the index was less or equal to 2 • 106. Per definition, the first element of the sequence has index 0.

Input

The only line of input contains three signed 64-bit integers: A, b and C (b > 0, c > 0).

Output

Print a single integer-the index of the second occurence of the first repeatable member if it was less or equal to 2 · 106. Print-1 If the index is more than 2 · 106.

The main topic: Give x[0] = 1, there is A, B, C, there is x[i+1] = (A * X[i] + x[i]% B)% C. Find the location where the second loop section appears.

Idea: Http://en.wikipedia.org/wiki/Cycle_detection#Tortoise_and_hare (Floyd algorithm, turtle-Rabbit algorithm?) )

Give an initial value of x[0], there is a function x[i + 1] = f (x[i]). If x is always operating in a finite set, then these x values form a ring.

In this problem, after the integer modulus of a C, obviously the resulting value is limited.

Set the first cycle section from x[μ] and the length of the loop is λ.

Then for any i = kλ≥μ, there must be x[kλ] = x[kλ+ kλ], i.e. x[i] = x[2i].

So we can use the following code to find the first ν satisfying x[v] = x[2v] , obviously there is a v = kλbetween [μ,μ+λ] , then the step complexity is O (μ+λ).

    Long long x = f (x0), y = f (f (x0));    int v = 1;    while (x! = y)        x = f (x), y = f (f (Y)), v++;

due to any i≥μ , there are X[i] = x[i +λ] = x[i + kλ] , then agree to have x[μ] = x[μ+λ] = x[μ+ kλ] = x[μ+ v] .

In the above code, we have obtained X[v], where two variables are equal to x[v].

The fact that based on x[μ] = x[μ+ v] . We can use the following code, loop μ times, to find out x[μ].

    x = x0;    int mu = 0;    while (x! = y)        x = f (x), y = f (y), mu++;

the code above has been calculated μ with the X[μ] . Finally, you can find the length of the cycle section by looping the λ- pass again:

    int lam = 1;    y = f (x);    while (x! = y) y = f (y), lam++;

The total time complexity is O (μ+λ)and the space complexity is O (1).

For the subject, output μ+λ can be.

As for the calculation of the subject may overflow things I did not think ... I found out that everyone was out of control and I didn't care ...

Code (312MS):

1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 5 Const intMAXR =2e6;6 7 LL A, B, C;8 9 ll Next (ll x) {Ten     return(A * x + x% B)%C; One } A  - intMain () { -scanf"%i64d%i64d%i64d", &a, &b, &C); theLL x = Next (1), y =next (x); -     intv =1; -      while(v <= MAXR && x! =y) -x = Next (x), y = Next (Next (y)), v++; +     if(V >MAXR) { -Puts"-1"); +         return 0; A     } at  -x =1; -     intMu =0; -      while(X! =y) -x = Next (x), y = Next (y), mu++; -  in     intLam =1; -y =next (x); to      while(mu + Lam <= MAXR && x! = y) y = next (y), lam++; +  -     if(mu + lam <= MAXR) printf ("%d\n", Mu +Lam); the     ElsePuts"-1"); *}
View Code

SGU 455 Sequence Analysis (Cycle Detection,floyd judgment Ring algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.