拡張ユークリッド--hdu3270

hdu3270

問題の説明

ここでは、線形Diaphonic方程式を検討すると、あなたは式が非負整数かどうかで解決できるかどうかを調べることです。
簡単に、それはないですか?

入力

複数のケースがあります。各ケースは、我々が解決したいディオファントス方程式を発現する単一のラインで構成されます。式は= cでフォーム斧+であろう。ここでAとBは、2つの変数xとyの共効率的に発現する2つの正の整数です。
間のスペースがあります。

  1. 「斧」と「+」
  2. 「+」や「によります」
  3. 「による」と「=」
  4. '=' と「C」

Cはによって斧+の結果を表現する別の整数です。-1000000 <C <1000000。他のすべての整数は正及び= 1、次いで「斧」が「X」として表さによってために同じになる場合、以下100000注あります。

出力

あなたが出力方程式が非負整数で解決可能であるかのいずれかで得られた各入力場合の単語「はい」または「いいえ」のいずれかを含む単一ラインべきです。xとyの負でない整数値のための式が真である場合、式は負でない整数で解決可能です。各テストケースの後に空白行があるはずです。さらに明確化のためのサンプル入力 - 出力を見てください。

サンプル入力

2x + 3y = 10
15x + 35y = 67
x + y = 0

サンプル出力

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

質問の意味:X、Y存在する場合、非負整数であり、出力はい、そうでない場合はありません

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

using namespace std;
typedef long long LL;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;

const LL N = 1e6+50;
const LL MOD = 1e9+7;
const LL INF = 0x3f3f3f3f;

#define lson l, m, rt>>1
#define rson m+1, r, rt>>1|1

LL exgcd(LL a, LL b, LL &x, LL &y)
{
    if(!b)
    {
        x = 1;y = 0;
        return a;
    }
    LL ret = exgcd(b, a%b, y, x);
    y -= x*(a/b);
    return ret;
}

int main()
{
    char sa[100], sb[100], sc[100];
    LL a, b, c, x, y;
    while(scanf("%s + %s = %s", sa, sb, sc) != EOF)
    {
        a = 0, b = 0, c = 0;
        for(int i = 0;sa[i] != 'x';++i)
            a = a*10+sa[i]-'0';
        for(int i = 0;sb[i] != 'y';++i)
            b = b*10+sb[i]-'0';
        for(int i = 0;sc[i];++i)
            c = c*10+sc[i]-'0';
        if(!a) a = 1;
        if(!b) b = 1;
        LL g = exgcd(a, b, x, y);
        if(c%g) puts("No.\n");
        else if(!c) puts("Yes.\n");
        else
        {
            x *= c/g;y *= c/g;
            LL t = b/g;//x最小加的数
            x = (x%t+t)%t;//x最小的时候
            if((c-a*x)/b > 0)
            {
                puts("Yes.\n");continue;
            }
            t = a/g;
            y = (y%t+t)%t;//y最小的时候
            if((c-b*y)/a > 0)
            {
                puts("Yes.\n");continue;
            }
            puts("No.\n");
        }
    }
    return 0;
}

おすすめ

転載: www.cnblogs.com/shuizhidao/p/11700094.html