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

题目链接:

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 );
        }
    }
}

你可能感兴趣的:(codeforces,拓展欧几里得,不定方程求解)