Advertisement
Nevtr4l

Factorización Prima (Fácil)

Mar 11th, 2025 (edited)
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using ll = long long;
  6.  
  7. vector<pair<ll, ll>> factorizacion(ll N) {
  8.   vector<pair<ll, ll>> factores;
  9.   if (N == 1) {
  10.     factores.push_back({ 1, 1 });
  11.     return factores;
  12.   }
  13.  
  14.   for (ll p = 2; p * p <= N; p++) {
  15.     if (N % p == 0) {
  16.       int exp = 0;
  17.       while (N % p == 0) {
  18.         exp++;
  19.         N /= p;
  20.       }
  21.       factores.push_back({ p, exp });
  22.     }
  23.   }
  24.   if (N != 1) factores.push_back({ N, 1 });
  25.  
  26.   return factores;
  27. }
  28.  
  29. int main() {
  30.   int t; cin >> t;
  31.   while (t--) {
  32.     ll N; cin >> N;
  33.     vector<pair<ll, ll>> factores = factorizacion(N);
  34.  
  35.     for (auto [p, exp] : factores) {
  36.       while (exp--) {
  37.         cout << p << ' ';
  38.       }
  39.     }
  40.     cout << '\n';
  41.   }
  42.  
  43.   return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement