Line(扩展欧几里得)

题意:本题给出一个直线,推断是否有整数点在这条直线上;

分析:本题最重要的是在给出的直线是不是平行于坐标轴,即A是不是为0或B是不是为0.。此外。本题另一点就是C输入之后要取其相反数,才干进行扩展欧几里得求解

关于扩展欧几里得详见:http://blog.csdn.net/qq_27599517/article/details/50888092

代码例如以下:

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
#include <utility>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;
long long gcd(long long a,long long b){
    if(b==0)return a;
    return gcd(b,a%b);
}
void _gcd(long long a,long long b,long long &x,long long &y){
    if(b==1){
        x=1;
        y=1-a;
        return;
    }
    else{
        long long x1,y1;
        _gcd(b,a%b,x1,y1);
        x=y1;
        y=x1-(a/b)*x;
    }
}
int main(){
    long long a,b,c;
    scanf("%I64d%I64d%I64d",&a,&b,&c);
    c=-c;
    if(a==0&&b==0){
        puts("-1");
        return 0;
    }
    if(a==0&&b!=0){
        if(c%b==0){
            cout<<0<<" "<<c/b<<endl;
        }
        else puts("-1");
        return 0;
    }
    if(a!=0&&b==0){
        if(c%a==0){
            cout<<c/a<<" "<<0<<endl;
        }
        else puts("-1");
        return 0;
    }
    int g=gcd(a,b);
    if(c%g!=0){
        puts("-1");
        return 0;
    }
    c/=g;
    a/=g;
    b/=g;
    long long x,y;
    _gcd(a,b,x,y);
    x=(x*c%b+b)%b;
    y=(c-a*x)/b;
    cout<<x<<" "<<y<<endl;
    return 0;
}


原文地址:https://www.cnblogs.com/yangykaifa/p/7094429.html