【裴蜀定理】

裴蜀定理

定理

对于任意正整数 a, b,存在整数 s, t,使得 sa + tb = gcd(a, b)

证明

设ri为辗转相除法的余数。ri=ri-2 mod ri-1
初始r0=a,r1=b。
设rn = rn−2 − rn−1qn−1
则rn-1 =rn−3 − rn−2qn−2
则gcd(a,b)=rn = rn−2 −(rn−3 − rn−2qn−2)qn−1
直到 gcd(a, b)=sr0+tr1=sa+tb。

ecgcd

求s,t

思路

若已知rn–1x’+rny’=gcd(rn–1,rn)
设x,y满足rn-2x+rn-1y=gcd(rn–1,rn)
因为gcd(rn–1,rn)=gcd(rn–1,rn),
所以rn–1x’+rny’=rn-2x+rn-1y
rn=rn-2- ⌊ r n − 2 r n − 1 ⌋ r n − 1 \lfloor {\frac{r_{n-2}}{r_{n-1}} } \rfloor r_{n-1} rn1rn2rn1带入得
ax + by = bx′ + (a − ⌊ a b ⌋ \lfloor \frac{a}{b} \rfloor bab)y′化简得
ax + by = ay’ + b(x’ − ⌊ a b ⌋ \lfloor \frac{a}{b} \rfloor bay’)
x=y’,y=x’ − ⌊ a b ⌋ \lfloor \frac{a}{b} \rfloor bay’

代码

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;
 }

解二元一次不等式

设方程 ax + by = c 的一组解为 x = x0, y = y0
那么x = x0+ b g c d ( a , b ) \frac {b}{gcd(a,b)} gcd(a,b)b·t, y = y0 a g c d ( a , b ) \frac {a}{gcd(a,b)} gcd(a,b)a·t(t 为任意整数)构成了方程所有的解。

证明

设存在另外一组解ax + by = c,与 ax0 + by0 = c
相减得a(x − x0) = b(y0 − y)
a g c d ( a , b ) \frac{a}{gcd(a,b)} gcd(a,b)a(x-x0)= b g c d ( a , b ) \frac {b}{gcd(a,b)} gcd(a,b)b(y0-y)
a g c d ( a , b ) \frac{a}{gcd(a,b)} gcd(a,b)a|yo-y , b g c d ( a , b ) \frac {b}{gcd(a,b)} gcd(a,b)b|x-x0
x=x0+ b g c d ( a , b ) \frac{b}{gcd(a,b)} gcd(a,b)b·t , y = y0 a g c d ( a , b ) \frac{a}{gcd(a,b)} gcd(a,b)a·t

*当且仅当c mod gcd(a,b)=0时有整数解

应用

五指山(来源NEFU 84)

题目描述

大圣在佛祖的手掌中。
我们假设佛祖的手掌是一个圆圈,圆圈的长为 n,逆时针记为:0,1,2,··· ,n-1而大圣每次飞的距离为 d。现在大圣所在的位置记为 x,而大圣想去的地方在 y。要你告诉大圣至少要飞多少次才能到达目的地。

输入格式

有多组测试数据。
第一行是一个正整数 T,表示测试数据的组数;
每组测试数据包括一行,四个非负整数,分别为如来手掌圆圈的长度 n,筋斗所能飞的距离 d,大圣的初始位置 x 和大圣想去的地方 y。
注意孙悟空的筋斗云只沿着逆时针方向翻。

输出格式

对于每组测试数据,输出一行,给出大圣最少要翻多少个筋斗云才能到达目的地。如果无论翻多少个筋斗云也不能到达,输出 Impossible。

样例
InputOutput
2
3 2 0 2
3 2 0 1
1
2
数据范围与提示

对于全部数据,2<n<109,0<d<n,0<d<n,0≤x,y<n。

  • 列方程x>0的最小整数解
代码
 #include<cstdio>
long long t,n,d,x,y,gcd;
long long exgcd(long long a,long long b,long long &i,long long &j){
	if(b==0){
		i=1;j=0;
		return a;
	}
	long long q,p;
	p=exgcd(b,a%b,i,j);
	q=i;
	i=j;
	j=q-a/b*j;
	return p;
}
int main(){
	long long i,j;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld%lld%lld",&n,&d,&x,&y);
		gcd=exgcd(d,n,i,j);
		if((y-x)%gcd){
			printf("Impossible\n");
		}
		else{
			i=i*(y-x)/gcd;
			gcd=n/gcd;
			printf("%lld\n",(i%gcd+gcd)%gcd);
		}
	}
}

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
InputOutput
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
  • 求|x|+|y|最小值分类讨论
代码
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
long long exgcd(long long a,long long b,long long &X,long long &Y){
	if(b==0){
		X=1;
		Y=0;
		return a;
	}
	long long t=exgcd(b,a%b,X,Y);
	long long xi=X;
	X=Y;
	Y=xi-a/b*Y;
	return t;
}
long long ans1,ans2,A,B,C,x,y,gcd,x1,x2,k1,k2;
void check(long long o){
	long long i=abs(x+k1*o),j=abs(y-k2*o);
	if(i+j<ans1+ans2){
		ans1=i;
		ans2=j;
	}
	else if(i+j==ans1+ans2&&i*A+j*B<ans1*A+ans2*B){
		ans1=i;
		ans2=j;
	}
}
int main(){
	while(1){
		scanf("%lld%lld%lld",&A,&B,&C);
		if(C==0&&B==0&&A==0)
			break;
		gcd=exgcd(A,B,x,y);
		if(C%gcd)
			continue;
		ans1=ans2=1e9;
		x*=C/gcd;y*=C/gcd;
		k1=B/gcd;k2=A/gcd;
		x1=-x/k1;x2=y/k2;
		check(x1-1);check(x1);check(x1+1);
		check(x2-1);check(x2);check(x2+1);
		printf("%lld %lld\n",ans1,ans2);
	}
}

应该可以不用long long吧

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值