Problem Description
YY 小时候性格孤僻,小朋友们都不喜欢跟他一起玩,于是他养成了一个奇怪的习惯:每天都在屋子里走来走去。有一天,他突然想到了一个问题,假设屋子是一个N x M 的矩形,里面铺着 1 x 1 的地板砖(即共有 N 行,每行 M 块地板砖),他想知道沿着对角线从左上角走到右下角会走过多少块地板砖( YY 可以看做一个质点)。样例中四组数据的对应图片如下图所示:
Input
输入数据的第一行为一个正整数 T(T ≤ 100),代表共有 T 组测试数据。
对于每组测试数据:
输入两个正整数 N 和 M (1 ≤ N, M ≤ 104)。
Output
对于每组测试数据,输出一个正整数 R,表示走过的地板砖数。
Sample Input
4
4 1
3 2
4 2
4 4
Sample Output
4
4
4
4
import java.util.Scanner;
public class Main {
public static int f(int n, int m, int x)
{
return n * x / m;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t, i, j, n, m, s;
t = in.nextInt();
for(i = 1; i <= t; i++)
{
n = in.nextInt();
m = in.nextInt();
s = 0;
for(j = 1; j <= m - 1; j++)
{
s += (int)f(n, m, j);
}
s = m * n - 2 * s;
System.out.println(s);
}
in.close();
}
}