The Diophantine Equation
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1116 Accepted Submission(s): 287
Problem Description
We will consider a linear Diaphonic equation here and you are to find out whether the equation is solvable in non-negative integers or not.
Easy, is not it?
Easy, is not it?
Input
There will be multiple cases. Each case will consist of a single line expressing the Diophantine equation we want to solve. The equation will be in the form ax + by = c. Here a and b are two positive integers expressing the co-efficient of two variables x and y.
There are spaces between:
1. “ax” and ‘+’
2. ‘+’ and “by”
3. “by” and ‘=’
4. ‘=’ and “c”
c is another integer that express the result of ax + by. -1000000<c<1000000. All other integers are positive and less than 100000. Note that, if a=1 then ‘ax’ will be represented as ‘x’ and same for by.
There are spaces between:
1. “ax” and ‘+’
2. ‘+’ and “by”
3. “by” and ‘=’
4. ‘=’ and “c”
c is another integer that express the result of ax + by. -1000000<c<1000000. All other integers are positive and less than 100000. Note that, if a=1 then ‘ax’ will be represented as ‘x’ and same for by.
Output
You should output a single line containing either the word “Yes.” or “No.” for each input cases resulting either the equation is solvable in non-negative integers or not. An equation is solvable in non-negative integers if for non-negative integer value of x and y the equation is true. There should be a blank line after each test cases. Please have a look at the sample input-output for further clarification.
Sample Input
2x + 3y = 10 15x + 35y = 67 x + y = 0
Sample Output
Yes. No. Yes. HINT: The first equation is true for x = 2, y = 2. So, we get, 2*2 + 3*2=10. Therefore, the output should be “Yes.”
一开始没有看到非负解,写了个错误的代码,只判断是否有解。。。。
代码一:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int exgcd(int a,int b,int &x,int &y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
int r=exgcd(b,a%b,x,y);
int t=x;
x=y;
y=t-a/b*y;
return r;
}
bool linear_equation(int a,int b,int c,int &x,int &y)
{
int d=exgcd(a,b,x,y);
if(c%d)
return false;
int k=c/d;
x*=k; y*=k;
return true;
}
int main()
{
int c,x,y,N,M;
char a[10],b[10];
while(scanf("%s + %s = %d",a,b,&c)!=EOF)
{
N=atoi(a);
if(N==0)N=1;
M=atoi(b);
if(M==0)M=1;
if(linear_equation(N,M,c,x,y))
printf("Yes.\n\n");
else
printf("No.\n\n");
}
return 0;
}
代码二:(正解!)
#include<stdio.h>
#include<stdlib.h>
long long gcd(long long x,long long y)
{
return y==0?x:gcd(y,x%y);
}
long long exgcd(long long a,long long b,long long &x,long long &y)
{
long long r,t;
if(b==0)
{
x=1;
y=0;
return a;
}
r=exgcd(b,a%b,x,y);
t=x;
x=y;
y=t-(a/b)*y;
return r;
}
int main()
{
long long c,x,y,N,M,t,d,n,k;
char a[100],b[100];
while(scanf("%s + %s = %lld",a,b,&c)!=EOF)
{
N=atoi(a);
if(N==0)N=1;
M=atoi(b);
if(M==0)M=1;
d=gcd(N,M);
if(c%d!=0)
{
printf("No.\n\n");
continue;
}
n=exgcd(N,M,x,y);
x=x*c/d;
t=M/d;
x=(x%t+t)%t;
k=(c-N*x)/M;
if(k>=0)
{
printf("Yes.\n\n");
continue;
}
y=y*c/d;
t=N/d;
y=(y%t+t)%t;
k=(c-M*y)/N;
if (k>=0)
{
printf("Yes.\n\n");
continue;
}
printf("No.\n\n");
}
return 0;
}