-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem5.cpp
176 lines (149 loc) · 3.7 KB
/
problem5.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
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
175
176
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
void generateMultiplicativePartitions(int n, vector<int> &factors, vector<vector<int>> &partitions)
{
if (n == 1)
{
partitions.push_back(factors);
return;
}
int start = factors.empty() ? 2 : factors.back();
for (int i = start; i <= n; i++)
{
if (n % i == 0)
{
factors.push_back(i);
generateMultiplicativePartitions(n / i, factors, partitions);
factors.pop_back();
}
}
}
int isPrime(int n)
{
if (n <= 1 || n > 3 && (n % 2 == 0 || n % 3 == 0))
return 0;
for (int i = 5, t = 2; i * i <= n; i += t, t = 6 - t)
if (n % i == 0)
return 0;
return 1;
}
int nthPrime(int n)
{
int res;
if (n >= 1)
res = 2;
if (n >= 2)
res = 3;
if (n >= 3)
{
int count = 2;
for (int i = 5, t = 2; count < n; i += t, t = 6 - t)
{
if (isPrime(i))
{
count++;
res = i;
}
}
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, num1, num2;
cin >> n;
num1 = 2 * n;
num2 = 2 * n - 1;
vector<vector<int>> partitionsOfNum1, partitionsOfNum2;
vector<int> factorsOfNum1, factorsOfNum2;
generateMultiplicativePartitions(num1, factorsOfNum1, partitionsOfNum1);
generateMultiplicativePartitions(num2, factorsOfNum2, partitionsOfNum2);
// Back Sort all the vectors in partitionsOfNum1
for (auto &p : partitionsOfNum1)
{
sort(p.rbegin(), p.rend());
}
// Back Sort all the vectors in partitionsOfNum2
for (auto &p : partitionsOfNum2)
{
sort(p.rbegin(), p.rend());
}
// Sort the vecotrs in partitionsOfNum1 by lexicographical order
sort(partitionsOfNum1.begin(), partitionsOfNum1.end());
// Sort the vecotrs in partitionsOfNum2 by lexicographical order
sort(partitionsOfNum2.begin(), partitionsOfNum2.end());
// subtract 1 from each element in the partitionsOfNum1 vector
for (auto &p : partitionsOfNum1)
{
for (int i = 0; i < p.size(); i++)
{
p[i] -= 1;
}
}
// subtract 1 from each element in the partitionsOfNum2 vector
for (auto &p : partitionsOfNum2)
{
for (int i = 0; i < p.size(); i++)
{
p[i] -= 1;
}
}
unsigned long long result = 1;
for (int i = 0; i < partitionsOfNum1[0].size(); i++)
{
result *= pow(nthPrime(i + 1), partitionsOfNum1[0][i]);
}
for (auto p : partitionsOfNum1)
{
unsigned long long product = 1;
for (int i = 0; i < p.size(); i++)
{
for (int j = 0; j < p[i]; j++)
{
product *= nthPrime(i + 1);
if (product >= result)
{
break;
}
}
if (product > result)
{
break;
}
}
if (product < result)
{
result = product;
}
}
for (auto p : partitionsOfNum2)
{
unsigned long long product = 1;
for (int i = 0; i < p.size(); i++)
{
for (int j = 0; j < p[i]; j++)
{
product *= nthPrime(i + 1);
if (product > result)
{
break;
}
}
if (product > result)
{
break;
}
}
if (product < result)
{
result = product;
}
}
cout << result << endl;
return 0;
}