stein算法求最大公约数
2013-11-04 21:44
190 查看
var
n,a,b,i:longint;
function gcd(a,b:longint):longint;
begin
if a=0 then exit(b);
if b=0 then exit(a);
if (a and 1=0) and (b and 1=0) then
exit(gcd(a shr 1,b shr 1) shl 1);
if (a and 1=0) then exit(gcd(a shr
1,b));
if (b and 1=0) then exit(gcd(a,b shr
1));
if (a and 1=1) and (b and 1=1) then
exit(gcd(abs(a-b) shr
1,a));{一定要加abs,否则会很惨,(-1)shr 1 是一个很大的数}
end;
begin
read(n);
for i:=1 to n do
begin
read(a,b);
writeln(gcd(a,b));
end;
end.
n,a,b,i:longint;
function gcd(a,b:longint):longint;
begin
if a=0 then exit(b);
if b=0 then exit(a);
if (a and 1=0) and (b and 1=0) then
exit(gcd(a shr 1,b shr 1) shl 1);
if (a and 1=0) then exit(gcd(a shr
1,b));
if (b and 1=0) then exit(gcd(a,b shr
1));
if (a and 1=1) and (b and 1=1) then
exit(gcd(abs(a-b) shr
1,a));{一定要加abs,否则会很惨,(-1)shr 1 是一个很大的数}
end;
begin
read(n);
for i:=1 to n do
begin
read(a,b);
writeln(gcd(a,b));
end;
end.
相关文章推荐
- Stein算法 求最大公约数
- HDU 2028 Lowest Common Multiple Plus (Stein算法 求最大公约数)
- 计算最大公约数的两种算法:辗转相除法和Stein算法
- 欧几里德算法的改进(求最大公约数Stein算法)
- 求最大公约数的Stein算法以及高精度实现
- Wikioi 天梯 最大公约数(1212)
- 计算最大公约数的两种算法
- 《数据结构与算法分析》笔记------欧几里得算法求最大公约数
- 两个数求最大公约数
- 升级版用迭代法求最大公约数
- 求最大公约数(递归)
- 1623. Sixth Grade Math 求最大公约数和最小公倍数的
- 计算“两个整数的最大公约数”
- 经典c程序(0014)---求最大公约数和最小公倍数
- 算法学习十六----求最大公约数
- C语言求最大公约数和最小公倍数
- 欧几里德算法(求两个数的最大公约数)
- 九度-1056-最大公约数
- 求两个数的最大公约数(位运算)
- /*分别求出俩个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果,俩个整数由键盘输入!?????怎么写,,求注释!??!*/