forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
56 lines (42 loc) · 1.05 KB
/
main2.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
/// Source : https://leetcode.com/problems/mirror-reflection/description/
/// Author : liuyubobobo
/// Time : 2018-06-23
#include <iostream>
#include <cassert>
using namespace std;
/// Mathematics
/// Time Complexity: O(log(max(p, q)))
/// Space Complexity: O(1)
class Solution {
public:
int mirrorReflection(int p, int q) {
int g = gcd(p, q);
int k = p / g;
int x = p * k;
int y = q * k;
assert(y % p == 0);
if(isEven(y / p))
return 0;
if(isEven(x / p))
return 2;
return 1;
}
private:
bool isEven(int x){
return x % 2 == 0;
}
int gcd(int a, int b){
if(a > b)
swap(a, b);
if(b % a == 0)
return a;
return gcd(b % a, a);
}
};
int main() {
cout << Solution().mirrorReflection(2, 1) << endl; // 2
cout << Solution().mirrorReflection(4, 3) << endl; // 2
cout << Solution().mirrorReflection(3, 2) << endl; // 0
cout << Solution().mirrorReflection(3, 1) << endl; // 1
return 0;
}