codevs 1200 同余方程 (Extend_Eulid)

/*
扩展欧几里得 
ax%b==1  -> ax-by==1 
求不定方程的一组解 使x为最小正整数解 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int x,y,gcd;
int Extend(int a,int b)
{
    if(b==0)
      {
          x=1;y=0;
          gcd=a;
      }
    else 
      {
          Extend(b,a%b);
          int tmp=x;
          x=y;
          y=tmp-a/b*y;
      }
}
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    Extend(a,-b);
    x=x*(1/gcd);
    if(x<0)while(x<0)x=x+b;
    else while(x-b>0)x=x-b;
    printf("%d\n",x);
    return 0;
}

 

posted @ 2016-05-17 19:58  一入OI深似海  阅读(227)  评论(0编辑  收藏  举报