-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP106.cpp
73 lines (64 loc) · 1.25 KB
/
P106.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
#include <iostream>
#include <stdio.h>
#define MAX 1000000
int gcd(int a, int b) {
int c;
while(a != 0) {
c = a;
a = b%a;
b = c;
}
return b;
}
/*
Given 0 < N <= 1.000.000
Output
A = number of relatvely prime tripples with components <= N.
B = number of integers not part of any tripple <= N
Any number in B: Can't be k times (m^2-n^2), 2nm, or m^2+n^2.
Eample: N = 10:
1 4 9 16 25 36 49 64 81 100
Relatively prime tripples: (3, 4, 5)
Other tripples: (6, 8, 10)
Numbers not in components: 1, 2, 7, 9
*/
int main() {
int N;
while(std::cin >> N) {
bool *notHit = new bool[N+1];
for(int i = 0; i < N+1; ++i)
notHit[i] = true;
int A = 0;
int B = N;
for(int m = 2; m*m < N; ++m) {
for(int n = (m%2 == 0) ? 1 : 2; n < m; n+=2) {
if(gcd(n, m) != 1)
continue;
if(n*n+m*m > N)
break;
// Primitime: (m^2-n^2), 2nm, or m^2+n^2.
int a = m*m-n*n;
int b = 2*n*m;
int c = m*m+n*n;
++A;
for(int k = 1; k*c <= N; ++k) {
if(notHit[a*k]) {
notHit[a*k] = false;
--B;
}
if(notHit[b*k]) {
notHit[b*k] = false;
--B;
}
if(notHit[c*k]) {
notHit[c*k] = false;
--B;
}
}
}
}
delete[] notHit;
std::cout << A << " " << B << std::endl;
}
return 0;
}