-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFloyd_Cycle_Finding_Algorithm.cpp
More file actions
61 lines (61 loc) · 982 Bytes
/
Copy pathFloyd_Cycle_Finding_Algorithm.cpp
File metadata and controls
61 lines (61 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <stack>
#include <vector>
#include <fstream>
#include <map>
#include <bitset>
#include <cstring>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> ii;
#define ll long long int
#define inf 2147483647
#define pi acos(-1)
int z, l, i, m;
int f(int x)
{
return (z*x+i)%m;
}
ii solve()
{
int t=f(l), h=f(f(l));
while(t!=h)
{
t=f(t);
h=f(f(h));
}
h=l;
int mu=0;
while(h!=t)
{
h=f(h);
t=f(t);
mu++;
}
h=f(t);
int lambda=1;
while(h!=t)
{
h=f(h);
lambda++;
}
return ii(mu,lambda);
}
int main()
{
int t=1;
while(1)
{
scanf("%d %d %d %d", &z, &i, &m, &l);
if(z==0 && i==0 && m==0 && l==0)
break;
ii ans=solve();
printf("Case %d: %d\n", t++, ans.second);
}
return 0;
}