# 寻找循环的问题

## 1. Introduction

(在这个可视化中，我们限制为 f(x) = (A*x^2 + B*x + C) % Mx0 % M，因此函数 f 的定义域和值域 ∈ [0..M-1])。

{x0, x1 = f(x0), x2 = f(x1), ..., xi = f(xi-1), ...}

mu (μ) 是最小的索引，设 lambda (λ)（循环长度）是最小的正整数，使得 xμ = xμ+λ

## 2. 自定义输入

1. 随机：函数将为 f(x) = (x2 - 1) % M，只有 Mx0 是随机生成的。
2. 自定义：您可以指定 f(x) 的系数 ABC（范围从 -999 到 999），模数值 M（范围从 10 到 1000）和初始值 x0（范围从 0 到 M-1）。

## 3. Floyd's 乌龟 - 野兔算法

Floyd's Tortoise-Hare Cycle-Finding is one algorithm that can solve this problem efficiently in both time and space complexities.

It just requires O(μ+λ) time and O(1) space to do the job.

### 3-1. 可视化

This is the visualization of Floyd's Tortoise-Hare Cycle-Finding algorithm.

The shape of rho (ρ) of the sequence of iterated function values defined by f(x) and x0 nicely visualizes μ and λ.

VisuAlgo uses green vertices to represent the tortoise (t) and orange vertices to represent the hare (h).

## 5. 实现

You are allowed to use/modify our implementation code for Floyd Cycle-Finding Algorithm:
UVa00350.cpp
UVa00350.java
UVa00350.py
UVa00350.ml