forked from markhary/codility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChocolatesByNumbers.cpp
63 lines (53 loc) · 1.78 KB
/
ChocolatesByNumbers.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
// https://app.codility.com/programmers/lessons/12-euclidean_algorithm/chocolates_by_numbers/
//
// Task Score: 100%
// Correctness: 100%
// Performance: 100%
// Detected time complexity: Complexity
//
// Defined in Codility Chapter 12 - Euclidean Algorithm (12.2)
long long int greatestCommonDivisor(long long int a, long long int b) {
if ((a % b) == 0) {
return b;
}
return greatestCommonDivisor(b, a%b);
}
// Defined in Codility Chapter 12 - Euclidean Algorithm (12.4)
long long int leastCommonMultiple(long long int a, long long int b) {
return (a*b)/greatestCommonDivisor(a,b);
}
int solution(int N, int M) {
// Algorithm
// Find Least Common Multiple of {N, M}
// Answer is LCM/M
long long int lcm = leastCommonMultiple(N, M);
return lcm/M;
}
#include <iostream>
using namespace std;
/*
https://codility.com/demo/results/trainingJZWCZJ-WXY/
Observations:
Description has given sufficient info/hint. It even tells you "(X + M) modulo N (remainder of division)" to
remind you that M*C will eventually be greater than N. It's not used for a O(log(N+M)) solution though...
In order to achieve O(log(N+M)), we have to further examine the relationship among M, N and the result. It's
easy to realize that we will meet at an empty wrapper if and only if the index of this position is LCM(M,N).
Least Common Multiple. Then the number of chocolates we ate is lcm/M.
lcm = M * N / gcd(M, N)
chocolates = lcm / M
So put them together, we have:
chocolates = N / gcd(M, N)
You definitely want to use the expression above directly. A two step approach could introduce a potential
integer overflow.
*/
int gcd(int a, int b)
{
if (0 == a%b)
return b;
return gcd(b, a%b);
}
int solutionChocolatesByNumbers(int N, int M)
{
//M/lcm --> n/gcd
return N / gcd(N, M);
}