#include <iostream>
#include <vector>
using namespace std;
// Funcție pentru descompunerea unui număr în factori primi
vector<pair<int, int>> descompuneInFactoriPrimi(int n) {
vector<pair<int, int>> factori;
int divisor = 2;
while (n > 1) {
int count = 0;
while (n % divisor == 0) {
n /= divisor;
count++;
}
if (count > 0) {
factori.push_back({divisor, count});
}
divisor++;
// Optimizare: verificăm doar până la √n
if (divisor * divisor > n) {
if (n > 1) {
factori.push_back({n, 1});
}
break;
}
}
return factori;
}
int main() {
int numar;
cout << "Introduceti un numar: ";
cin >> numar;
if (numar <= 1) {
cout << "Numarul trebuie sa fie mai mare decat 1!" << endl;
return 0;
}
auto factori = descompuneInFactoriPrimi(numar);
cout << "Descompunerea in factori primi este: ";
for (size_t i = 0; i < factori.size(); ++i) {
cout << factori[i].first << "^" << factori[i].second;
if (i < factori.size() - 1) {
cout << " * ";
}
}
cout << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gRnVuY8ibaWUgcGVudHJ1IGRlc2NvbXB1bmVyZWEgdW51aSBudW3Eg3Igw65uIGZhY3RvcmkgcHJpbWkKdmVjdG9yPHBhaXI8aW50LCBpbnQ+PiBkZXNjb21wdW5lSW5GYWN0b3JpUHJpbWkoaW50IG4pIHsKCiAgICB2ZWN0b3I8cGFpcjxpbnQsIGludD4+IGZhY3Rvcmk7CiAgICBpbnQgZGl2aXNvciA9IDI7CgogICAgd2hpbGUgKG4gPiAxKSB7CiAgICAgICAgaW50IGNvdW50ID0gMDsKICAgICAgICB3aGlsZSAobiAlIGRpdmlzb3IgPT0gMCkgewogICAgICAgICAgICBuIC89IGRpdmlzb3I7CiAgICAgICAgICAgIGNvdW50Kys7CiAgICAgICAgfQogICAgICAgIGlmIChjb3VudCA+IDApIHsKICAgICAgICAgICAgZmFjdG9yaS5wdXNoX2JhY2soe2Rpdmlzb3IsIGNvdW50fSk7CiAgICAgICAgfQogICAgICAgIGRpdmlzb3IrKzsKICAgICAgICAvLyBPcHRpbWl6YXJlOiB2ZXJpZmljxINtIGRvYXIgcMOibsSDIGxhIOKImm4KICAgICAgICBpZiAoZGl2aXNvciAqIGRpdmlzb3IgPiBuKSB7CiAgICAgICAgICAgIGlmIChuID4gMSkgewogICAgICAgICAgICAgICAgZmFjdG9yaS5wdXNoX2JhY2soe24sIDF9KTsKICAgICAgICAgICAgfQogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gZmFjdG9yaTsKfQoKaW50IG1haW4oKSB7CiAgICBpbnQgbnVtYXI7CiAgICBjb3V0IDw8ICJJbnRyb2R1Y2V0aSB1biBudW1hcjogIjsKICAgIGNpbiA+PiBudW1hcjsKCiAgICBpZiAobnVtYXIgPD0gMSkgewogICAgICAgIGNvdXQgPDwgIk51bWFydWwgdHJlYnVpZSBzYSBmaWUgbWFpIG1hcmUgZGVjYXQgMSEiIDw8IGVuZGw7CiAgICAgICAgcmV0dXJuIDA7CiAgICB9CgogICAgYXV0byBmYWN0b3JpID0gZGVzY29tcHVuZUluRmFjdG9yaVByaW1pKG51bWFyKTsKCiAgICBjb3V0IDw8ICJEZXNjb21wdW5lcmVhIGluIGZhY3RvcmkgcHJpbWkgZXN0ZTogIjsKICAgIGZvciAoc2l6ZV90IGkgPSAwOyBpIDwgZmFjdG9yaS5zaXplKCk7ICsraSkgewogICAgICAgIGNvdXQgPDwgZmFjdG9yaVtpXS5maXJzdCA8PCAiXiIgPDwgZmFjdG9yaVtpXS5zZWNvbmQ7CiAgICAgICAgaWYgKGkgPCBmYWN0b3JpLnNpemUoKSAtIDEpIHsKICAgICAgICAgICAgY291dCA8PCAiICogIjsKICAgICAgICB9CiAgICB9CiAgICBjb3V0IDw8IGVuZGw7CgogICAgcmV0dXJuIDA7Cn0=