Skip to main content

858. Mirror Reflection

https://leetcode.com/problems/mirror-reflection/

Python

  • pq的最小公倍數會打到頂點
  • 命中(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