当前位置:网站首页>GCD maximum common divisor
GCD maximum common divisor
2022-06-28 03:56:00 【I am already furious!】
gcd function
10 Advance preparation gcd
Let's start with a simple gcd function ( Recursive ), It should be understood at a glance , I won't talk about it here .
int gcd(int a,int b){
return a%b==0?b:gcd(b,a%b);
}
Here is the optimization , It is similar to the above principle , But the complexity is a little lower .
int gcd( int a , int b )
{
return b == 0 ? a : gcd( b , a%b ) ;
}
Binary fetch gcd
Learned from big brother Wang Yu , If even Wang Yu doesn't know , Then you have learned in vain QAQ
First, introduce a concept ,“^” Symbol , It's a decimal number , Or by bit .
Take a chestnut ;
5 Binary for 00101
9 Binary for 01001
After XOR, it is 01100
by 12
The following is the binary exchange of two numbers , It belongs to fast forward and fast read , Because binary is faster than decimal .
void swap( int &a , int &b )
{
a=a^b;
b=a^b;
a=a^b;
}
And then use this to do the operation gcd function
int gcd(int a,int b){
while(b=^a=^b^=a%=b);
return a;
// First a and b The remainder of , And then use the binary exchange rule
// The algorithm is the same as the previous decimal system
// return a Because a%b==0 however a and b Exchanged , So back a Value of equals before return b Value
}
边栏推荐
- 资源管理、高可用与自动化(下)
- 小程序image组件不显示图片?
- 多线程与高并发四:VarHandle与强软弱虚引用和ThreadLocal
- TypeScript 联合类型
- Sublime Text 3 基本配置教程
- How the uni app automatically switches the requested address according to the environment
- Go speed
- Summary of the use of composition API in the project
- Pycharm setting pseudo sublime color scheme
- 物体上下漂浮工具
猜你喜欢
随机推荐
密码加密md5和加盐处理
Database migration
Analysis of slow logs in MySQL and tidb of database Series
多线程与高并发六:线程池源码解析
音频 scipy 中 spectrogram 的运作机制
局域网内共享打印机的几种方式
Web APIs DOM-事件基础丨黑马程序员
Automatic backup of MySQL database
数字有为,易步到位 华为携“5极”明星产品加速布局商业市场
电学基础知识整理(一)
Li Kou daily question - day 29 -575 Divide candy
力扣每日一题-第29天-219.存在重复元素Ⅱ
Several ways of sharing printers in LAN
JVM一:JVM入门以及Class文件认识
数组的方法
vscode中出现无法在只读编辑器中编辑
大咖说·计算讲谈社|什么是东数西算要解决的核心问题?
Chapter IX app project test (3) test tools
Anaconda命令用法
组件拆分实战









