hdu 3270 The Diophantine Equation

来源:互联网 发布:马自达3 知乎 编辑:程序博客网 时间:2024/05/21 10:11

The Diophantine Equation

Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 933 Accepted Submission(s): 250


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?

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.

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 = 1015x + 35y = 67x + 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>long long gcd(long long a,long long b,long long &x,long long &y){    if(b == 0)    {        y = 0;        x = 1;        return a;    }    long long d = gcd (b,a%b,x,y);    long long t=x;    x = y;    y = t - a/b*y;    return d;}long long to_int(char *s){    if(s[1] == 0)        return 1LL;    long long res=0;    for(int i = 0;s[i+1];++i)        res = res*10 + (s[i]-'0');    return res;}int main(){    long long a[3];    char str[5][100];    while(scanf("%s",str[0]) != EOF)    {        int i;        for(i=1;i < 5;++i)            scanf("%s",str[i]);        for(i=0;i<2;++i)            a[i] = to_int(str[i<<1]);        a[2]=0;        for (i=0;str[4][i];++i)            a[2] = a[2]*10 + str[4][i]-'0';        long long x=0,y=0;        long long d=gcd(a[0],a[1],x,y);        if(a[2] % d != 0)        {            printf("No.\n\n");            continue;        }        x = x * a[2]/d;        long long t = a[1] / d;        x = (x % t + t) % t;        if((a[2] - x * a[0])/a[1] >= 0)        {            printf("Yes.\n\n");            continue;        }        y = y * a[2] / d;        t = a[0] / d;        y = ((y % t + t) % t);        if((a[2] - y * a[1])/ a[0] >= 0 )        {            printf("Yes.\n\n");            continue;        }        printf("No.\n\n");    }    return 0;}/**///判断一个二元一次方程是否有非负整数解,///也就是给定ax+by=c,求是否存在x,y>=0使得式子成立。///先求出x的最小非负整数解和y的最小非负整数解,///暴力枚举也可///然后判断另一个数是否也非负即可///quetion? if C is negative integer?????int main(){    long long a,b,c,x,y,d,t,k;    char str[100];    while(scanf("%s",str) != EOF)    {        int i;        a = 0;        for(i = 0;str[i+1]; ++i)            a = a * 10 + str[i] - '0';        if(a == 0)            a = 1;        scanf("%s",str);        scanf("%s",str);        b=0;        for(i = 0; str[i+1] ; ++i)            b  = b * 10 +str[i] - '0';        if(b == 0)            b = 1;        scanf("%s",str);        scanf("%s",str);        c=0;        for(i=0;str[i];++i)            c = c * 10 + str[i] - '0';        d = gcd(a,b,x,y);        if( c%d !=0)        {            printf("No.\n\n");            continue;        }        x = x * c / d;        t = b / d;        x = (x % t + t)% t;        k = (c - x * a) / b;        if(k >=0)        {            printf("Yes.\n\n");            continue;        }        y = y * c / d;        t = a / d;        y = (y % t + t) % t;        k = (c - y * b ) / a;        if(k >=0 )        {            printf("Yes.\n\n");            continue;        }        printf("No.\n\n");    }    return 0;}*//*2x + 3y = 1015x + 35y = 67x + y = 0*/




原创粉丝点击