fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef pair<int, int> ii;
  6.  
  7. const int INF = 1e9;
  8. const ll LINF = 1e18;
  9.  
  10. void factorize(ll n) {
  11. for (ll i = 2; i * i <= n; i++) {
  12. if (n % i == 0) {
  13. int e = 0;
  14. while (n % i == 0) n /= i, e++;
  15. cout << i << '^' << e << ' ';
  16. }
  17. }
  18. if (n > 1) cout << n << '^' << 1;
  19. cout << '\n';
  20. }
  21.  
  22. int main() {
  23. ios::sync_with_stdio(0); cin.tie(0);
  24. ll n;
  25. while (cin >> n) {
  26. if (n == 0) break;
  27. factorize(n);
  28. }
  29. }
Success #stdin #stdout 0.01s 5288KB
stdin
3111989
13091989
77145199750673
0
stdout
317^1 9817^1
17^2 89^1 509^1
328439^1 234884407^1