HDU 2669 Romantic

简介: 题意:找出最小的非负整数X,使之满足式子X*a + Y*b = 1。

题意:找出最小的非负整数X,使之满足式子X*a + Y*b = 1。


思路:简单的扩展欧几里德。


#include<stdio.h>
int exgcd(int a,int b,int &x,int &y) 
{
  int i,j;
  if(b==0)
  {
    x=1;
    y=0;
    return a;
  }
  i=exgcd(b,a%b,x,y);
  j=x;
  x=y;
  y=j-a/b*y;
  return i;
}
int main()
{
  int a,b,x,y,r;
  while(scanf("%d %d",&a,&b)!=EOF)
  {
      r=exgcd(a,b,x,y);
    if(r==1)
    {
      while(x<0)
      {
        x+=b;
        y-=a;
      }
      printf("%d %d\n",x,y);
    }
    else
      printf("sorry\n");
  }
  return 0;
}
相关文章
|
Java 测试技术
hdu 1228 A + B
hdu 1228 A + B
58 0
|
人工智能 Java
2021杭电多校5-Arrary-hdu7020
Array Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 965 Accepted Submission(s): 312 Problem Description Given an integer array a[1…n].
181 0
2021杭电多校5-Arrary-hdu7020
|
人工智能 BI Java
HDU 1003
Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 105228    Accepted Submission(s): 242...
897 0