fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4.  
  5. const int MAXN = 1e6;
  6. vector<int> spf(MAXN + 1, 0);
  7.  
  8. void computeSpf(){
  9. for (int i = 2; i <= MAXN; i++){
  10. spf[i] = i;
  11. }
  12.  
  13. // starting the sieve process
  14.  
  15. for (int i = 2; i * i <= MAXN; i++){
  16. if(spf[i] == i){
  17. for (int j = i*i; j <= MAXN; j += i){
  18. if(spf[j] == j){
  19. spf[j] = i;
  20. }
  21. }
  22. }
  23. }
  24.  
  25. }
  26.  
  27. unordered_map<int, int> calFactors(int n){
  28. unordered_map<int, int> mp;
  29.  
  30. while(n != 1){
  31. int d = spf[n];
  32. mp[d]++;
  33. n /= d;
  34. }
  35.  
  36. return mp;
  37. }
  38.  
  39.  
  40. signed main() {
  41. ios_base::sync_with_stdio(0);
  42. cin.tie(0); cout.tie(0);
  43.  
  44. // int t; cin >> t;
  45. // while (t--) {
  46. // solve();
  47. // }
  48. computeSpf();
  49. int n; cin >> n;
  50. vector<int> input(n);
  51.  
  52. for (int i = 0; i < n; i ++){
  53. cin >> input[i];
  54.  
  55. unordered_map<int, int> factors = calFactors(input[i]);
  56.  
  57. cout << "power of factors of " << input[i] << " are: " << '\n';
  58. for (auto i : factors){
  59. cout << i.first << " " << i.second << '\n';
  60. }
  61. cout << '\n';
  62. }
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0.02s 11104KB
stdin
5
45 89 34 2 79
stdout
power of factors of 45 are: 
5 1
3 2

power of factors of 89 are: 
89 1

power of factors of 34 are: 
17 1
2 1

power of factors of 2 are: 
2 1

power of factors of 79 are: 
79 1