-
Notifications
You must be signed in to change notification settings - Fork 4
/
CountingSeries.cpp
338 lines (306 loc) · 7.28 KB
/
CountingSeries.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <climits>
#include <cstring>
using namespace std;
typedef long long int64;
typedef vector<int> vi;
typedef string ST;
typedef stringstream SS;
typedef vector< vector <int> > vvi;
typedef pair<int,int> ii;
typedef vector <string> vs;
#define Pf printf
#define Sf scanf
#define ep 1e-9
#define PI M_PI
#define E M_E
#define CL(a, b) memset(a, b, sizeof(a))
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(i, c) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(x, c) ((c).find(x) != (c).end()) //map & set//
#define cpresent(x, c) (find(all(c),x) != (c).end()) //vector & list//
#define forn(i, n) for(int i = 0, loop_ends_here = (int)n; i < loop_ends_here; i++)
#define forab(i, a, b) for(int i = a, loop_ends_here = (int) b; i <= loop_ends_here; i++)
#define rep(i, a, b) for(int i = a, loop_ends_here = (int)b; i>=loop_ends_here; i--)
class CountingSeries
{
public:
long long countThem(long long a, long long b, long long c, long long d, long long upper)
{
vector <int64> gp; gp.clear();
if(d == 1LL) {
if(c*d <= upper)
gp.pb(c*d);
}
else {
for(int64 t = c; t<= upper; t*=d)
gp.pb(t);
}
vector <int64> agp; agp.clear();
tr(it, gp) {
int64 t = *it;
if( (t-a)%b == 0LL && (t-a)/b >= 0) agp.pb(t);
}
int64 count;
if(a > upper) count = 0;
else count = (upper - a)/b + 1;
cout << "GP : ";
tr(it, gp) cout << *it << " ";
cout << endl;
cout << "AGP: ";
tr(it, agp) cout << *it << " ";
cout << endl;
cout << "AP : ";
Pf("x = %lld, a+b*x = %lld\n", count-1, a+b*(count-1));
return count + gp.size() - agp.size();
}
};
// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit 2.1.8 (beta) modified by pivanof
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool KawigiEdit_RunTest(int testNum, long long p0, long long p1, long long p2, long long p3, long long p4, bool hasAnswer, long long p5) {
cout << "Test " << testNum << ": [" << p0 << "," << p1 << "," << p2 << "," << p3 << "," << p4;
cout << "]" << endl;
CountingSeries *obj;
long long answer;
obj = new CountingSeries();
clock_t startTime = clock();
answer = obj->countThem(p0, p1, p2, p3, p4);
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" << p5 << endl;
}
cout << "Your answer:" << endl;
cout << "\t" << answer << endl;
if (hasAnswer) {
res = answer == p5;
}
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;
all_right = true;
long long p0;
long long p1;
long long p2;
long long p3;
long long p4;
long long p5;
{
// ----- test 0 -----
p0 = 1ll;
p1 = 1ll;
p2 = 1ll;
p3 = 2ll;
p4 = 1000ll;
p5 = 1000ll;
all_right = KawigiEdit_RunTest(0, p0, p1, p2, p3, p4, true, p5) && all_right;
// ------------------
}
{
// ----- test 1 -----
p0 = 3ll;
p1 = 3ll;
p2 = 1ll;
p3 = 2ll;
p4 = 1000ll;
p5 = 343ll;
all_right = KawigiEdit_RunTest(1, p0, p1, p2, p3, p4, true, p5) && all_right;
// ------------------
}
{
// ----- test 2 -----
p0 = 40ll;
p1 = 77ll;
p2 = 40ll;
p3 = 100000ll;
p4 = 40ll;
p5 = 1ll;
all_right = KawigiEdit_RunTest(2, p0, p1, p2, p3, p4, true, p5) && all_right;
// ------------------
}
{
// ----- test 3 -----
p0 = 452ll;
p1 = 24ll;
p2 = 4ll;
p3 = 5ll;
p4 = 600ll;
p5 = 10ll;
all_right = KawigiEdit_RunTest(3, p0, p1, p2, p3, p4, true, p5) && all_right;
// ------------------
}
{
// ----- test 4 -----
p0 = 234ll;
p1 = 24ll;
p2 = 377ll;
p3 = 1ll;
p4 = 10000ll;
p5 = 408ll;
all_right = KawigiEdit_RunTest(4, p0, p1, p2, p3, p4, true, p5) && all_right;
// ------------------
}
{
// ----- test 5 -----
p0 = 1000000000000ll;
p1 = 1000000000000ll;
p2 = 1000000000000ll;
p3 = 100000ll;
p4 = 1000000000000ll;
p5 = 1ll;
all_right = KawigiEdit_RunTest(5, p0, p1, p2, p3, p4, true, p5) && all_right;
// ------------------
}
{
// ----- test 6 -----
p0 = 371658585308ll;
p1 = 274808924218ll;
p2 = 611329026706ll;
p3 = 75085ll;
p4 = 848787675931ll;
p5 = 3ll;
all_right = KawigiEdit_RunTest(6, p0, p1, p2, p3, p4, true, p5) && all_right;
// ------------------
}
{
// ----- test 7 -----
p0 = 17340ll;
p1 = 40451ll;
p2 = 7763ll;
p3 = 1ll;
p4 = 3462ll;
p5 = 0ll;
all_right = KawigiEdit_RunTest(7, p0, p1, p2, p3, p4, true, p5) && all_right;
// ------------------
}
if (all_right) {
cout << "You're a stud (at least on the example cases)!" << endl;
} else {
cout << "Some of the test cases had errors." << endl;
}
return 0;
}
// PROBLEM STATEMENT
// You are given long longs a, b, c and d. The numbers a and b define an arithmetic progression that consists of all numbers of the form a + b*x, where x is a nonnegative integer. Likewise, c and d define a geometric progression that consists of all the numbers that are equal to c * d^y, where y is a nonnegative integer. You are also given a long long upperBound. Return the total number of integers between 1 and upperBound, inclusive, that belong to the arithmetic progression, the geometric progression or both.
//
// DEFINITION
// Class:CountingSeries
// Method:countThem
// Parameters:long long, long long, long long, long long, long long
// Returns:long long
// Method signature:long long countThem(long long a, long long b, long long c, long long d, long long upperBound)
//
//
// NOTES
// -The ^ operator in this statement denotes the exponentiation operation. For example, 3^0 = 1 and 2^4 = 2*2*2*2 = 16.
//
//
// CONSTRAINTS
// -a, b, c and upperBound will each be between 1 and 1000000000000 (10^12), inclusive.
// -d will be between 1 and 100000 (10^5), inclusive.
//
//
// EXAMPLES
//
// 0)
// 1
// 1
// 1
// 2
// 1000
//
// Returns: 1000
//
// The arithmetic progression is: 1, 2, 3, 4, ... .
// The geometric progression is: 1, 2, 4, 8, 16, ... .
// Each positive integer is contained in at least one of the progressions.
//
// 1)
// 3
// 3
// 1
// 2
// 1000
//
// Returns: 343
//
// This time, the arithmetic progression is: 3, 6, 9, 12, ... .
// The geometric progression is still: 1, 2, 4, 8, 16, ....
// There are 333 multiples of 3 between 1 and 1000, inclusive, and there are 10 powers of 2, 512 being the highest. As these two progressions do not have any common elements, the total result is 343.
//
// 2)
// 40
// 77
// 40
// 100000
// 40
//
// Returns: 1
//
//
//
// 3)
// 452
// 24
// 4
// 5
// 600
//
// Returns: 10
//
// The 10 numbers are: 4, 20, 100, 452, 476, 500, 524, 548, 572 and 596.
//
// 4)
// 234
// 24
// 377
// 1
// 10000
//
// Returns: 408
//
//
//
// END KAWIGIEDIT TESTING
//Powered by KawigiEdit 2.1.8 (beta) modified by pivanof!