二进制GCD

int gcd( int a, int b ){
    if( a == 0 ) return b;
    if( b == 0 ) return a;
    if( a%2 == 0 && b%2 == 0 ) return 2*gcd( a/2, b/2 );
    else if( a%2 == 0 )        return gcd( a/2, b );
    else if( b%2 == 0 )        return gcd( a, b/2 );
    else                       return gcd( abs(a-b), min(a,b) );
}

 

posted @ 2013-04-07 13:18  yefeng1627  阅读(113)  评论(0编辑  收藏  举报

Launch CodeCogs Equation Editor