#include<iostream> #include<vector> #include<string> using namespace std; vector<int> GenerateFactors(long long x){ int j = 2; vector<int> vFactors; do{ if(x%j == 0){ vFactors = GenerateFactors(x/j); vFactors.insert(vFactors.begin(), j); return vFactors; } j++; }while(j<(x/2)); vFactors.insert(vFactors.begin(), x); if(vFactors.back() == 1){ vFactors.pop_back(); } return vFactors; } string to5(int n){ if(n>5 || n<2) return ""; string result = "("; for(int i=1;i<n;++i){ result+="1+"; } result+="1)"; return result; } string calc(long long n){ if(n<=5){ return to5(n); } vector<int> fact = GenerateFactors(n); string result = "("; if(fact.size() == 1){ fact = GenerateFactors(n-1); result += "1+"; } for(int i=0;i<fact.size()-1;i++){ if(fact[i] == 1) continue; result+=calc(fact[i])+"*"; } int size = fact.size(); result+=calc(fact[size-1])+")"; return result; } int main(){ long long n; int tests; cin>>tests; for(int test=0;test<tests;test++){ cin>>n; if(n == 1){ cout<<"1"<<endl; continue; } string result = calc(n); int count = 0; for(auto &znak : result){ if(znak == '1') count ++; } if(count>100){ cout<<"NIE"<<endl; } else{ cout<<result<<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 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 | #include<iostream> #include<vector> #include<string> using namespace std; vector<int> GenerateFactors(long long x){ int j = 2; vector<int> vFactors; do{ if(x%j == 0){ vFactors = GenerateFactors(x/j); vFactors.insert(vFactors.begin(), j); return vFactors; } j++; }while(j<(x/2)); vFactors.insert(vFactors.begin(), x); if(vFactors.back() == 1){ vFactors.pop_back(); } return vFactors; } string to5(int n){ if(n>5 || n<2) return ""; string result = "("; for(int i=1;i<n;++i){ result+="1+"; } result+="1)"; return result; } string calc(long long n){ if(n<=5){ return to5(n); } vector<int> fact = GenerateFactors(n); string result = "("; if(fact.size() == 1){ fact = GenerateFactors(n-1); result += "1+"; } for(int i=0;i<fact.size()-1;i++){ if(fact[i] == 1) continue; result+=calc(fact[i])+"*"; } int size = fact.size(); result+=calc(fact[size-1])+")"; return result; } int main(){ long long n; int tests; cin>>tests; for(int test=0;test<tests;test++){ cin>>n; if(n == 1){ cout<<"1"<<endl; continue; } string result = calc(n); int count = 0; for(auto &znak : result){ if(znak == '1') count ++; } if(count>100){ cout<<"NIE"<<endl; } else{ cout<<result<<endl; } } return 0; } |