-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVa11053.cpp
More file actions
59 lines (54 loc) · 1.06 KB
/
UVa11053.cpp
File metadata and controls
59 lines (54 loc) · 1.06 KB
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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// shortcuts for "common" data types in contests
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define INF 1000000000
ll N, a, b;
ll f(ll x)
{
return (((((a % N) * (x % N)) % N) * (x % N)) % N + (b % N)) % N;
}
pair<ll, ll> cycleFinding(ll x0)
{
ll tortoise = f(x0), hare = f(f(x0));
while (tortoise != hare)
{
tortoise = f(tortoise);
hare = f(f(hare));
}
ll mu = 0;
hare = x0;
while (tortoise != hare)
{
tortoise = f(tortoise);
hare = f(hare);
++mu;
}
ll lambda = 1;
hare = f(tortoise);
while (tortoise != hare)
{
hare = f(hare);
++lambda;
}
return pair<ll, ll>(mu, lambda);
}
int main()
{
scanf("%lld %lld %lld\n", &N, &a, &b);
while (N != 0ll)
{
pair<ll, ll> result = cycleFinding(0);
// printf("mu:%lld lambda:%lld ans?:%lld\n", result.first, result.second, N - result.second);
printf("%lld\n", N - result.second);
scanf("%lld %lld %lld\n", &N, &a, &b);
}
return 0;
}