-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathABCDEF.cs
174 lines (159 loc) · 6.72 KB
/
ABCDEF.cs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
// https://www.spoj.com/problems/ABCDEF/ #ad-hoc #combinatorics #hash-table #math
// Counts all sextuples satisfying the equation (a * b + c) / d - e = f.
public static class ABCDEF
{
// Naively we can generates all sextuples and test each. Generating them
// is simple because repetitions are allowed (and nums are already distinct).
public static long SolveSlowly(int[] nums)
{
long validSextupleCount = 0;
foreach (int a in nums)
{
foreach (int b in nums)
{
foreach (int c in nums)
{
foreach (int d in nums.Where(n => n != 0 && (a * b + c) % n == 0))
{
foreach (int e in nums)
{
foreach (int f in nums)
{
if ((a * b + c) / d - e == f)
{
++validSextupleCount;
}
}
}
}
}
}
}
return validSextupleCount;
}
// Equivalently we're trying to find: (a * b + c) / d == e + f. We can make use of
// multiplicative commutativity to search through all of a's nums but about half of b's.
// Only some d's need searched through; non-zero, and ones that evenly divide a * b + c,
// as to equal e + f the result must be an integer. And I guess the possible results of
// e + f can be stored in a dictionary with the # of ways to produce them, using commutativity
// here too to cut down the search space a little bit. So looping 4-ish deep with an O(1)
// lookup, instead of the naive approach which loops 6-ish deep.
public static long SolveFaster(int[] nums)
{
// n numbers, so at most (n choose 2) + n (for equal pairs) = n * (n - 1) / 2 + n different results.
var efResultCounts = new Dictionary<int, int>(
capacity: (nums.Length * (nums.Length - 1)) / 2 + nums.Length);
for (int ei = 0; ei < nums.Length; ++ei)
{
// Take advantage of commutativity here and in gainedResultCount.
for (int fi = ei; fi < nums.Length; ++fi)
{
int currentResultCount;
int gainedResultCount = (ei == fi ? 1 : 2);
int sum = nums[ei] + nums[fi];
if (efResultCounts.TryGetValue(sum, out currentResultCount))
{
efResultCounts[sum] = currentResultCount + gainedResultCount;
}
else
{
efResultCounts[sum] = gainedResultCount;
}
}
}
int[] dNums = nums.Where(n => n != 0).ToArray();
long validSextupleCount = 0;
for (int ai = 0; ai < nums.Length; ++ai)
{
// Take advantage of commutativity here and down below when incrementing the sextuple count.
for (int bi = ai; bi < nums.Length; ++bi)
{
foreach (int c in nums)
{
foreach (int d in dNums)
{
int abc = nums[ai] * nums[bi] + c;
if (abc % d != 0) continue;
int efResultCount;
if (efResultCounts.TryGetValue(abc / d, out efResultCount))
{
// For this unique combination of a, b, c, d, there are efResultCount ways
// to get an e and f such that (a * b + c) / d == e + f. And take into account
// commutativity on a and b.
validSextupleCount += ai == bi ? efResultCount : 2 * efResultCount;
}
}
}
}
}
return validSextupleCount;
}
// Similar ideas as above, except now trying (a * b + c) == d * (e + f) so we can turn
// the 2-ish deep preprocess and 4-ish deep process into 3-ish deep each.
public static long SolveEvenFaster(int[] nums)
{
int[] dNums = nums.Where(n => n != 0).ToArray();
// n numbers, so at most (n choose 2) + n (for equal pairs) = n * (n - 1) / 2 + n different sums
// for e + f, and then * n (maybe minus one if nums includes a zero) for the possible d factors.
var defResultCounts = new Dictionary<int, int>(
capacity: dNums.Length * ((nums.Length * (nums.Length - 1)) / 2 + nums.Length));
foreach (int d in dNums)
{
for (int ei = 0; ei < nums.Length; ++ei)
{
// Take advantage of commutativity here and in gainedResultCount.
for (int fi = ei; fi < nums.Length; ++fi)
{
int currentResultCount;
int gainedResultCount = (ei == fi ? 1 : 2);
int result = d * (nums[ei] + nums[fi]);
if (defResultCounts.TryGetValue(result, out currentResultCount))
{
defResultCounts[result] = currentResultCount + gainedResultCount;
}
else
{
defResultCounts[result] = gainedResultCount;
}
}
}
}
long validSextupleCount = 0;
for (int ai = 0; ai < nums.Length; ++ai)
{
// Take advantage of commutativity here and down below when incrementing the sextuple count.
for (int bi = ai; bi < nums.Length; ++bi)
{
foreach (int c in nums)
{
int defResultCount;
if (defResultCounts.TryGetValue(nums[ai] * nums[bi] + c, out defResultCount))
{
// For this unique combination of a, b, and c there are defResultCount ways
// to get an d, e, and f such that (a * b + c) == d * (e + f). And take into account
// commutativity on a and b.
validSextupleCount += ai == bi ? defResultCount : 2 * defResultCount;
}
}
}
}
return validSextupleCount;
}
}
public static class Program
{
private static void Main()
{
int[] nums = new int[int.Parse(Console.ReadLine())];
for (int i = 0; i < nums.Length; ++i)
{
nums[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine(
ABCDEF.SolveEvenFaster(nums));
}
}