fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int ll ;
  4. // A function to print all prime
  5. // factors of a given number n
  6.  
  7. unordered_map<ll,ll> primeFactors(ll n){
  8. unordered_map <ll,ll> a2 ;
  9. // Print the number of 2s that divide n
  10. while (n % 2 == 0)
  11. {
  12. a2[2]++;
  13. n = n/2;
  14. }
  15.  
  16. for (ll i = 3; i <= sqrt(n); i = i + 2)
  17. {
  18. // While i divides n, print i and divide n
  19. while (n % i == 0)
  20. {
  21. a2[i]++;
  22. n = n/i;
  23. }
  24. }
  25.  
  26. // This condition is to handle the case when n
  27. // is a prime number greater than 2
  28. if (n > 2)
  29. a2[n]++;
  30.  
  31. return a2;
  32. }
  33.  
  34. /* Driver code */
  35. int main()
  36. {
  37. ll n;
  38. cin>>n;
  39. unordered_map <ll,ll> a2 = primeFactors(n);
  40. for(auto itr=a2.begin();itr!=a2.end();++itr){
  41. cout<<itr->first<<" "<<itr->second;
  42. cout<<"\n";
  43. }
  44. return 0;
  45. }
  46.  
  47. // This is code is contributed by rathbhupendra
  48.  
Success #stdin #stdout 0s 5320KB
stdin
99
stdout
11 1
3 2