1422. Table Tennis
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
?
There is a rectangular pool table ABCD with side lengths m and n, where m and n are integers with m
B---------C
| |
| |
A---------D
?
Input
Input consist of mutiple cases. Each case include two integers m and n, both of which fits in 32-bit signed integer.
Output
For each case, you should find out which pocket (A,B,C,D) the ball first hit and how many reflections the ball make .
Sample Input
6 10
Sample Output
C 6
?
假设球撞到边界时不会反弹,而是继续进入下一个与原图一样大小的边界,那么,由于出发的角度是45度,经过了m,n的公倍数的时候就会撞击一次洞,第一次撞击自然就是最小公倍数了(这样的话其实30度60度也是同样的思路)。见下图:
?
上图已经显示得很清晰了,这样,我们只需计算出最小公倍数,然后计算撞击横边和纵边的次数,最后再根据撞击次数确定洞就行:
?
#includeint gcd(int a, int b) {//求最大公约数不用递归会快 int c = a % b; while (c != 0) { a = b; b = c; c = a % b; } return b; } int main() { int n, m, lcm, hit_horizontal_size, hit_vertical_size; while (~scanf("%d%d", &m, &n)) { lcm = n * m / gcd(n, m); hit_horizontal_size = lcm / m - 1;//注意由于最后到洞的时候是不算撞边的,所以都要减去一 hit_vertical_size = lcm / n - 1; //1 & 1 = 1, 0 & 1 = 0,判断奇偶这样快 if (hit_horizontal_size & 1) {//如果撞击水平边的次数是奇数次,那么可能的点是AD if (hit_vertical_size & 1) {//如果撞击竖直边的次数是奇数次,那么可能的点是AB printf("A "); } else { printf("D "); } } else { if (hit_vertical_size & 1) { printf("B "); } else { printf("C "); } } printf("%d\n", hit_horizontal_size + hit_vertical_size); } return 0; }
?