Sicily 1422. Table Tennis

1422. Table TennisConstraints

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<n. Bug put the cue ball at corner A and shoot it at a 45 angle to the sides, and it reflects perfectly (at 45 degres) off all sides, and never loses energy. The table has four pockets, one in each corner, and the ball will be sunk as soon as it hits a corner. The question is : given the integers m and n, how do you tell which pocket the ball hits first, and how many reflections will the ball make on the way?Assume A,B,C,D is the lower left corner,upper left corner, upper right corner, lower right corner respectively. The length of AB is m, and the length of AD is n.

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 Input6 10Sample OutputC 6假设球撞到边界时不会反弹,而是继续进入下一个与原图一样大小的边界,,那么,由于出发的角度是45度,经过了m,n的公倍数的时候就会撞击一次洞,第一次撞击自然就是最小公倍数了(这样的话其实30度60度也是同样的思路)。见下图:

上图已经显示得很清晰了,这样,我们只需计算出最小公倍数,然后计算撞击横边和纵边的次数,最后再根据撞击次数确定洞就行:

#include <stdio.h>int 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) {//如果撞击水平边的次数是奇数次,那么可能的点是ADif (hit_vertical_size & 1) {//如果撞击竖直边的次数是奇数次,那么可能的点是ABprintf("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;}

不论你在什么时候结束,重要的是结束之后就不要悔恨

Sicily 1422. Table Tennis

相关文章:

你感兴趣的文章:

标签云: