#include <cstdio> #include <sstream> #include <string> #include <algorithm> #include <vector> int getOnesCountForNumber(int number){ if (number == 1){ return 1; } else if (number == 2){ return 2; } else if (number == 3){ return 3; } else if (number % 3 == 0){ return getOnesCountForNumber(3) + getOnesCountForNumber(number/3); } else if (number % 2 == 0){ return getOnesCountForNumber(2) + getOnesCountForNumber(number/2); } else { return getOnesCountForNumber(number-1) + 1; } } std::string getExpressionForNumber(int number){ if (number == 1){ return "1"; } else if (number == 2){ return "(1+1)"; } else if (number == 3){ return "(1+1+1)"; } else if (number % 3 == 0){ std::stringstream ss; ss << "(" << getExpressionForNumber(3) << "*" << getExpressionForNumber(number/3) << ")"; return ss.str(); } else if (number % 2 == 0){ std::stringstream ss; ss << "(" << getExpressionForNumber(2) << "*" << getExpressionForNumber(number/2) << ")"; return ss.str(); } else { std::stringstream ss; ss << "(" << getExpressionForNumber(number-1) << "+1)"; return ss.str(); } } int main(){ int t; scanf("%d", &t); while (t--){ int n; scanf("%d", &n); std::string expr = getExpressionForNumber(n); int onesCount = std::count(expr.begin(), expr.end(), '1'); if (onesCount <= 100){ printf("%s\n", expr.c_str()); } else { printf("NIE\n"); } } return 0; } int mainTest(){ std::stringstream ss; for (int i = 0; i < 200; ++i){ ss << "1"; } std::string test = ss.str(); int onesCount = std::count(test.begin(), test.end(), '1'); printf("OnesCount = %d\n", onesCount); } int mainTestAll(){ for (int i=1; i < 1000000000; ++i){ if (i % 10000000 == 0){ printf("%d%% done\n", i/10000000); } int onesCount = getOnesCountForNumber(i); if (onesCount > 100){ printf("%d\n", i); } } }
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include <cstdio> #include <sstream> #include <string> #include <algorithm> #include <vector> int getOnesCountForNumber(int number){ if (number == 1){ return 1; } else if (number == 2){ return 2; } else if (number == 3){ return 3; } else if (number % 3 == 0){ return getOnesCountForNumber(3) + getOnesCountForNumber(number/3); } else if (number % 2 == 0){ return getOnesCountForNumber(2) + getOnesCountForNumber(number/2); } else { return getOnesCountForNumber(number-1) + 1; } } std::string getExpressionForNumber(int number){ if (number == 1){ return "1"; } else if (number == 2){ return "(1+1)"; } else if (number == 3){ return "(1+1+1)"; } else if (number % 3 == 0){ std::stringstream ss; ss << "(" << getExpressionForNumber(3) << "*" << getExpressionForNumber(number/3) << ")"; return ss.str(); } else if (number % 2 == 0){ std::stringstream ss; ss << "(" << getExpressionForNumber(2) << "*" << getExpressionForNumber(number/2) << ")"; return ss.str(); } else { std::stringstream ss; ss << "(" << getExpressionForNumber(number-1) << "+1)"; return ss.str(); } } int main(){ int t; scanf("%d", &t); while (t--){ int n; scanf("%d", &n); std::string expr = getExpressionForNumber(n); int onesCount = std::count(expr.begin(), expr.end(), '1'); if (onesCount <= 100){ printf("%s\n", expr.c_str()); } else { printf("NIE\n"); } } return 0; } int mainTest(){ std::stringstream ss; for (int i = 0; i < 200; ++i){ ss << "1"; } std::string test = ss.str(); int onesCount = std::count(test.begin(), test.end(), '1'); printf("OnesCount = %d\n", onesCount); } int mainTestAll(){ for (int i=1; i < 1000000000; ++i){ if (i % 10000000 == 0){ printf("%d%% done\n", i/10000000); } int onesCount = getOnesCountForNumber(i); if (onesCount > 100){ printf("%d\n", i); } } } |