POJ 1061 && POJ 2142 两道扩展欧几里得简单题
来源:互联网 发布:中端单反推荐 知乎 编辑:程序博客网 时间:2024/05/16 19:38
额 扩展欧几里得在暑假集训的数学周就刷过,但是又是一味的套模板,导致到了最后BALL东西没学到。先把这算法的一些基本的东西写一下o(╯□╰)o
最简单的: gcd(a,b)=gcd(b,a%b)
证明:不妨设a=kb+r 则r=amodb ,若gcd(a,b)=d,则有a%d=0&&b%d=0又因为r=a-kb所以r%d=0 故gcd(b,a%b)=d则有 gcd(a,b)=gcd(b,a%b)。
然后对于形如ax+by=gcd(a,b)的形式
证明:设a>b
1.若b=0 则x=1,y=0 gcd(a,b)=a;
2.若a*b!=0不妨设 ax1+by1=gcd(a,b),通过前面的等量转换有:bx2+(a%b)y2=gcd(b,a%b)===>bx2+(a-(a/b)*b)*y2=ax1+by1。
再有对应相等有:x1=y2,y1=x2-(a/b)*y2,一直递归下去,总能得到1的形式
第一大应用:求解不定方程的解
形如 ax+by=c的二元不定方程,首先若c%gcd(a,b)!=0则不可能有整数解。
若等于零 则我们先求ax+by=gcd(a,b)的一组特解不妨设为x0,y0 则ax+by=c的一组特解可以写成形如x=x*c/gcd(a,b) y=y*c/gcd(a,b)的形式,
进而求出ax+by=c的通解为:x=x+b*t/gcd(a,b) y=y-a*t/gcd(a,b),t是任意整数
关于求出x>0的解,这里主要讨论x>0的最小解。为了求该x,主要是确定通解中t的值应为多少,如果x原来就大于0,那么直接对b/gcd(a,b)取模即可,如果x<0,那么可以让-x对b/gcd(a,b)取模,之后取反再加上b/gcd(a,b)。其实如果用程序,那么可以统一解决,设t=b/gcd(a,b),那么有x'=(x%t+t)%t,然后再求对应的y'即可.(这一段是复制学长当年写的o(╯□╰)o)
POJ 1061
Description
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4题目意思描述是中文。。 很明显了,假设最小需要t步则 (x+mt)恒等于(y+nt)mod(L)写成一般形式有(n-m)*t+kL=x-y
很自然的转化成了ax+by=c的x的最小正整数解的形式。。。
于是就很简单了。。 不过记住用long long 这个暑假因为犯过错。。 所以记住了。
#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>using namespace std;long long a,b,c,d,x,y;long long m,n,xx,yy,l;long long t;long long ex_gcd(long long a,long long b,long long &x,long long &y){ long long res,tmp; if(b==0) { x=1; y=0; return a; } res=ex_gcd(b,a%b,x,y); tmp=x; x=y; y=tmp-(a/b)*y; return res;}int main(){ while(cin>>x>>y>>m>>n>>l) { a=n-m; b=l; c=x-y; d=ex_gcd(a,b,xx,yy);//ax+by=gcd(a,b)的解 if(c%d!=0) { cout<<"Impossible"<<endl; } else { xx=xx*(c/d);//ax+by=c的解 t=b/d; xx=(xx%t+t)%t;//最小正整数解 cout<<xx<<endl; } } return 0;}
POJ 2142
Description
You are asked to help her by calculating how many weights are required.
Input
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.
Output
- 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 Input
700 300 200500 200 300500 200 500275 110 330275 110 385648 375 40023 1 100000 0 0
Sample Output
1 31 11 00 31 149 743333 1题目更加通俗 求ax+by=d且|x|+|y|要最小
实际上写成|x+b*t/g|+|y-a*t/g|最小。。 观察发现只要尽可能的使得a*t/g=y就能保证最小了 然后我们可以得到t=y*g/a,接着我们对t附近的一些数进行扫描就可以得到最小值。
当然听说可以把t t+1 t-1三个值带入就可以求得极值不用扫描也可以。。
#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>using namespace std;int a,b,d;int ex_gcd(int a,int b,int &x,int &y){ int tmp; int res; if(b==0) { x=1; y=0; return a; } res=ex_gcd(b,a%b,x,y); tmp=x; x=y; y=tmp-(a/b)*y; return res;}int main(){ while(cin>>a>>b>>d) { if(a==b&&b==0&&d==0) return 0; int flag=0; if(a<b)//看看有没有交换 { swap(a,b); flag=1; } int x0,y0; int g=ex_gcd(a,b,x0,y0); int x=x0*d/g; int y=y0*d/g; int t=y*g/a; int x1,y1; int xx,yy; int mi=999999999; for(int i=t-5;i<t+5;i++)//扫描 { x1=abs(x+b*i/g); y1=abs(y-a*i/g); if(x1+y1<mi) { mi=x1+y1; xx=x1; yy=y1; } } if(flag) { cout<<yy<<" "<<xx<<endl; } else { cout<<xx<<" "<<yy<<endl; } } return 0;}
- POJ 1061 && POJ 2142 两道扩展欧几里得简单题
- POJ 2142 扩展欧几里得
- POJ 2142 扩展欧几里得
- POJ 2142 扩展欧几里得
- poj 2142 扩展欧几里得
- poj 1061 扩展欧几里得
- poj 1061 扩展欧几里得
- POJ 1061 扩展欧几里得
- poj 1061 扩展欧几里得
- poj 1061(扩展欧几里得)
- POJ 1061 扩展欧几里得
- POJ 1061 扩展欧几里得
- POJ 1061 扩展欧几里得
- poj 2142 数论 扩展欧几里得
- 扩展欧几里得算法...... POJ 1061
- POJ 1061(扩展欧几里得)
- poj 1061(扩展欧几里得算法)
- 扩展欧几里得(poj 2115 poj 1061)
- LINQ TO SQL 实现无限递归查询
- Log4J日志文件的详细配置
- Sum Root to Leaf Numbers -- LeetCode
- ActionContext. ServletActionContext.getRequest().getSession();区别
- Linux内核源代码分析——fork()原理&多进程网络模型
- POJ 1061 && POJ 2142 两道扩展欧几里得简单题
- MongoDB学习
- 28岁大龄女嫁46岁剩男 婆婆:住我家就得交房租
- orace10g手动创建数据库
- 硬盘数据恢复过程记录(断断续续更新中……)
- ActionContext.getContext().getSession().put("user", getUsername()); request.setAttribute("username
- Choose a destination with a supported architecture in order to run on this device.
- jQuery Ajax 全解析
- 平衡二叉树——AVL树的原理和实现