zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    读书笔记之理想设计的特征
    一些javascript 变量声明的 疑惑
    LINQ 使用方法
    Google MySQL tool releases
    读书笔记之设计的层次
    EF之数据库连接问题The specified named connection is either not found in the configuration, not intended to be used with the Ent
    转载 什么是闭包
    javascript面向对象起步
    Tips
    数据结构在游戏中的应用
  • 原文地址:https://www.cnblogs.com/jhz033/p/5458358.html
Copyright © 2011-2022 走看看