pallord-rho(质因子分解)

要期末考试了,即有millerRabin又有pollard-rho一下子说不清。

先把代码贴这里考完以后再来补充!


poj1811

#include<iostream> using namespace std; #include <ctime> #include<cstdlib> typedef __int64 LL ;//__int64 , long long LL multiMod ( LL a , LL b , LL n ) {//s=(a*b)(mod n) (n<2^62) // return ( (a%m) * (b%m) ) % m; // this will overflow! a %= n ; b %= n ; LL s = 0 ; while( b ) { if( b & 1 ) { s += a ; if( s >= n ) s -= n ; } a <<= 1 ; b >>= 1 ;//a*b=(a*2)*(b/2) if(a >= n ) a -= n ; } return s ; } LL powerMod ( LL s , LL u , LL n ) { s %= n ; LL tmp = 1 ; while ( u ) { if ( u & 1 ) tmp = multiMod ( tmp , s , n ) ; s = multiMod ( s , s , n ) ; u >>= 1 ; } return tmp ; } bool witness ( LL s , LL n ) { LL u = n - 1 ; int t = 0 ; while ( ( u & 1 ) == 0 ) u >>= 1 , t ++ ; LL x = powerMod ( s , u , n ) ; while ( t -- ) { LL tmp = x ; x = multiMod ( x , x , n ) ; if ( x == 1 ) { if ( tmp == n - 1 || tmp == 1 ) return false ;//may be prime else return true ;//composite } } return true ; //composite } bool millerRabin ( LL n , const int times = 3 ) { if ( n == 2 ) return true ; if ( ( n & 1 ) == 0 || n < 2 ) return false ; int i = times ; while ( i -- ) { LL s = rand ( ) % ( n - 1 ) + 1 ; if ( witness ( s , n ) ) return false ; } return true ; } LL gcd ( LL a , LL b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } LL pollard_rho ( LL n ) { LL x , y , k , d ; x = y = rand ( ) % n ; k = 2 ; int i = 1 ; int c =rand ( ) % n ; //Õâ¸ö²»ÐС£¡£¡£ /* do { c = rand ( ) % ( n - 1 ) + 1 ; } while ( c == 2 ) ; */ while ( true ) { i ++ ; x = ( multiMod ( x , x , n ) + c ) % n ; if ( y == x ) return 1 ;//restart else if ( y > x ) d = gcd ( y - x , n ) ; else d = gcd ( x - y , n ) ; if ( d != 1 && d != n - 1 ) return d ; else { if ( i == k ) { y = x ; k <<= 1 ; } } } } LL prime [ 54 ] ; int cnt ; void split ( LL n ) {//n != 1 if ( millerRabin ( n ) ) prime [ cnt ++ ] = n ; else { LL p ; do { p = pollard_rho ( n ) ; }while ( p == n || p == 1 ); split ( p ) ; split ( n / p ) ; } } int main ( ) { srand ( ( unsigned ) time ( NULL ) ) ; int t ; LL n ; cin >> t ; while ( t -- ) { scanf ( "%I64d" , & n ) ; if ( millerRabin ( n ) ) cout << "Prime" << endl ; else { cnt = 0 ; split ( n ) ; int i , mini = 0 ;// for ( i = 1 ; i < cnt ; i ++ ) if ( prime [ mini ] > prime [ i ] ) mini = i ; printf ( "%I64d/n" , prime [ mini ] ) ; } } return 0 ; } /* 13 2 10 12 2717 35 36 17 2717 16 81 32 105 85 */

本人也可参考网上代码写的,我搜了很多,其实大致都差不多,囧!,不知道最初是哪位大牛所写。

你可能感兴趣的:(c,null,n2)