Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef unsigned long long ull;
- ull fastPow(ull base, ull exp, ull mod){
- base %= mod;
- //exp %= phi(mod) if base and mod are relatively prime
- ull ans = 1LL;
- while (exp > 0){
- if (exp & 1LL)
- ans = (ans * (__int128_t)base) % mod;
- base = (base * (__int128_t)base) % mod;
- exp >>= 1;
- }
- return ans;
- }
- bool checkComposite(ull n, ull a, ull d, int s){
- ull x = fastPow(a, d, n);
- if (x == 1 or x == n - 1)
- return false;
- for (int r = 1; r < s; r++){
- x = (x * (__int128_t)x) % n;
- if (x == n - 1LL)
- return false;
- }
- return true;
- };
- bool millerRabin(ull n){
- if (n < 2)
- return false;
- int r = 0;
- ull d = n - 1LL;
- while ((d & 1LL) == 0){
- d >>= 1;
- r++;
- }
- for (ull a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}){
- if (n == a)
- return true;
- if (checkComposite(n, a, d, r))
- return false;
- }
- return true;
- }
- ull modMul(ull a, ull b, ull mod){
- return (a*(__uint128_t)b)%mod;
- }
- ull f(ull x, ull mod){
- return (modMul(x, x, mod) + 1)%mod;
- }
- ull pollard(ull n) {
- auto f = [n](ull x){ return modMul(x, x, n) + 1; };
- ull x = 0, y = 0, t = 0, prd = 2, i = 1, q;
- while (t++ % 40 || __gcd(prd, n) == 1) {
- if (x == y) x = ++i, y = f(x);
- if ((q = modMul(prd, max(x,y) - min(x,y), n))) prd = q;
- x = f(x), y = f(f(y));
- }
- return __gcd(prd, n);
- }
- vector<ull> factor(ull n) {
- if (n == 1) return {};
- if (millerRabin(n)) return {n};
- ull x = pollard(n);
- auto l = factor(x), r = factor(n / x);
- l.insert(l.end(), r.begin(), r.end());
- return l;
- }
- int main() {
- ios_base::sync_with_stdio(false); cin.tie(NULL);
- ull n;
- while(true){
- cin >> n;
- if(n == 0)
- break;
- auto v = factor(n);
- map<ull, int> mp;
- for(ull x: v)
- mp[x]++;
- bool can = false;
- for(auto p : mp){
- if(can)
- cout << " ";
- cout << p.first << "^" << p.second;
- can = true;
- }
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement