裴蜀定理

裴蜀定理

对于任意的正整数a,b,都存在整数s,t,使a*s+b*t=gcd(a,b)

拓展欧几里得算法:

裴蜀定理可写一个算法exgcd,去求得s,t的一组解

但首先我们要先知道gcd(a,b)=gcd(b,a%b)

又因为gcd(a,b)=a*s+b*tgcd(b,a%b)=b*{s}'+(a%b)*{t}'

且a%b=a-\left [ \frac{a}{b} \right ]*b

所以a*s+b*t=b*{s}'+(a-\left [ \frac{a}{b} \right ]*b)*{t}'

整理可得a*s+b*t=a* {t}'+b*({s}'-\left [ \frac{a}{b} \right ]*{t}')

所以当s={t}',t= {s}'-\left [ \frac{a}{b} \right ]*t时,等式成立

到最后, a*s+b*t=gcd(a,0),此时令s=1,t=0即可得出一组特解

代码实现如下
 

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

 应用:

对于实际问题中,我们常常遇到解一元二次方程的问题,形如a*x+b*y=c

根据裴蜀定理,我们知道a*s+b*t=gcd(a,b),一定有解,且若a*s+b*t=c有解,那一定有c=k*gcd(a,b)

即c是gcd(a,b)的倍数

那么我们令s=x,t=y,利用exgcd求得x,y的一组解后,并让x,y分别扩大k倍,就得到了a*x+b*y=c的一组特解x_{0},y_{0}

我们知道x的所有解的集合满足{ x|x_{0}+n*\frac{b}{gcd(a,b)}},y满足{y|y_{0}+n*\frac{a}{gcd(a,b)}}

由此可得x,y的任意一组解

例题:

The Balance

 Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. For example, to measure 200mg of aspirin using 300mg weights and 700mg weights, she can put one 700mg weight on the side of the medicine and three 300mg weights on the opposite side (Figure 1). Although she could put four 300mg weights on the medicine side and two 700mg weights on the other (Figure 2), she would not choose this solution because it is less convenient to use more weights.
You are asked to help her by calculating how many weights are required.

Input:

The input is a sequence of datasets. A dataset is a line containing three positive integers a, b, and d separated by a space. The following relations hold: a != b, a <= 10000, b <= 10000, and d <= 50000. You may assume that it is possible to measure d mg using a combination of a mg and b mg weights. In other words, you need not consider "no solution" cases.
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.

Output:

The output should be composed of lines, each corresponding to an input dataset (a, b, d). An output line should contain two nonnegative integers x and y separated by a space. They should satisfy the following three conditions.

  • You can measure dmg using x many amg weights and y many bmg weights.
  • The total number of weights (x + y) is the smallest among those pairs of nonnegative integers satisfying the previous condition.
  • The total mass of weights (ax + by) is the smallest among those pairs of nonnegative integers satisfying the previous two conditions.


No extra characters (e.g. extra spaces) should appear in the output.

Sample

InputcopyOutputcopy
700 300 200
500 200 300
500 200 500
275 110 330
275 110 385
648 375 4002
3 1 10000
0 0 0
1 3
1 1
1 0
0 3
1 1
49 74
3333 1

 代码如下:

#include<iostream>
#include<algorithm>
#define ll long long 
using namespace std;
ll a,b;
ll n,d,x,y;
ll ans1,ans2;
ll exgcd(ll a, ll b, ll &ax, ll &ay) {
	if(!b) {
		ax = 1;
		ay = 0;
		return a;
	}
	ll ans = exgcd(b, a%b, ax, ay);
	ll f = ax;
	ax = ay;
	ay = f-a/ b*ay;
	return ans;
}

int main () {
	while(cin>>a>>b>>d){
		if(a==0&&b==0&&d==0)return 0; 
		ll MIN=1e18;
		ll MINN=1e18;
		ll k=exgcd(a,b,x,y);
		x*=(d/k);
		y*=(d/k);
		ll q=b/k;
		ll p=a/k;
		ll xa=((x%q)+q)%q;
		ll ya=(d-(a*xa))/b;
		ll yb=((y%p)+p)%p;
		ll xb=(d-(b*yb))/a;
		for(int i=-10000;i<=10000;i++){
			ll x1=abs(xa+(i*b)/k);
			ll y1=abs(ya-(i*a)/k);
			ll x2=abs(xb+(i*b)/k);
			ll y2=abs(yb-(i*a)/k);
			if(x1+y1<MIN||(x1+y1==MIN&&(a*x1)+(b*y1)<=MINN)){
				ans1=x1,ans2=y1;
				MIN=x1+y1,MINN=(a*x1)+(b*y1);
			}
			if(x2+y2<MIN||(x2+y2==MIN&&(a*x2)+(b*y2)<=MINN)){
				ans1=x2,ans2=y2;
				MIN=x2+y2,MINN=(a*x2)+(b*y2);
			}
		} 
		printf("%lld %lld\n",ans1,ans2);
	}
}

 说实话这个做法比较麻烦

解法2:

#include<iostream>
#include<algorithm>
#define LL long long
#define MAX 5
#define INF 0x3f3f3f3f
using namespace std;
int exgcd(int a,int b,int &x,int &y){
    if(b == 0){
        x = 1;
        y = 0;
        return a;
    }
    int ans = exgcd(b,a%b,x,y);
    int temp = y;
    y = x - (a/b)*y;
    x = temp;
    return ans;
}
 
int gcd(int a, int b){
    if(b == 0) return a;
    return gcd(b, a%b);
}
 
int main(){
    int a,b,d;
    while(cin >> a >> b >> d){
        if(!a && !b && !d) break;
        int x,y;
        int m = gcd(a,b);
        int flag = 0;
        if(a < b){  // 确保a > b
            flag = 1;
            swap(a,b);
        }
        exgcd(a,b,x,y);
        x = x*d/m;
        y = y*d/m;  // 对解进行转化,扩展欧几里德算法是默认解的翡蜀方程
        int t = (y*m)/a;  
        int ans = INF;
        int x1,x2,y1,y2;
        for(int i = t-5;i <= t+5;i++){ 
            x1 = x + (b*i)/m;
            y1 = y - (a*i)/m;
            if(abs(x1) + abs(y1) < ans){
                x2 = x1;
                y2 = y1;
                ans = abs(x1) + abs(y1);
            }
        }
        if(flag == 0){
            cout << abs(x2) << " " << abs(y2) << endl;
        }else{
            cout << abs(y2) << " " << abs(x2) << endl;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值