-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem155.cs
82 lines (65 loc) · 2.09 KB
/
problem155.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
using System;
using System.Collections.Generic;
public class problem155 {
public class Fraction {
public long numerator { get; }
public long denominator { get; }
public Fraction(long numerator, long denominator) {
long d = Gcd(numerator, denominator);
this.numerator = numerator / d;
this.denominator = denominator / d;
}
public override bool Equals(object that) =>
(that is Fraction) &&
Equals(that as Fraction);
public bool Equals(Fraction that) =>
this.numerator == that.numerator &&
this.denominator == that.denominator;
public override int GetHashCode() =>
HashCode.Combine(numerator, denominator);
public Fraction Reciprocal() =>
new Fraction(denominator, numerator);
public static Fraction operator +(Fraction a, Fraction b) =>
new Fraction(a.numerator * b.denominator + b.numerator * a.denominator, a.denominator * b.denominator);
public Fraction ParallelAdd(Fraction that) =>
(this.Reciprocal() + that.Reciprocal()).Reciprocal();
}
public static long Gcd(long a, long b) {
while (b != 0) {
long tmp = a;
a = b;
b = tmp % a;
}
return a;
}
private static HashSet<Fraction> BaseCase() {
var result = new HashSet<Fraction>();
result.Add(new Fraction(1, 1));
return result;
}
public static long UpTo(int n) {
var distinct = new List<HashSet<Fraction>>();
distinct.Add(BaseCase());
for (var x = 2; x <= n; x++) {
var newDistinct = new List<Fraction>();
for (var n1 = 1; n1 <= x / 2; n1++) {
foreach (var c1 in distinct[n1 - 1]) {
var n2 = x - n1;
foreach (var c2 in distinct[n2 - 1]) {
newDistinct.Add(c1 + c2);
newDistinct.Add(c1.ParallelAdd(c2));
}
}
}
distinct.Add(new HashSet<Fraction>(newDistinct));
}
var finalSet = new HashSet<Fraction>();
foreach (var constituent in distinct) {
finalSet.UnionWith(constituent);
}
return finalSet.Count;
}
public static void Main(string[] args) {
Console.WriteLine(UpTo(18));
}
}