HDU 3270 The Diophantine Equation

The Diophantine Equation

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


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 = 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.”
 

Author
Muhammed Hedayet
 

Source
 

Recommend
chenheng

题意:判断ax+by=c是否存在x y位非负数解

可以用扩展欧几里得求出一个最小x,根据y=(c-ax)/b求得,再判断x y是否同时位非负数就行,同理计算一个最小y


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<cmath>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;

#define pi acos(-1.0)
#define eps 1e-10
#define pf printf
#define sf scanf
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define e tree[rt]
#define _s second
#define _f first
#define all(x) (x).begin,(x).end
#define mem(i,a) memset(i,a,sizeof i)
#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)
#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)
#define mi ((l+r)>>1)
#define sqr(x) ((x)*(x))

const int inf=0x3f3f3f3f;
char g[100];
ll A,B,C,D,x,y;

ll e_gcd(ll a,ll b,ll& x,ll& y)
{
    if(!b)
    {
        x=1,y=0;
        return a;
    }
    ll ans=e_gcd(b,a%b,x,y),tmp=x;
    x=y,y=tmp-a/b*y;
    return ans;
}

int main()
{
    while(~sf("%s",g))
    {
        int tag=0;
        A=B=C=0;
        for(int i=0;i<strlen(g)-1;i++)
            A=A*10+g[i]-'0';
        sf("%s",g);
        sf("%s",g);
        for(int i=0;i<strlen(g)-1;i++)
            B=B*10+g[i]-'0';
        sf("%s",g);
        sf("%s",g);
        for(int i=0;i<strlen(g);i++)
            C=C*10+g[i]-'0';
        if(!A)A=1;
        if(!B)B=1;
        D=e_gcd(A,B,x,y);
        if(C%D)
        {
            puts("No.\n");
            continue;
        }
        x=x*C/D;//计算一个x
        ll t=B/D;//x的周期
        x=(x%t+t)%t;//计算一个最小非负整数x
        ll k=(C-A*x)/B;//计算对应y
        if(k>=0)tag=1;//判断y是否大于0
        y=y*C/D;
        t=A/D;
        y=(y%t+t)%t;
        k=(C-B*y)/A;
        if(k>=0)tag=1;
        puts(tag?"Yes.\n":"No.\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值