标签:scan 数论 函数 main 扩展 amp style void scanf
首先,ax+by=gcd(a,b)肯定有解(相信度娘)
那么,ax+by=gcd(k*a,k*b)=gcd(a,b)*k也一定有解(解就是上面的x,y分别乘k)
我们写成ax+by=d, ( gcd(a,b)|d,即d能整除gcd(a,b) )
现在,已知a和b,让你求一组解x,y满足ax+by=gcd(a,b)
#include<cstdio> typedef long long LL; void ex_gcd(LL a, LL b, LL &x, LL &y, LL &d){ if (!b) {d = a, x = 1, y = 0;} else{ extend_Eulid(b, a % b, y, x, d); y -= x * (a / b); } } int main(){ LL a, b, d, x, y; while(~scanf("%lld%lld", &a, &b)){ ex_gcd(a, b, x, y, d); printf("%lld*a + %lld*b = %lld\n", x, y, d); } }
简化函数:
1 void ex_gcd(LL a, LL b, LL &d, LL &x, LL &y){ 2 if(!b){d = a; x = 1; y = 0;} 3 else{ex_gcd(b, a%b, d, y, x); y -= x*(a/b);} 4 }
标签:scan 数论 函数 main 扩展 amp style void scanf
原文地址:http://www.cnblogs.com/eastblue/p/7627057.html