CodeForces 7 C.Line(一元线性同余方程)

Description

给出方程 Ax+By+C=0 的任一介于 51018 ~ 51018 之间的解,不存在则输出 1

Input

三个整数 A,B,C(2109A,B,C2109,A2+B2>0)

Output

如果存在介于 51018 ~ 51018 之间的解则输出任一解,不存在输出 1

Sample Input

2 5 3

Sample Output

6 -3

Solution

特判 A=0 B=0 的情况, A,B0 时解一元线性同余方程即可

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100001;
ll extend_gcd(ll a,ll b,ll &x,ll &y)
{
    ll d=a;
    if(b)d=extend_gcd(b,a%b,y,x),y-=(a/b)*x;
    else x=1,y=0;
    return d;
}
int flag;
ll linear(ll a,ll b,ll c)
{
    ll x,y;
    ll g=extend_gcd(a,c,x,y);
    if(b%g)
    {
        flag=0;
        return -1;
    }
    x=x*(b/g);
    ll mod=c/g;
    x=(x%mod+mod)%mod;
    return x;
}
int main()
{
    ll a,b,c;
    while(~scanf("%I64d%I64d%I64d",&a,&b,&c))
    {
        flag=1;
        c=-c;
        if(b<0)a=-a,b=-b,c=-c;
        if(a==0||b==0)
        {
            if(b==0)
            {
                if(c%a==0)printf("%I64d 0\n",c/a);
                else printf("-1\n");
            }
            else
            {
                if(c%b==0)printf("0 %I64d\n",c/b);
                else printf("-1\n");
            }
            continue;
        }
        ll x=linear(a,c,b);
        if(!flag)printf("-1\n");
        else 
        {
            ll y=(c-a*x)/b;
            printf("%I64d %I64d\n",x,y);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值