HDU 2669

来源:互联网 发布:数据库应用教程 编辑:程序博客网 时间:2024/06/05 17:00

扩展欧几里德算法

#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include <iostream>#include<queue>using namespace std;int x,y;int gcd(int m,int n){        if(m==0)        {                x=0;                y=1;                return n;         }         else               {                      int tmp=gcd(n%m,m);                      int p1=y-n/m*x;                      int p2=x;                      x=p1;                      y=p2;                      return tmp;             }}int main(){    int a,b;    while(cin>>a>>b)    {        if(gcd(a,b)!=1)            cout<<"sorry"<<endl;        else        {            while(x<0)            {                x+=b;                y-=a;            }            while(x-b>=0)            {                x-=b;                y+=a;            }            cout<<x<<" "<<y<<endl;        }    }    return 0;}


0 0
原创粉丝点击