858. Mirror Reflection
https://leetcode.com/problems/mirror-reflection/
Python
p
和q
的最小公倍數會打到頂點- 命中(2)或(1,0)以最小公倍數除以
q
是偶數或奇數決定 - 命中1或0以最小公倍數除以
p
決定
import math
class Solution:
def mirrorReflection(self, p: int, q: int) -> int:
lcm = math.lcm(p, q)
m, n = lcm/p, lcm/q
if m % 2 == 0 and n % 2 == 1: return 0
if m % 2 == 1 and n % 2 == 1: return 1
if m % 2 == 1 and n % 2 == 0: return 2
return -1