pOJ-1061 exgcd求同余方程组

本文介绍了解决形如(m-n)*a+b*l=y-x的线性方程组的方法,通过扩展欧几里得算法(exgcd)求解a*x+b*y=c的整数解,并进一步求得最小正整数解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接

就是求(m-n)*a+b*l=y-x,

类似于求解a*x+b*y=c,r=gcd(a,b),当c%r==0时有解,用exgcd求出a*x+b*y=gcd(a,b)的解,然后x*c/gcd(a,b)就是其中一个解,最后求最小正整数解,就是(x%b+b)%b,要求y的话,对应求解即可

#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pii pair<int,int>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)

using namespace std;

const double g=10.0,eps=1e-12;
const int N=100000+10,maxn=400000+10,inf=0x3f3f3f3f;

ll gcd(ll a,ll b)
{
    return b?gcd(b,a%b):a;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(!b)
    {
        x=1,y=0;
        return a;
    }
    ll ans=exgcd(b,a%b,x,y);
    ll t=x;x=y;y=t-a/b*y;
    return ans;
}
int main()
{
    ll x,y,n,m,l,a,b;
    scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l);
    if(m<n)swap(x,y),swap(m,n);
    ll r=gcd(m-n,l);
    if((y-x)%r!=0)return 0*puts("Impossible");
    exgcd(m-n,l,a,b);
    a*=(y-x)/r;
    a=(a%l+l)%l;
    printf("%lld\n",a);
    return 0;
}
/********************

********************/
View Code

 

转载于:https://www.cnblogs.com/acjiumeng/p/8445594.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值