How Many Points? LightOJ - 1077

Given two points A and B on the X-Y plane, output the number of the lattice points on the segment AB. Note that A and B are also lattice point. Those who are confused with the definition of lattice point, lattice points are those points which have both x and y co-ordinate as integer.

For example, for A (3, 3) and B (-1, -1) the output is 5. The points are: (-1, -1), (0, 0), (1, 1), (2, 2) and (3, 3).


Input

Input starts with an integer T (≤ 125), denoting the number of test cases.

Each case contains four integers, Ax, Ay, Bxand By. Each of them will be fit into a 32 bit signed integer.

Output

For each test case, print the case number and the number of lattice points between AB.

Sample Input

2

3 3 -1 -1

0 0 5 2

Sample Output

Case 1: 5

Case 2: 2

#include <cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;

/*
要不是这道题挂在这里,我都不敢想gcd,感觉有点蒙
不过想一想确实 用在这里是蛮好的,gcd就是可以最多
分成几块,感觉很神奇
*/

ll gcd(ll a,ll b){
    return b==0?a:gcd(b,a%b);
}

int main()
{
    int T,kase=1;
    scanf("%d",&T);
    while(T--){
        ll ax,ay,bx,by;
        scanf("%lld %lld %lld %lld",&ax,&ay,&bx,&by);
        ll d1=abs(bx-ax),d2=abs(by-ay);
        if(d1>d2)swap(d1,d2);
        printf("Case %d: ",kase++);
        ll g=gcd(d1,d2);
        printf("%lld\n",g+1);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值