forked from aseembrahma/topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheKingsFactorization.cpp
137 lines (122 loc) · 3.6 KB
/
TheKingsFactorization.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
#include <iostream>
#include <iterator>
#include <algorithm>
#include <map>
#include <vector>
#include <iomanip>
#include <sstream>
#include <queue>
#include <string>
#include <cmath>
#include <set>
#include <limits>
#include <numeric>
#include <bitset>
#define DEBUG 1
#define ARR_SIZE(x) ((sizeof(x)) / (sizeof(x[0])))
using namespace std;
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
template <typename K, typename V>
V GetWithDef(const std::map <K,V> & m, const K & key, const V & defval ) {
typename std::map<K,V>::const_iterator it = m.find( key );
if ( it == m.end() ) {
return defval;
}
else {
return it->second;
}
}
template<typename T>
void print_vector(const vector<T> &v) {
cerr << " { ";
for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
cerr << *it << ", ";
}
cerr << " } " << endl;
}
class TheKingsFactorization {
public:
vector<long long> getVector(long long N, vector<long long> primes) {
vector<long long> output;
long long N_orig = N;
for (size_t i = 0; i < primes.size(); ++i) {
while (N % primes[i] == 0) {
output.push_back(primes[i]);
N /= primes[i];
}
}
if (1 != N) {
primes.push_back(N_orig / primes[0]);
for (size_t i = 0; i < primes.size() - 1; ++i) {
for (long long j = primes[i] + 1; j < primes[i + 1]; ++j) {
while (N % j == 0) {
output.push_back(j);
N /= j;
}
}
}
}
sort(output.begin(), output.end());
return output;
}
bool runTest(long long number,
long long hints[], size_t hints_size,
long long solution[], size_t solution_size) {
vector<long long> hints_vec(hints, hints + hints_size);
vector<long long> solution_vec(solution, solution + solution_size);
vector<long long> output = getVector(number, hints_vec);
if (output.size() != solution_vec.size() ||
!equal(output.begin(), output.end(), solution_vec.begin()))
{
cerr << "Failed for " << number << ". Expected:" << endl;
print_vector(solution_vec);
cerr << "Found instead:" << endl;
print_vector(output);
}
}
};
int main(int argc, char* argv[]) {
TheKingsFactorization test;
{
long long hints[] = {2, 3};
long long solution[] = {2, 2, 3};
test.runTest(12, hints, ARR_SIZE(hints), solution, ARR_SIZE(solution));
}
{
long long hints[] = {7};
long long solution[] = {7};
test.runTest(7, hints, ARR_SIZE(hints), solution, ARR_SIZE(solution));
}
{
long long hints[] = {2, 3, 7};
long long solution[] = {2, 2, 3, 3, 7, 7};
test.runTest(1764, hints, ARR_SIZE(hints), solution, ARR_SIZE(solution));
}
{
long long hints[] = {7};
long long solution[] = {7, 7};
test.runTest(49, hints, ARR_SIZE(hints), solution, ARR_SIZE(solution));
}
{
long long hints[] = {2, 5};
long long solution[] = {2, 3, 5, 7};
test.runTest(210, hints, ARR_SIZE(hints), solution, ARR_SIZE(solution));
}
{
long long hints[] = {2, 2, 2, 5, 5};
long long solution[] = {2, 2, 2, 2, 2, 5, 5, 5, 5, 5};
test.runTest(100000, hints, ARR_SIZE(hints), solution, ARR_SIZE(solution));
}
}