In the internal competition, this question is sdut_2369. Map writes TLE. In other words, the OJ team members were sentenced to AC and me to TLE. Finally, the Captain concluded that this is a matter of character... Khan = _ = ///
After the game, I wrote it with hash. The key is to handle conflicts and write it with the chain method. Finally, let's take a look at how it looks like an adjacent table. Maybe you can change this question to a good topic.
Poj 1235 + MS, sdutoj 578 + MS. Our server is still very powerful.
Slag code:
View code
1 #include <vector>
2 #include <list>
3 #include <map>
4 #include <set>
5 #include <queue>
6 #include <deque>
7 #include <stack>
8 #include <bitset>
9 #include <algorithm>
10 #include <functional>
11 #include <numeric>
12 #include <utility>
13 #include <sstream>
14 #include <iostream>
15 #include <iomanip>
16 #include <cstdio>
17 #include <cmath>
18 #include <cstdlib>
19 #include <ctime>
20 #include <cstring>
21
22 using namespace std;
23
24 const int MOD = 1000007;
25 typedef long long ll;
26 struct node {
27 long long n;
28 int cnt;
29 int next;
30 } mp[MOD];
31
32 int head[MOD], t;
33
34 void init() {
35 for(int i = 0; i < MOD; ++i) head[i] = 0;
36 t = 1;
37 }
38
39 void add(int u, long long v) {
40 mp[t].n = v; mp[t].cnt = 1;
41 mp[t].next = head[u]; head[u] = t++;
42 }
43
44 int find(int t, long long x) {
45 int i;
46 for(i = head[t]; i; i = mp[i].next) {
47 if(mp[i].n == x) {
48 mp[i].cnt++;
49 return mp[i].cnt;
50 }
51 }
52 return 0;
53 }
54
55 int main() {
56 //freopen("data.in", "r", stdin);
57
58 ll x;
59 int n, a, b, u;
60 int cnt;
61 while(scanf("%d", &n), n) {
62 scanf("%d%d", &a, &b);
63 init();
64 x = b%n; cnt = 0;
65
66 while(1) {
67 u = find(x%MOD, x);
68 if(u == 2) cnt++;
69 if(u == 3) break;
70 if(!u) {
71 add(x%MOD, x);
72 }
73 x = (((a*x)%n*x) + b)%n;
74 }
75 printf("%d\n", n - cnt);
76 }
77 return 0;
78 }