exgcd之求解最小正整数解

套用exgcd模板求得的是一组特殊解,但其实这一个方程式是有一个解系,在很多问题中是要你求得最小整数解,下面我们就解决这个问题,在阅读过很多博客加上自己的理解总结了两种方法(其实差距不大)

1、a*x+b*y=gcd(a,b)

void exgcd(int a,int b,int &x,int &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return;
    }
    int x1,y1;
    exgcd(b,a%b,x1,y1);
    x=y1;
    y=x1-(a/b)*y1;
}
x=(x%b+b)%b;(求出的就是最小正整数解)
2.可以说这是求最小正整数的模板
LL e_gcd(LL a,LL b,LL &x,LL &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    LL ans=e_gcd(b,a%b,x,y);
    LL temp=x;
    x=y;
    y=temp-a/b*y;
    return ans;
}

LL cal(LL a,LL b,LL c)
{
    LL x,y;
    LL gcd=e_gcd(a,b,x,y);
    if(c%gcd!=0) return -1;
    x*=c/gcd;//转化为a*x+b*y=c的解
    b/=gcd;//约去c后原来b就变为了b/gcd;
    if(b<0) b=-b;//如果b为负数就去绝对值
    LL ans=x%b;
    if(ans<=0) ans+=b;//求最小正整数解
    return ans;//返回的就是最小正整数解
}
这个地方给大家推荐一个博客http://blog.csdn.net/zhjchengfeng5/article/details/7786595(写的非常奈斯)
这两种本质上没啥区别,只是在一些问题中a,b等系数可能为负,第一种需要预处理,而第二种则可以直接用
例题

  
  
The Sky is Sprite. The Birds is Fly in the Sky. The Wind is Wonderful. Blew Throw the Trees Trees are Shaking, Leaves are Falling. Lovers Walk passing, and so are You. ................................Write in English class by yifenfei Girls are clever and bright. In HDU every girl like math. Every girl like to solve math problem! Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead.

Input

The input contains multiple test cases. Each case two nonnegative integer a,b (0<a, b<=2^31)

Output

output nonnegative integer X and integer Y, if there are more answers than the X smaller one will be choosed. If no answer put "sorry" instead.

Sample Input

77 51
10 44
34 79

Sample Output

2 -3
sorry
7 -3
题目大意就是给你两个数,然后求出最小整数解x,在根据方程求出y
代码:
#include <iostream>
#include <stdio.h>
using namespace std;
long long gcd(long long x,long long y)
{
    return y?gcd(y,x%y):x;
}
void exgcd(long long a,long long b,long long &x,long long &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return;
    }
    long long x1,y1;
    exgcd(b,a%b,x1,y1);
    x=y1;
    y=x1-(a/b)*y1;
}
int main()
{
    long long n,t,k,x,y,p;
    while(scanf("%lld%lld",&t,&k)!=-1)
    {
        if(1%gcd(t,k)!=0)
        printf("sorry\n");
        else
        {
            exgcd(t,k,x,y);
            x=(x%k+k)%k;
            y=(1-t*x)/k;
            printf("%lld %lld\n",x,y);

        }
    }
    return 0;
}
例题---最最最经典的裸exgcd

  
  
两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。 我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。

Input

输入只包括一行5个整数x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。

Output

输出碰面所需要的跳跃次数,如果永远不可能碰面则输出一行"Impossible"

Sample Input

1 2 3 4 5

Sample Output

4
题解
假设:跳t此后相遇,相遇时路乘差是pl(套了p圈)
列出方程:x+mt-(y+n*t)=pl;
化简为(n-m)*t+p*l=x-y;(n-m)与x-y的正负不知故用第二种
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

typedef long long LL;
typedef double DB;

LL e_gcd(LL a,LL b,LL &x,LL &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    LL ans=e_gcd(b,a%b,x,y);
    LL temp=x;
    x=y;
    y=temp-a/b*y;
    return ans;
}

LL cal(LL a,LL b,LL c)
{
    LL x,y;
    LL gcd=e_gcd(a,b,x,y);
    if(c%gcd!=0) return -1;
    x*=c/gcd;
    b/=gcd;
    if(b<0) b=-b;
    LL ans=x%b;
    if(ans<=0) ans+=b;
    return ans;
}

int main()
{
    LL a,b,m,n,k,t;
    while(scanf("%lld%lld%lld%lld%lld",&a,&b,&m,&n,&k)!=-1)
    {
        t=cal(n-m,k,a-b);
        if(t==-1)
        printf("Impossible\n");
        else
        {
            printf("%lld\n",t);
        }
    }
    return 0;
}

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值