您的位置:首页 > 其它

codeforces 17C C. Line(exgcd解不定方程)

2015-08-24 12:00 489 查看

题目链接:

codeforces 17C

题目大意:

解Ax+By+C = 0

题目分析:

拓展欧几里得解不定方程模板题

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long LL;

LL exgcd ( LL a , LL b  , LL &x , LL &y )
{
    if ( b == 0 )
    {
        x = 1;
        y = 0;
        return a;
    }
    LL r = exgcd ( b , a%b , x , y );
    LL t = x;
    x = y;
    y = t - a/b*y;
    return r;
}

LL a,b,c,x,y;

int main ( )
{
    while ( ~scanf ( "%lld%lld%lld" , &a , &b , &c ) )
    {
        c = -c;
        LL r = exgcd ( a , b , x , y );
        if ( c%r ) puts ( "-1" );
        else
        {
            x *= c/r;
            y *= c/r;
            printf ( "%I64d %I64d\n" , x , y );
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: