-
Notifications
You must be signed in to change notification settings - Fork 4
/
BigFatInteger.cpp
240 lines (226 loc) · 5.46 KB
/
BigFatInteger.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <bits/stdc++.h>
using namespace std;
class BigFatInteger
{
public:
int minOperations(int A, int B);
};
int div1(int &A, int d) {
int ret = 0;
while(A % d == 0) {
ret++;
A/=d;
}
return ret;
}
int BigFatInteger::minOperations (int A, int B) {
int ret = 0;
//int mn = 1e8;
int mx = 0;
//vector <int> mul;
for(int i = 2; i <= A; i++) if(A % i == 0) {
int cntTmp = div1(A, i);
int cnt = cntTmp * B;
ret++;
int tmp = 0;
while(cnt > 1) {
int p;
for(p = 0; (1<<p) < cnt && (1<<p) <= cnt; p++);
p--;
cnt -= (1<<p);
tmp++;
}
mx = max(mx, tmp);
}
return ret + mx;
}
// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit-pf 2.3.0
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cmath>
using namespace std;
bool KawigiEdit_RunTest(int testNum, int p0, int p1, bool hasAnswer, int p2) {
cout << "Test " << testNum << ": [" << p0 << "," << p1;
cout << "]" << endl;
BigFatInteger *obj;
int answer;
obj = new BigFatInteger();
clock_t startTime = clock();
answer = obj->minOperations(p0, p1);
clock_t endTime = clock();
delete obj;
bool res;
res = true;
cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
if (hasAnswer) {
cout << "Desired answer:" << endl;
cout << "\t" << p2 << endl;
}
cout << "Your answer:" << endl;
cout << "\t" << answer << endl;
if (hasAnswer) {
res = answer == p2;
}
if (!res) {
cout << "DOESN'T MATCH!!!!" << endl;
} else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
cout << "FAIL the timeout" << endl;
res = false;
} else if (hasAnswer) {
cout << "Match :-)" << endl;
} else {
cout << "OK, but is it right?" << endl;
}
cout << "" << endl;
return res;
}
int main() {
bool all_right;
bool disabled;
bool tests_disabled;
all_right = true;
tests_disabled = false;
int p0;
int p1;
int p2;
// ----- test 0 -----
disabled = false;
p0 = 6;
p1 = 1;
p2 = 2;
all_right = (disabled || KawigiEdit_RunTest(0, p0, p1, true, p2) ) && all_right;
tests_disabled = tests_disabled || disabled;
// ------------------
// ----- test 1 -----
disabled = false;
p0 = 162;
p1 = 1;
p2 = 4;
all_right = (disabled || KawigiEdit_RunTest(1, p0, p1, true, p2) ) && all_right;
tests_disabled = tests_disabled || disabled;
// ------------------
// ----- test 2 -----
disabled = false;
p0 = 999983;
p1 = 9;
p2 = 5;
all_right = (disabled || KawigiEdit_RunTest(2, p0, p1, true, p2) ) && all_right;
tests_disabled = tests_disabled || disabled;
// ------------------
// ----- test 3 -----
disabled = false;
p0 = 360;
p1 = 8;
p2 = 8;
all_right = (disabled || KawigiEdit_RunTest(3, p0, p1, true, p2) ) && all_right;
tests_disabled = tests_disabled || disabled;
// ------------------
// ----- test 4 -----
disabled = false;
p0 = 2;
p1 = 27;
all_right = (disabled || KawigiEdit_RunTest(4, p0, p1, false, p2) ) && all_right;
tests_disabled = tests_disabled || disabled;
// ------------------
// ----- test 5 -----
disabled = false;
p0 = 2;
p1 = 32;
all_right = (disabled || KawigiEdit_RunTest(5, p0, p1, false, p2) ) && all_right;
tests_disabled = tests_disabled || disabled;
// ------------------
if (all_right) {
if (tests_disabled) {
cout << "You're a stud (but some test cases were disabled)!" << endl;
} else {
cout << "You're a stud (at least on given cases)!" << endl;
}
} else {
cout << "Some of the test cases had errors." << endl;
}
return 0;
}
// PROBLEM STATEMENT
// This problem statement contains superscipts that may not display properly outside the applet.
//
// Lun the dog loves very large integers. Her favorite is AB (A to the power of B).
//
//
// She has an integer variable X. Initially, the value of X is set to 1. She can perform the following two kinds of operations in any order, any number of times.
//
// Operation 1: choose a prime number p, then multiply X by p.
// Operation 2: choose a positive divisor d of the value of X at that point, then multiply X by d.
//
//
//
// You are given two ints A and B. Return the minimum number of operations Lun needs to perform
// in order to obtain X = AB from the initial state X = 1.
//
// DEFINITION
// Class:BigFatInteger
// Method:minOperations
// Parameters:int, int
// Returns:int
// Method signature:int minOperations(int A, int B)
//
//
// CONSTRAINTS
// -A will be between 2 and 1,000,000 (106), inclusive.
// -B will be between 1 and 1,000,000 (106), inclusive.
//
//
// EXAMPLES
//
// 0)
// 6
// 1
//
// Returns: 2
//
// Here, AB = 61 = 6. Here is one of the optimal sequences of operations:
//
// Perform operation 1 by choosing p=2. X is now 1*2 = 2.
// Perform operation 1 by choosing p=3. X is now 2*3 = 6.
//
//
// 1)
// 162
// 1
//
// Returns: 4
//
// One of the optimal sequences of operations:
//
// Perform operation 1 by choosing p=3. X is now 1*3 = 3.
// Perform operation 1 by choosing p=3. X is now 3*3 = 9.
// Perform operation 2 by choosing d=9. X is now 9*9 = 81.
// Perform operation 1 by choosing p=2. X is now 81*2 = 162.
//
//
// 2)
// 999983
// 9
//
// Returns: 5
//
// Here, A is prime. One of the optimal sequences of operations:
//
// Perform operation 1 by choosing p=A. X is now A.
// Perform operation 1 by choosing p=A. X is now A2.
// Perform operation 1 by choosing p=A. X is now A3.
// Perform operation 2 by choosing d=A3. X is now A6.
// Perform operation 2 by choosing d=A3. X is now A9.
//
//
// 3)
// 360
// 8
//
// Returns: 8
//
//
//
// END KAWIGIEDIT TESTING