题目链接:http://acm.tju.edu.cn/toj/showp3469.html
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 239 Accepted Runs: 173
There are two piles of apples, one of which contains N apples and another contains M apples. Now we want to put these apples into bags. For some purpose, each of these bags must contains the same number of apples. When we put the first pile of apples into these bags, we get some bags of apples and b1 apples remains, and when we put the second pile of apples into these bags, we get some bags of apples and b2 apples remains. Now the question is: What is the maximum possible number of apples that a bag could contain?
Input
The first line of input is a single integer T, representing the number of test cases. Then T lines follows. Each line gives the number: N, M, b1, b2 in order. All numbers in the input are 32-bit signed integers and 0 ≤ b1 < N; 0 ≤ b2 < M; T ≤ 100.
Output
For each test cases, output a sigle line, contain the answer.
Sample Input
2 27 47 0 2 14 17 3 6
Sample Output
9 11
Source: TJU
#include <stdio.h>
#define swap(a,b) {a=a+b;b=a-b;a=a-b;}
int gcd(int a,int b){
if(b==0)
return a;
return gcd(b,a%b);
}
int main()
{
int n,m,b1,b2,cast;
scanf("%d",&cast);
while(cast--){
scanf("%d%d%d%d",&n,&m,&b1,&b2);
n-=b1;m-=b2;
if(n<m)
swap(n,m);
printf("%d\n",gcd(n,m));
}
}