裴蜀定理
裴蜀定理:
对于任意的正整数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*t;gcd(b,a%b)=b*+(a%b)*
且a%b=a-*b
所以a*s+b*t=b*+(a-
*b)*
整理可得a*s+b*t=a* +b*(
-
*
)
所以当s=,t=
-
*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的所有解的集合满足{ x|+n*
},y满足{y|
+n*
}
由此可得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
Inputcopy | Outputcopy |
---|---|
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;
}
}
}
本文来自博客园,作者:MegaSam,转载请注明原文链接:https://www.cnblogs.com/MegaSamTXL/p/17607143.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】