码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-07-30 23:08:33      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

455. Sequence analysis

Time limit per test: 1 second(s)
Memory limit: 4096 kilobytes
input: standard
output: standard


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 xmthat 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.

Example(s)
sample input
sample output
2 2 9
4

 

sample input
sample output
2305843009213693951 1 9223372036854775807
5

 

sample input
sample output
-2 1 5
4




Note

In the first sample test the sequence starts with the following numbers: 1, 3, 7, 6, 3, 7. The first repeatable element is 3. The second occurence of 3 has index 4. 

In the second sample test the sequence starts with the following numbers: 1, 2305843009213693951, -4611686018427387903, 6917529027641081855, 0, 0, 0. The first repeatable element is 0. The second occurence of 0 has index 5. 

In the third sample test the sequence starts with the following numbers: 1, -2, 4, -3, 1, -2, 4. The first repeatable element is 1. The second occurence of 1 has index 4.

 

比赛的时候逗了,往看空间限制了....

直接开了个set判重。。。显然MLE 了。。。

然后这道题的正解是 floyd判圈算法(也叫龟兔算法?)

http://www.cnblogs.com/oyking/p/4286916.html  这份题解讲得很详细

 1 /*************************************************************************
 2     > File Name: code/2015summer/#5/CC.cpp
 3     > Author: 111qqz
 4     > Email: rkz2013@126.com 
 5     > Created Time: 2015年07月30日 星期四 21时02分17秒
 6  ************************************************************************/
 7 #include<iostream>
 8 #include<iomanip>
 9 #include<cstdio>
10 #include<algorithm>
11 #include<cmath>
12 #include<cstring>
13 #include<string>
14 #include<map>
15 #include<set>
16 #include<queue>
17 #include<vector>
18 #include<stack>
19 #define y0 abc111qqz
20 #define y1 hust111qqz
21 #define yn hez111qqz
22 #define j1 cute111qqz
23 #define tm crazy111qqz
24 #define lr dying111qqz
25 using namespace std;
26 #define REP(i, n) for (int i=0;i<int(n);++i)  
27 typedef long long LL;
28 typedef unsigned long long ULL;
29 const int inf = 0x7fffffff;
30 const int LIM = 2E6;
31 LL a,b,c;
32 LL next (LL x)
33 {
34     return (a*x+x%b)%c;
35 }
36 int main()
37 {
38     cin>>a>>b>>c;
39     LL x = next(1);
40     LL y = next(next(1));
41     int v = 1;
42     while (v<=LIM &&x!=y)
43     {
44     x = next(x);  //乌龟走一步
45     y = next(next(y)); //兔子走两步
46     v++;
47     }
48     if (v>LIM)   //在规定范围内找不到循环节(兔子没有和乌龟到同一个位置)
49     {
50     puts("-1");  
51     return 0;
52     }
53 
54     x = 1;
55     int mu = 0;  //找到循环节的起点
56     while (x!=y) 
57     {
58     x = next(x);
59     y = next(y);
60     mu++;
61     }
62     
63     int lam = 1; //循环节的长度
64     y = next(x);
65     while (mu+lam <= LIM  &&x!=y)
66     {
67     y = next(y);
68     lam++;
69     }
70     if (mu+lam<=LIM)
71     {
72     printf("%d\n",mu+lam);
73     }
74     else
75     {
76     puts("-1");
77     }
78 
79     return 0;
80 }

 

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

标签:

原文地址:http://www.cnblogs.com/111qqz/p/4690649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!