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, Bx and 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
题目大意:给出两个点,然后问我们这两个点确定的线段中的x坐标和y坐标都是整数的点有多少个。
解题思路:本来一个很简单的GCD,我读完题之后理解成求两个点确定的线段中的x坐标和y坐标都相等并且是整数的点,然后再加上题目上说的A,B两个点也是晶格点,然后我强行将样例构造出来,一直WA了两个小时。最后没办法,用了半个小时写E题,才过了一题,不然就真的爆零了,哎,还是好好反思一下自己叭。
代码:
#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <queue>
//#include <random>
#include <time.h>
using namespace std;
#define int long long
#define ls root<<1
#define rs root<<1|1
//std::mt19937 rnd(time(NULL));
signed main()
{
int t;
cin>>t;
for (int test = 1; test <= t;test++){
int x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
printf("Case %lld: ", test);
cout << __gcd(abs(x1 - x2), abs(y1 - y2)) + 1 << endl;
}
}