How Many Points? LightOJ - 1077(线段经过整点个数与gcd 证明)

题意:

   已知两点 (x1,y1) 和 (x2, y2)求两点间线段上的整点的个数

解析:

  就是求gcd(abs(x2- x1),abs(y2 - y1))

证明:

  我们分水平方向和竖直方向两个方向看   这些在线段上的整点的横纵坐标一定可以平分 x2-x1  和  y2-y1 这两条线段

即需要求这两条线段的最大公约数  使得点最多

代码转自:https://blog.csdn.net/xiang_6/article/details/78523634   懒得写了

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define PI acos(-1.0)
#define in freopen("in.txt", "r", stdin)
#define out freopen("out.txt", "w", stdout)
 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e6 + 7, maxd = 20 + 7, mod = 1e9 + 7;
const int INF = 0x7f7f7f7f;
 
int T;
 
ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a%b);
}
 
int main() {
    int T;
    scanf("%d", &T);
    for(int tt = 1; tt <= T; ++tt) {
        ll a, b, c, d;
        scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
        ll ans = gcd( abs(a-c), abs(b-d) );
        printf("Case %d: %lld\n", tt, ans+1);
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/WTSRUVF/p/9343078.html