โจทย์
มีลูกบาศก์ a×b×c สร้าง Box ขนาด a×b×c (a,b,c คู่ต่อคู่ Coprime) ตัด Box ด้วย Plane ผ่านจุด P,Q,R หาจำนวนลูกบาศก์ที่ถูกตัด
ตัวอย่าง: [[1,2,3], [4,2,5], [6,8,2]] → Output: [5, 18, 37]
สูตร
จำนวนลูกบาศก์ที่ถูกตัด = floor((ab + ac + bc - 1) / 2) mod 1e9+7
โค้ด Python
from math import ceil
def solve(n, input_array):
output = []
for i in range(n):
a, b, c = input_array[i][0], input_array[i][1], input_array[i][2]
val = ((a * b + a * c + b * c - 1) // 2) % 1000000007
output.append(val)
return output
print(solve(3, [[1, 2, 3], [4, 2, 5], [6, 8, 2]])) # Output: [5, 18, 37]
Input / Output
Input: 3, [[1, 2, 3], [4, 2, 5], [6, 8, 2]]
Output: [5, 18, 37]