20160321 CodeForces 7C Line(拓展欧几里得)

来源:互联网 发布:恶意广告拦截软件 编辑:程序博客网 时间:2024/05/22 03:36

题意:

给你a,b,c,求在ax+by+c=0线段上的坐标为整数的点,没有输出-1.

思路:

ax+by=-c符合拓展欧几里得的形式,模板走你,注意a或b为零的处理。

#include <iostream>#include <stdio.h>using namespace std;long long exgcd(long long a,long long b,long long &x,long long &y){    if(b==0)    {        x=1;        y=0;        return a;    }    else    {        long long r=exgcd(b,a%b,y,x);        y-=x*(a/b);        return r;    }}int main(){    long long a,b,c,x1,x2,x,y;    while(scanf("%I64d%I64d%I64d",&a,&b,&c)==3)    {        x1=x2=0;        if(a==0)        {              if(-c%b==0)                 printf("0 %I64d\n",-c/b);              else                 printf("-1\n");              continue;        }        if(b==0)        {              if(-c%a==0)                 printf("%I64d 0\n",-c/a);              else                 printf("-1\n");              continue;        }        if(c>0)        {            a=-a;            b=-b;        }        else            c=-c;        if(a<0)        {            a=-a;            x1=1;        }        if(b<0)        {            b=-b;            x2=1;        }        long long r=exgcd(a,b,x,y);        if(c%r!=0)        {            printf("-1\n");        }        else        {            if(x1)                x=-x;            if(x2)                y=-y;            printf("%I64d %I64d\n",x*(c/r),y*(c/r));        }    }    return 0;}



0 0