// Grzegorz Bukowiec // PA 2016 - Zadanie 2B - Jedynki #include <iostream> #include <string> using namespace std; string makeNumber(int n) { string number = "(1"; for (int i = 1; i < n; ++i) { number += "+1"; } number += ")"; return number; } void multiply(string* a, int b) { if ((*a) != "") { (*a) += "*"; } (*a) += makeNumber(b); } int main() { ios_base::sync_with_stdio(0); int t, n, sum; string expression; cin >> t; while (t--) { cin >> n; sum = 0; expression = ""; if (n == 1) { cout << "1" << endl; continue; } for (int i = 2; i <= 100 && i <= n; ++i) { while (n % i == 0) { n /= i; sum += i; if (sum > 100) { break; } multiply(&expression, i); } if (sum > 100) { break; } } if (sum > 100 || n > 1) { cout << "NIE" << endl; } else { cout << expression << endl;; } } return 0; }
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 | // Grzegorz Bukowiec // PA 2016 - Zadanie 2B - Jedynki #include <iostream> #include <string> using namespace std; string makeNumber(int n) { string number = "(1"; for (int i = 1; i < n; ++i) { number += "+1"; } number += ")"; return number; } void multiply(string* a, int b) { if ((*a) != "") { (*a) += "*"; } (*a) += makeNumber(b); } int main() { ios_base::sync_with_stdio(0); int t, n, sum; string expression; cin >> t; while (t--) { cin >> n; sum = 0; expression = ""; if (n == 1) { cout << "1" << endl; continue; } for (int i = 2; i <= 100 && i <= n; ++i) { while (n % i == 0) { n /= i; sum += i; if (sum > 100) { break; } multiply(&expression, i); } if (sum > 100) { break; } } if (sum > 100 || n > 1) { cout << "NIE" << endl; } else { cout << expression << endl;; } } return 0; } |