#include <cstdlib> #include <iostream> #include <cstring> #include <vector> #include <algorithm> #include <string> using namespace std; #define REP(i,n) for(int i=0; i<(n); i++) #define FOR(i,a,b) for(int i=(a); i<=(b); ++i) #define FORD(i,a,b) for(int i=(a); i>=(b); --i) #define FOREACH(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i) template<class T> inline int size(const T &c) { return c.size(); } //////////////////////////////////////////////////////////////////////////////// string calc(int n) { if(n==1) return ""; if(n==2) return "(1+1)"; if(n%2 == 1) { string s = calc((n-1)/2); return "((1+1)" + (s=="" ? "" : "*"+s) + "+1)"; } else { vector<int> factors; for(int i=2; i*i <= n; i++) { while(n%i == 0) { factors.push_back(i); n /= i; } } if(n > 1) factors.push_back(n); string res = calc(factors[0]); FOR(i,1,size(factors)-1) res += "*" + calc(factors[i]); return res; } } int main() { int t; cin >> t; while(t--) { int k; cin >> k; cout << (k==1 ? "1" : calc(k)) << 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 | #include <cstdlib> #include <iostream> #include <cstring> #include <vector> #include <algorithm> #include <string> using namespace std; #define REP(i,n) for(int i=0; i<(n); i++) #define FOR(i,a,b) for(int i=(a); i<=(b); ++i) #define FORD(i,a,b) for(int i=(a); i>=(b); --i) #define FOREACH(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i) template<class T> inline int size(const T &c) { return c.size(); } //////////////////////////////////////////////////////////////////////////////// string calc(int n) { if(n==1) return ""; if(n==2) return "(1+1)"; if(n%2 == 1) { string s = calc((n-1)/2); return "((1+1)" + (s=="" ? "" : "*"+s) + "+1)"; } else { vector<int> factors; for(int i=2; i*i <= n; i++) { while(n%i == 0) { factors.push_back(i); n /= i; } } if(n > 1) factors.push_back(n); string res = calc(factors[0]); FOR(i,1,size(factors)-1) res += "*" + calc(factors[i]); return res; } } int main() { int t; cin >> t; while(t--) { int k; cin >> k; cout << (k==1 ? "1" : calc(k)) << endl; } return 0; } |