#include <iostream> #include <vector> using namespace std; const int PRIMES = 25; const int POSSIBLEONES = 100; int primes[PRIMES] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61 ,67,71,73,79,83,89,97 }; void printNumber(const int &number) { if (number == 1) { cout << "1"; return; } cout << "("; for (int i = 0; i < number; ++i) { if (i > 0) { cout << "+"; } cout << "1"; } cout << ")"; } void algo() { int k; cin >> k; vector<int> primeDivisors; int sumDivisors = 0; for (int i = 0; i < PRIMES; ++i) { if (k == 1 || POSSIBLEONES < sumDivisors) { break; } while (k % primes[i] == 0) { k /= primes[i]; primeDivisors.push_back(primes[i]); sumDivisors += primes[i]; } } if (POSSIBLEONES < sumDivisors) { cout << "NIE\n"; } else if (k != 1) { cout << "NIE\n"; } else { if (primeDivisors.size() == 0) { cout << "1"; } for (auto i = 0; i < primeDivisors.size(); ++i) { if (i > 0) { cout << "*"; } printNumber(primeDivisors[i]); } cout << "\n"; } } int main() { int t; cin >> t; while (t--) { algo(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <iostream> #include <vector> using namespace std; const int PRIMES = 25; const int POSSIBLEONES = 100; int primes[PRIMES] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61 ,67,71,73,79,83,89,97 }; void printNumber(const int &number) { if (number == 1) { cout << "1"; return; } cout << "("; for (int i = 0; i < number; ++i) { if (i > 0) { cout << "+"; } cout << "1"; } cout << ")"; } void algo() { int k; cin >> k; vector<int> primeDivisors; int sumDivisors = 0; for (int i = 0; i < PRIMES; ++i) { if (k == 1 || POSSIBLEONES < sumDivisors) { break; } while (k % primes[i] == 0) { k /= primes[i]; primeDivisors.push_back(primes[i]); sumDivisors += primes[i]; } } if (POSSIBLEONES < sumDivisors) { cout << "NIE\n"; } else if (k != 1) { cout << "NIE\n"; } else { if (primeDivisors.size() == 0) { cout << "1"; } for (auto i = 0; i < primeDivisors.size(); ++i) { if (i > 0) { cout << "*"; } printNumber(primeDivisors[i]); } cout << "\n"; } } int main() { int t; cin >> t; while (t--) { algo(); } } |