-
Notifications
You must be signed in to change notification settings - Fork 0
/
triangle_lattice.cpp
82 lines (72 loc) · 1.91 KB
/
triangle_lattice.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
// yup - implausible
// also run time executes within 10-15 seconds for largest matrix 200, 200
// though if they run several large numbers it will take a bit :-\
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
typedef unsigned long long ullong;
// thank you knuth
ullong choose(ullong n, ullong k) {
if (k > n) {
return 0;
}
ullong r = 1;
for (ullong d = 1; d <= k; ++d) {
r *= n--;
r /= d;
}
return r;
}
int vertsDiag(int m, int n, int x, int y, int run, int fall) {
int points = 0;
while (x < m && y < n) {
points++;
x += run;
y += fall;
}
return points;
}
int main(int argc, char **argv) {
string input;
if (argc < 2) {
input = "input.txt";
} else {
input = argv[1];
}
fstream fin, fout;
fin.open(input, ios::in);
fout.open("output.txt", ios::out);
if (!fin.is_open() || !fout.is_open()) {
cout << "There was an error opening a file for input or output, check your permissions." << endl;
cin.get();
return -1;
}
int m, n;
ullong combinations, rows, cols, diagonals, quarterCount;
while (fin >> m >> n) {
combinations = choose((++m)*(++n), 3);
rows = n*choose(m, 3);
cols = m*choose(n, 3);
diagonals = 0;
for (int x = 0; x < n - 2; ++x) {
for (int y = 0; y < m - 2; ++y) {
// get the center line
diagonals += 2 * choose( vertsDiag(m, n, x, y, 1, 1), 3);
//cout << "(" << x << ", " << y << ") [1,1]: " << vertsDiag(m, n, x, y, 1, 1) << endl;
quarterCount = 0;
for (int run = 1; run < n/2; ++run) {
for (int fall = (run + 1); fall < m/2; ++fall) {
//cout << "(" << x << ", " << y << ") [" << run << "," << fall << "]: " << vertsDiag(m, n, x, y, run, fall) << endl;
quarterCount += choose( vertsDiag(m, n, x, y, run, fall), 3);
}
}
diagonals += 4*quarterCount;
}
}
fout << combinations - rows - cols - diagonals << endl;
}
fin.close();
fout.close();
return 0;
}