Description
The Sky is Sprite.
The Birds is Fly in the Sky.
The Wind is Wonderful.
Blew Throw the Trees
Trees are Shaking, Leaves are Falling.
Lovers Walk passing, and so are You.
................................Write in English class by yifenfei
Girls are clever and bright. In HDU every girl like math. Every girl like to solve math problem!
Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead.
The Birds is Fly in the Sky.
The Wind is Wonderful.
Blew Throw the Trees
Trees are Shaking, Leaves are Falling.
Lovers Walk passing, and so are You.
................................Write in English class by yifenfei
Girls are clever and bright. In HDU every girl like math. Every girl like to solve math problem!
Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead.
Input
The input contains multiple test cases.
Each case two nonnegative integer a,b (0<a, b<=2^31)
Each case two nonnegative integer a,b (0<a, b<=2^31)
Output
output nonnegative integer X and integer Y, if there are more answers than the X smaller one will be choosed. If no answer put "sorry" instead.
Sample Input
77 51 10 44 34 79
Sample Output
2 -3 sorry 7 -3
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <cstdio>
#include <iostream>
#include <map>
#define ll long long
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
int main()
{
long long int a,b,i;
int flag=0;
while(scanf("%lld %lld",&a,&b)!=EOF)
{
flag=0;
if(a==1&&b==1)
printf("2 -1\n");
else if(a==1)
printf("%lld -1\n",b+1);
else if(b==1)
printf("1 -%lld\n",a-1);
else if(gcd(a,b)!=1)
{
printf("sorry\n");
}
else
{
if(a>b)
{
for(i=1;i<=b;i++)
{
if((i*a-1)%b==0)
{
printf("%lld -%lld\n",i,(i*a-1)/b);
flag=1;
break;
}
}
}
else
{
for(i=1;i<=a;i++)
{
if((i*b+1)%a==0)
{
printf("%lld -%lld\n",(i*b+1)/a,i);
flag=1;
break;
}
}
}
if(flag==0)
printf("sorry\n");
}
}
return 0;
}