forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10006.cpp
107 lines (97 loc) · 2.06 KB
/
10006.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <bits/stdc++.h>
using namespace std;
#define MAXPRIME 65001
// mod exp, i.e. b^e mod m.
unsigned long long fast_mod_pow(unsigned long long b, unsigned long long e, unsigned long long m)
{
unsigned long long r = 1;
while (e > 0)
{
if ((e & 1) == 1)
{
r = (r * b) % m;
}
e >>= 1;
b = (b * b) % m;
}
return r;
}
// gen all prime ≤MAXPRIME, via sieve
void gen_primes(bool p[])
{
p[0] = p[1] = false;
// starting at number 2 and going to the upper limit, mark
// all numbers as potential primes
for (int i = 2; i < MAXPRIME; i++)
{
p[i] = true;
}
int m = floor(sqrt(MAXPRIME));
int n;
// mark all multiples of a prime as non-primes. this has to be done for primes
// only up to the square root, since every number in the array has at least
// one factor smaller than the square root of the limit
for (int i = 2; i < m; i++)
{
if (p[i])
{
n = MAXPRIME / i;
for (int j = 2; j <= n; j++)
{
p[i * j] = false;
}
}
}
}
// gen all carmichael numbers ≤given limit by performing fermat tst
void gen_carmi(bool c[], bool p[])
{
// initialize carmichael numbers array with false
memset(c, 0, MAXPRIME * sizeof(bool));
// starting from the first non-prime, mark all
// odd numbers as potential carmichael numbers
for (int i = 9; i < MAXPRIME; i += 2)
{
c[i] = true;
}
// again, for all odd numbers, we exclude the primes and perform
// the fermat test for 2 <= a <= n-1.
for (int n = 9; n < MAXPRIME; n += 2)
{
// VERY IMPORTANT! check first if this number is prime, otherwise TLE
if (p[n])
{
c[n] = false;
continue;
}
for (int a = 2; a <= n - 1; a++)
{
if (fast_mod_pow(a, n, n) != a)
{
c[n] = false;
break;
}
}
}
}
int main()
{
unsigned long long n; // #
unsigned long long a; // a of fermat tst
bool prime[MAXPRIME]; // prime[]
bool carmi[MAXPRIME]; // carmichael[]
gen_primes(prime);
gen_carmi(carmi, prime);
while (cin >> n && (n != 0))
{
if (carmi[n])
{
printf("The number %llu is a Carmichael number.\n", n);
}
else
{
printf("%llu is normal.\n", n);
}
}
return 0;
}