扩展欧几里得算法——pku1061
2011-04-26 21:50
267 查看
直接用欧几里得
AX+BY=gcd(A,B);
问题里s(n-m)+k*l=x-y
所以存在s,k的整数解的话就要 (x-y)%gcd(n-m,l)
再分情况考虑n-m是否是正负 枚举k得出解
View Code
AX+BY=gcd(A,B);
问题里s(n-m)+k*l=x-y
所以存在s,k的整数解的话就要 (x-y)%gcd(n-m,l)
再分情况考虑n-m是否是正负 枚举k得出解
View Code
#include<stdio.h> __int64 Ext_gcd(__int64 a,__int64 b,__int64 &x,__int64 &y) { if(b==0) { x=1, y=0; return a; } __int64 res= Ext_gcd(b,a%b,y,x); y-= a/b*x; return res; } __int64 Ax_b_mod_n(__int64 a,__int64 b,__int64 n)//同余方程 (m-n)*t = (y-x) (mod L) 的最小正整数解 { __int64 res,x,y,t; res= Ext_gcd(a,b,x,y); if(n%res==0) { x= x*n/res; t= b/res; if(t<0) t=-t; x= (x%t+t)%t; return x; } return -1; } int main() { __int64 x,y,m,n,len,t; scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&len); t= Ax_b_mod_n(m-n,len,y-x); if(t==-1) printf("Impossible\n"); else printf("%I64d\n",t); }
相关文章推荐
- POJ 1061青蛙的约会 ,扩展欧几里得算法
- 扩展欧几里得算法——POJ1061
- pku 1061 欧几里德扩展定理
- 扩展欧几里得算法的应用 POJ 2115 POJ 2142 POJ 1061 HDU 2669 HDU 1576 SGU 106
- [拓展欧几里得算法]Pku1061-青蛙的约会
- PKU1061 解题报告 青蛙的约会 __用扩展欧几里得解模同余方程
- poj 1061青蛙的约会 (数论:扩展的欧几里得算法)
- poj-1061-青蛙约会-扩展的欧几里得算法的模板题
- PKU-1061 青蛙的约会 (扩展的欧几里德算法)
- PKU 1061 (扩展欧几里德)
- 扩展欧几里得算法(POJ 1061)
- 计算机程序设计艺术一扩展欧几里得算法
- 浅谈数论(四)扩展欧几里得算法与乘法逆元
- 扩展欧几里得算法
- POJ 1061 青蛙的约会 扩展欧几里得
- POJ 1061(转)扩展欧几里德
- poj-1061-青蛙的约会【扩展欧几里得】
- 扩展欧几里得算法
- POJ 1061 青蛙的约会(扩展欧几里德)
- POJ 1061 青蛙的约会 扩展欧几里得