math数学部分
来源:互联网 发布:经济增长数据 编辑:程序博客网 时间:2024/04/28 19:50
gcd最大公约数(欧几里德算法,迭代形式)
int gcd(int a,int b)//普通方法{int m,n,r;m=a>=b?a:b;//m>=nn=a<b?a:b;r=m%n;while(r!=0){m=n;n=r;r=m%n;}return n;}
lcm最小公倍数
int lcm(int a,int b){ return a/gcd(a,b)*b;}
gcd的优化,Stein算法+位运算,可以算较大的整数
int gcdcore(int a,int b){ if (a==0) return b; if (b==0) return a; while ((a & 0x1)==0) { a=a>>1; } if (a<b) { b=(b-a)>>1; return gcdcore(b,a); } else { a=(a-b)>>1; return gcdcore(a,b); }}int gcd_fast(int a,int b)//stein算法+位运算优化{ int c=0; while (((a & 0x1)==0)&&(( b & 0x1 )==0)) { a=a>>1; b=b>>1; c++; } if ((a & 0x1) == 0) { a=a>>1; return gcdcore(a,b)<<c; } else return gcdcore(b,a)<<c;}
扩展欧几里德算法(递归形式)
int ex_gcd(int a,int b,int &x,int &y){ int tmp,ans; if(b==0) { x=1; y=0; return a; } ans=ex_gcd(b,a%b,x,y); tmp=x; x=y; y=tmp-(a/b)*y; return ans;}
扩展欧几里德算法(迭代形式)
int exgcd(int m,int n,int &x,int &y)//迭代形式{ int x1,y1,x0,y0; x0=1; y0=0; x1=0; y1=1; x=0; y=1; int r=m%n; int q=(m-r)/n; while(r) { x=x0-q*x1; y=y0-q*y1; x0=x1; y0=y1; x1=x; y1=y; m=n; n=r; r=m%n; q=(m-r)/n; } return n;}
扩展欧几里德求乘法逆元
int cal(int a,int m)//乘法逆元{ int x,y; int gcd=ex_gcd(a,m,x,y); if(1%gcd!=0) return -1; x*=1/gcd; m=abs(m); int ans=x%m; if(ans<=0) ans+=m; return ans;}
0 0
- math数学部分
- [Math]数学与其分类
- 数学函数<math.h>
- 数学函数<math.h>
- Math(数学对象)
- math.h 数学函数库
- Math数学方法--常用
- lua数学库math
- Math数学函数
- Math 数学类
- 数学库(math)
- SSL P2676 数学math
- 数学类Math
- JavaScript Math (数学对象)
- 1.数学函数,math.h
- java数学函数(Math)
- Java中的Math数学类
- unity3d math 常用的数学
- Disjoint-Set并查集
- 报告
- UVA 1368(p58)----DNA Consensus String
- Leetcode 160:Intersection of Two Linked Lists
- cpu运行模式切换以及异常处理
- math数学部分
- Heap和stack有什么区别?
- leetcode 5. Longest Palindromic Substring
- GenyMotion不能启动的问题,Unable to start the virtual device.VirtualBox cannot start the virtual device
- 支付宝福卡破解——飘雪效果
- Post-quantum key exchange for the TLS protocol from the ring learning with errors problem
- 如何使用 F12 开发人员工具调试网页
- Android中 Parcelable和Serializable
- Manifest.xml文件中主要包括哪些信息?