SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)

题目链接: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) run much slower than the same code written in C++ or Java. We do not 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 are evaluated without overflow checking. Use standard "remainder" operator for programming languages (it differs from the mathematical version; for example  in programming, while  in mathematics). Use "

long long

" type in C++, "

long

" in Java and "

int64

" in Delphi to store xi and all other values.

Let‘s call a sequence element xp repeatable if it occurs later in the sequence — meaning that there exists such qq > p, that xq = xp. The first repeatable element M of the sequence is such an element xm that xm is repeatable, and none of the xp where p < m are repeatable.

Given AB and C, your task is to find the index of the second occurence of the first repeatable element M in the sequence if the index is 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: AB and C (B > 0, C > 0).

Output

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

题目大意:给出x[0] = 1,还有A、B、C,有x[i+1] = (A * x[i] + x[i] % B) % C。求第二个循环节出现的位置。

思路:http://en.wikipedia.org/wiki/Cycle_detection#Tortoise_and_hare (floyd判圈算法,龟兔算法?)

给一个初始值x[0],有一个函数x[i + 1] = f(x[i])。若x始终在有限集合中运算,那么这些x值会构成一个环。

在此题中,整数模一个C后,显然得到的值是有限的。

设第一个循环节从x[μ]开始,循环节长度为λ

那么对任意i = kλ ≥ μ,一定会有x[kλ] = x[kλ + kλ],即x[i] = x[2i]

于是我们可以用以下代码,找出满足x[v] = x[2v]的第一个ν,显然在[μ, μ + λ]间存在一个v = kλ,则此步复杂度为O(μ+λ)

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

由于对任意i ≥ μ,有x[i] = x[i + λ] = x[i + kλ],那么同意有x[μ] = x[μ + λ] = x[μ + kλ] = x[μ + v]

在上面的代码中,我们已经求得了x[v],其中两个变量都等于x[v]

那么基于x[μ] = x[μ + v]的事实。我们可以用下面的代码,循环μ次,求出x[μ]

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

上述代码已经求出μx[μ]。最后,只要再循环λ遍,即可求出循环节长度:

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

总时间复杂度为O(μ+λ),空间复杂度为O(1)

对于本题,输出μ+λ即可。

至于本题的计算过程可能溢出的事情我没想……我发现大家都没管,我也不管了……

代码(312MS):

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4
 5 const int MAXR = 2e6;
 6
 7 LL A, B, C;
 8
 9 LL next(LL x) {
10     return (A * x + x % B) % C;
11 }
12
13 int main() {
14     scanf("%I64d%I64d%I64d", &A, &B, &C);
15     LL x = next(1), y = next(x);
16     int v = 1;
17     while(v <= MAXR && x != y)
18         x = next(x), y = next(next(y)), v++;
19     if(v > MAXR) {
20         puts("-1");
21         return 0;
22     }
23
24     x = 1;
25     int mu = 0;
26     while(x != y)
27         x = next(x), y = next(y), mu++;
28
29     int lam = 1;
30     y = next(x);
31     while(mu + lam <= MAXR && x != y) y = next(y), lam++;
32
33     if(mu + lam <= MAXR) printf("%d\n", mu + lam);
34     else puts("-1");
35 }

时间: 2025-01-16 04:02:20

SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)的相关文章

sgu 455. Sequence analysis (floyd 判圈算法,O(1)空间复杂度求循环节)

455. Sequence analysis Time limit per test: 1 second(s)Memory limit: 4096 kilobytes input: standardoutput: standard Due to the slow 'mod' and 'div' operations with int64 type, all Delphi solutions for the problem 455 (Sequence analysis) run much slow

Floyd判圈算法

Floyd判圈算法(Floyd Cycle Detection Algorithm),又称龟兔赛跑算法(Tortoise and Hare Algorithm),是一个可以在有限状态机.迭代函数或者链表上判断是否存在环,求出该环的起点与长度的算法.该算法据高德纳称由美国科学家罗伯特·弗洛伊德发明,但这一算法并没有出现在罗伯特·弗洛伊德公开发表的著作中.  如果有限状态机.迭代函数或者链表上存在环,那么在某个环上以不同速度前进的2个指针必定会在某个时刻相遇.同时显然地,如果从同一个起点(即使这个起

leetcode202(Floyd判圈算法(龟兔赛跑算法))

Write an algorithm to determine if a number is "happy". 写出一个算法确定一个数是不是快乐数. A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat th

[LeetCode] 287. Find the Duplicate Number(Floyd判圈算法)

传送门 Description Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You mu

uva 11549计算器谜题(floyd判圈算法)

 题意:有个老式计算器,每次只能记住一个数字的前n位.现在输入一个整数k,然后反复平方,一直做下去,能得到的最大数是多少.例如,n=1,k=6,那么一次显示:6,3,9,1... 思路:这个题一定会出现循环,所以一个个模拟,遇到相同的就再之前所有数中找最大的输出即可. 最容易想到的就是set判重,一开始k直接生算每次除十......超时 然后看了书,用string,ac,很方便但是时间达到了4s,果然string和stringstream好慢啊......... 又改成了记录k*k的每一位,

UVa 11549 计算器谜题(Floyd判圈算法)

https://vjudge.net/problem/UVA-11549 题意: 有一个老式计算器,只能显示n位数字,输入一个整数k,然后反复平方,如果溢出的话,计算器会显示结果的最高n位.如果一直这样做下去,能得到的最大数是多少? 思路: 这个肯定是会循环的. 比较普通的做法就是用set来判断是否出现过来终止循环. 另一个高效算法:Floyd判圈算法!! 想象一下,假设有两个小孩子在一个“可以无限向前跑”的跑道上赛跑,同时出发,但其中一个孩子的速度是另一个两倍.如果跑到是直的,跑得快的小孩永远

Floyd判圈算法(判断链表是否含环)

Floyd判圈算法 简介 Floyd判圈算法,也称龟兔赛跑算法,可用于判断链表.迭代函数.有限状态机是否有环.如果有,找出环的起点和大小.时间复杂度O(n),空间复杂度O(1). 可以先思考一下,假设有一个圆形的跑道周长为\(C\),A和B从同一个起点,分别以\(v\)和\(2v\)的速度同向出发,可以知道,因为B比A跑得快而且跑道是环形的,所以接下来一定存在某一时刻,B和A相遇,这时候,B跑过的总距离\(S_B\)减去A跑的总距离\(S_A\)一定是\(C\)的整数倍.即: \(S_B-S_A

Floyd判圈算法(判断是否有环)

介意转吗博主~~http://blog.csdn.net/thestoryofsnow/article/details/6822576,我知道不介意啦~ 问题:如何检测一个链表是否有环,如果有,那么如何确定环的起点. 龟兔解法的基本思想可以用我们跑步的例子来解释,如果两个人同时出发,如果赛道有环,那么快的一方总能追上慢的一方.进一步想,追上时快的一方肯定比慢的一方多跑了几圈,即多跑的路的长度是圈的长度的倍数. 基于上面的想法,Floyd用两个指针,一个慢指针(龟)每次前进一步,快指针(兔)指针每

UVA11549 计算机谜题(Floyd判圈算法)

1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<set> 7 #include<sstream> 8 using namespace std; 9 /*int next1(int n,int k) 10 { 11 stringstream ss; 12