1 #include<iostream> 2 #include<ctime> 3 #include<algorithm> 4 #include<map> 5 using namespace std; 6 typedef long long ll; 7 map<ll, int>m; 8 const int mod = 10000019; 9 const int times = 50;//测试50次 10 ll mul(ll a, ll b, ll m) 11 //求a*b%m 12 { 13 ll ans = 0; 14 a %= m; 15 while(b) 16 { 17 if(b & 1)ans = (ans + a) % m; 18 b /= 2; 19 a = (a + a) % m; 20 } 21 return ans; 22 } 23 ll pow(ll a, ll b, ll m) 24 //a^b % m 25 { 26 ll ans = 1; 27 a %= m; 28 while(b) 29 { 30 if(b & 1)ans = mul(a, ans, m); 31 b /= 2; 32 a = mul(a, a, m); 33 } 34 ans %= m; 35 return ans; 36 } 37 bool Miller_Rabin(ll n, int repeat)//n是测试的大数,repeat是测试重复次数 38 { 39 if(n == 2 || n == 3)return true;//特判 40 if(n % 2 == 0 || n == 1)return false;//偶数和1 41 42 //将n-1分解成2^s*d 43 ll d = n - 1; 44 int s = 0; 45 while(!(d & 1)) ++s, d >>= 1; 46 //srand((unsigned)time(NULL));在最开始调用即可 47 for(int i = 0; i < repeat; i++)//重复repeat次 48 { 49 ll a = rand() % (n - 3) + 2;//取一个随机数,[2,n-1) 50 ll x = pow(a, d, n); 51 ll y = 0; 52 for(int j = 0; j < s; j++) 53 { 54 y = mul(x, x, n); 55 if(y == 1 && x != 1 && x != (n - 1))return false; 56 x = y; 57 } 58 if(y != 1)return false;//费马小定理 59 } 60 return true; 61 } 62 ll gcd(ll a, ll b) 63 { 64 return b == 0 ? a : gcd(b, a % b); 65 } 66 ll pollard_rho(ll n, ll c)//找到n的一个因子 67 { 68 ll x = rand() % (n - 2) + 1; 69 ll y = x, i = 1, k = 2; 70 while(1) 71 { 72 i++; 73 x = (mul(x, x, n) + c) + n;//不断调整x2 74 ll d = gcd(y - x, n); 75 if(1 < d && d < n) 76 return d;//找到因子 77 if(y == x) 78 return n;//找到循环,返回n,重新来 79 if(i == k)//一个优化 80 { 81 y = x; 82 k <<= 1; 83 } 84 } 85 } 86 void Find(ll n, ll c) 87 { 88 if(n == 1)return;//递归出口 89 90 if(Miller_Rabin(n, times))//如果是素数,就加入 91 { 92 m[n]++; 93 return; 94 } 95 96 ll p = n; 97 while(p >= n) 98 p = pollard_rho(p, c--);//不断找因子,知道找到为止,返回n说明没找到 99 100 Find(p, c); 101 Find(n / p, c); 102 } 103 int main() 104 { 105 ll n;srand((unsigned)time(NULL)); 106 while(cin >> n) 107 { 108 m.clear(); 110 Find(n, rand() % (n - 1) + 1);//这是自己设置的一个数 111 cout<<n<<" = "; 112 for(map<ll ,int>::iterator it = m.begin(); it != m.end();) 113 { 114 cout<<it->first<<" ^ "<<it->second; 115 if((++it) != m.end()) 116 cout<<" * "; 117 } 118 cout<<endl; 119 } 120 return 0; 121 }