• Codeforces Beta Round #7 C. Line 扩展欧几里德


    C. Line
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from  - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist.

    Input

    The first line contains three integers AB and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corresponding coefficients of the line equation. It is guaranteed that A2 + B2 > 0.

    Output

    If the required point exists, output its coordinates, otherwise output -1.

    Examples
    input
    2 5 3
    output
    6 -3
    思路:简单的扩展欧几里德,就是b==0的情况小心点;
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    #define true ture
    #define false flase
    using namespace std;
    #define ll __int64
    #define inf 0xfffffff
    int scan()
    {
        int res = 0 , ch ;
        while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
        {
            if( ch == EOF )  return 1 << 30 ;
        }
        res = ch - '0' ;
        while( ( ch = getchar() ) >= '0' && ch <= '9' )
            res = res * 10 + ( ch - '0' ) ;
        return res ;
    }
    ll gcd(ll x,ll y)
    {
        return x%y==0?y:gcd(y,x%y);
    }
    void extend_Euclid(ll a, ll b, ll &x, ll &y)
    {
        if(b == 0)
        {
            x = 1;
            y = 0;
            return;
        }
        extend_Euclid(b, a % b, x, y);
        ll tmp = x;
        x = y;
        y = tmp - (a / b) * y;
    }
    int main()
    {
        ll a,b,c,x,y;
        cin>>a>>b>>c;
        c=-c;
        if(b==0)
        {
            if(c%a==0)
            {
                cout<<c/a<<" 0"<<endl;
            }
            else
            cout<<"-1"<<endl;
            return 0;
        }
        ll gg=gcd(a,b);
        if(c%gg==0)
        {
            extend_Euclid(a,b,x,y);
            cout<<c/gg*x<<" "<<c/gg*y<<endl;
        }
        else
        cout<<"-1"<<endl;
        return 0;
    }
    View Code
  • 相关阅读:
    ADO.NET操作PostgreSQL:数据库操作类(未封装)
    ADO.NET操作SQL Server:数据库操作类(已封装)
    ADO.NET操作SQL Server:数据库操作类(未封装)
    h5微信支付
    css3的transform:tanslateZ没有效果
    判断页面是否被嵌入iframe里面
    vue项目的环境变量
    iOS下调用元素的focus方法,input元素不聚焦,键盘不弹起的问题
    关于common.js里面的module.exports与es6的export default的思考总结
    Vue2.4.0 新增的inheritAttrs,attrs
  • 原文地址:https://www.cnblogs.com/jhz033/p/5458358.html
Copyright © 2020-2023  润新知