-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbit-array.cpp
55 lines (43 loc) Β· 1.04 KB
/
bit-array.cpp
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
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define f(x, p, q) ( (x * p) + q )
typedef unsigned long long ULONG;
int main() {
int n, s, p, q;
cin >> n >> s >> p >> q;
ULONG p_231 = static_cast<ULONG>(pow(2, 31));
ULONG x0 = s % p_231;
ULONG vals = 1;
ULONG tort = f(x0, p, q) % p_231;
ULONG hare = f(x0, p, q) % p_231;
hare = f(hare, p, q) % p_231;
while (tort != hare) {
if (vals >= n) {
cout << vals << endl;
return 0;
} else {
vals++;
}
tort = f(tort, p, q) % p_231;
hare = f(hare, p, q) % p_231;
hare = f(hare, p, q) % p_231;
}
ULONG mu = 0;
tort = x0;
while (tort != hare) {
tort = f(tort, p, q) % p_231;
hare = f(hare, p, q) % p_231;
mu++;
}
ULONG lam = 1;
hare = f(tort, p, q) % p_231;
while (tort != hare) {
hare = f(hare, p, q) % p_231;
lam++;
}
cout << lam + mu << endl;
return 0;
}