zoukankan      html  css  js  c++  java
  • 扩展欧几里得--hdu3270

    hdu3270

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

    题意:x,y为非负整数,若存在,输出Yes,否则为No

    #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.
    ");
            else if(!c) puts("Yes.
    ");
            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.
    ");continue;
                }
                t = a/g;
                y = (y%t+t)%t;//y最小的时候
                if((c-b*y)/a > 0)
                {
                    puts("Yes.
    ");continue;
                }
                puts("No.
    ");
            }
        }
        return 0;
    }
    
    
  • 相关阅读:
    NodeJs使用Mysql模块实现事务处理
    Javascript(JS)对Cookie的读取、删除、写入操作帮助方法
    SLERP 导数
    General matrix representations for B-splines 论文理解
    搭建私人实体编译服务器
    A Micro Lie Theory 论文理解
    [备忘,无新意] undistort (求反函数对某个值的映射)使用迭代优化方法
    Dual Quaternion representing Rigid Transformation
    B 样条曲线的 SE(3) 应用
    [ceres-solver] From QuaternionParameterization to LocalParameterization
  • 原文地址:https://www.cnblogs.com/shuizhidao/p/11700094.html
Copyright © 2011-2022 走看看