#include <iostream> #include <vector> #include <fstream> #include <cmath> #include <string> void add_ones(int n, std::string & str) { if( str.size() > 0 ) str += '*'; str += '('; for(int a = 0 ; a < n ; ++a) { if( a > 0 ) str += '+'; str += '1'; } str += ')'; } void analyze(int k) { int n; int nmax = (int)sqrt(k) + 1; std::string res; int ones_size = 0; bool overflow = false; res.reserve(1000); if( k == 1 ) { std::cout << "1" << std::endl; return; } for(n = 2 ; n <= nmax && k > 1 && !overflow ; ++n) { if( n <= 100 ) { while( (k % n) == 0 ) { k = k / n; //std::cout << n << " "; add_ones(n, res); ones_size += n; if( ones_size > 100 ) overflow = true; } } else { overflow = true; } } if( !overflow && k > 1 ) { if( k > 100 ) { overflow = true; } else { //std::cout << k << " "; add_ones(k, res); ones_size += k; if( ones_size > 100 ) overflow = true; } } if( !overflow ) std::cout << res; else std::cout << "NIE"; std::cout << std::endl; } void read(std::istream & str) { int t; int k; str >> t; for(int i=0 ; i<t ; ++i) { str >> k; analyze(k); } } int main() { read(std::cin); }
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | #include <iostream> #include <vector> #include <fstream> #include <cmath> #include <string> void add_ones(int n, std::string & str) { if( str.size() > 0 ) str += '*'; str += '('; for(int a = 0 ; a < n ; ++a) { if( a > 0 ) str += '+'; str += '1'; } str += ')'; } void analyze(int k) { int n; int nmax = (int)sqrt(k) + 1; std::string res; int ones_size = 0; bool overflow = false; res.reserve(1000); if( k == 1 ) { std::cout << "1" << std::endl; return; } for(n = 2 ; n <= nmax && k > 1 && !overflow ; ++n) { if( n <= 100 ) { while( (k % n) == 0 ) { k = k / n; //std::cout << n << " "; add_ones(n, res); ones_size += n; if( ones_size > 100 ) overflow = true; } } else { overflow = true; } } if( !overflow && k > 1 ) { if( k > 100 ) { overflow = true; } else { //std::cout << k << " "; add_ones(k, res); ones_size += k; if( ones_size > 100 ) overflow = true; } } if( !overflow ) std::cout << res; else std::cout << "NIE"; std::cout << std::endl; } void read(std::istream & str) { int t; int k; str >> t; for(int i=0 ; i<t ; ++i) { str >> k; analyze(k); } } int main() { read(std::cin); } |