Skip to main content
  1. Posts/

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

·1 min
Table of Contents
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

455. Sequence analysis
#

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

直接开了个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    }

Related

SGU 456 Annuity Payment Scheme

·1 min
水题,推个公式出来,注意精度…一遍A 1 2 /************************************************************************* 3 > File Name: code/2015summer/#5/D.cpp 4 > Author: 111qqz 5 > Email: rkz2013@126.com 6 > Created Time: 2015年07月30日 星期四 13时17分26秒 7 ************************************************************************/ 8 9 #include<iostream> 10 #include<iomanip> 11 #include<cstdio> 12 #include<algorithm> 13 #include<cmath> 14 #include<cstring> 15 #include<string> 16 #include<map> 17 #include<set> 18 #include<queue> 19 #include<vector> 20 #include<stack> 21 #define y0 abc111qqz 22 #define y1 hust111qqz 23 #define yn hez111qqz 24 #define j1 cute111qqz 25 #define tm crazy111qqz 26 #define lr dying111qqz 27 using namespace std; 28 #define REP(i, n) for (int i=0;i<int(n);++i) 29 typedef long long LL; 30 typedef unsigned long long ULL; 31 const int inf = 0x7fffffff; 32 int s,m,p; 33 double ans; 34 35 double cal(double x,int n) 36 { 37 double res = 1.0; 38 for ( int i = 1 ; i <= n ; i++ ) 39 { 40 res = res * x; 41 } 42 // cout<<"res:"<<res<<endl; 43 return res; 44 } 45 int main() 46 { 47 cin>>s>>m>>p; 48 double sum = 0; 49 double per = p*1.0/100+1; 50 for ( int i = 1 ; i <= m; i++ ) 51 { 52 sum=sum+1.0/cal(per,i); 53 // cout<<"sum:"<<sum<<endl; 54 } 55 // cout<<sum<<endl; 56 cout<<fixed<<setprecision(5)<<s*1.0/sum<<endl; 57 58 return 0; 59 }

SPOJ AMR10F Cookies Piles

·1 min
AMR10F - Cookies Piles # 水. 1 2 3 /************************************************************************* 4 > File Name: code/2015summer/#4/F.cpp 5 > Author: 111qqz 6 > Email: rkz2013@126.com 7 > Created Time: 2015年07月29日 星期三 21时47分23秒 8 ************************************************************************/ 9 10 #include<iostream> 11 #include<iomanip> 12 #include<cstdio> 13 #include<algorithm> 14 #include<cmath> 15 #include<cstring> 16 #include<string> 17 #include<map> 18 #include<set> 19 #include<queue> 20 #include<vector> 21 #include<stack> 22 #define y0 abc111qqz 23 #define y1 hust111qqz 24 #define yn hez111qqz 25 #define j1 cute111qqz 26 #define tm crazy111qqz 27 #define lr dying111qqz 28 using namespace std; 29 #define REP(i, n) for (int i=0;i<int(n);++i) 30 typedef long long LL; 31 typedef unsigned long long ULL; 32 const int inf = 0x7fffffff; 33 int main() 34 { 35 int T; 36 int n,a,d; 37 cin>>T; 38 while (T--) 39 { 40 scanf("%d %d %d",&n,&a,&d); 41 cout<<n*a+n*(n-1)/2*d<<endl; 42 } 43 44 return 0; 45 }

poj 2823 Sliding Window (单调队列)

·4 mins
Sliding Window 看这个问题:An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position.Your task is to determine the maximum and minimum values in the sliding window at each position.