poj 2142 The Balance
扩展欧几里得的运用:
View Code
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<set> #include<map> #include<cstring> #include<vector> #define LL long long using namespace std; LL Ex_Gcd( LL a , LL b , LL &x , LL &y ) { if( b ==0 ) { x = 1 ; y = 0; return a; } LL mod = Ex_Gcd( b , a%b , x , y ); LL temp = x; x = y; y = temp - ( a / b ) * y; return mod; } int main( ) { LL A,B,C,x,y,X,Y; while( scanf( "%I64d %I64d %I64d",&A,&B,&C ),A||B||C ) { LL mod = Ex_Gcd( A , B , x ,y ); A /= mod; B /= mod; C /= mod; //当x是最小正整数解 X = ( (x*C)%B + B )%B; Y = ( C - X*A )/B; if( Y < 0 ) Y = -Y; //当y是最小正整数解 y = ( (y*C)%A + A )%A; x = ( C - y*B )/A; if( x < 0 ) x = -x; //判断和最小 if( x + y < X + Y ) { X = x ; Y = y; } printf( "%I64d %I64d\n",X,Y); } //system( "pause" ); return 0; }


浙公网安备 33010602011771号