求教,为什么这道题不考虑a,b是负数的情况呢?
  • 板块CF7C Line
  • 楼主xiaofeng258
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/2/29 16:55
  • 上次更新2024/2/29 20:14:53
查看原帖
求教,为什么这道题不考虑a,b是负数的情况呢?
290783
xiaofeng258楼主2024/2/29 16:55
#include<bits/stdc++.h>
#define int long long
using namespace std;

int gcd(int a,int b)
{
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}

int exgcd(int a,int b,int &x,int &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    int d=exgcd(b,a%b,x,y);
    int t=x;
    x=y;
    y=t-a/b*y;
    return d;
}

signed main()
{
	std::ios::sync_with_stdio(0);
	std::cin.tie(nullptr);
    int a,b,c;
    cin>>a>>b>>c;
    c=-c;
    int g=gcd(a,b);
    //cout<<gcd(-7,-3)<<'\n';

    if(c%g!=0)
    {
        cout<<-1<<'\n';
        return 0;
    }
    int a1=a/g,b1=b/g;
    int x,y;
    exgcd(a1,b1,x,y);
    x*=c/g,y*=c/g;
    cout<<x<<" "<<y<<'\n';
	return 0;
}

刚学exgcd,可能还不是特别理解。exgcd可以适用于负数情况吗?网上有人说要取相反数。

2024/2/29 16:55
加载中...