// Lupus Nocawy 22 XI 2015, PA2016 // http://potyczki.mimuw.edu.pl/ // Runda 2 // Zadanie: JED // Jedynki [B] #include <cstdio> #include <iostream> #include <cassert> #include <cstring> #include <algorithm> #include <vector> #include <deque> #include <queue> #include <stack> #include <list> #include <set> #include <map> #include <cmath> using namespace std; #define REP(i,n) for(int i=0, _n=n; i<_n; ++i) #define FOR(i,a,b) for(int i=(a), _b=(b); i<=_b; ++i) #define FORD(i,a,b) for(int i=(a), _b=(b); i>=_b; --i) #define IT(i,x) __typeof__(x) i=x #define FOREACH(it,x) for(__typeof__((x).begin()) it=(x).begin(); it!=(x).end(); ++it) #define ALL(x) x.begin(),x.end() #define MP make_pair #define PB push_back #define DEBUG(x) if(debug) cout << x << endl; typedef long long int lli; typedef unsigned long long int llu; typedef pair<int,int> pii; const int debug=0; const int INF = 2147483647; const int max_k = 1000000000; // 10^9 // The 3,500th prime is 32,609, it's square is 1,063,346,881 > 10^9 const int max_prime = 32609; int sito[max_prime+1]; vector<int> prime; // 2, 3, 5, 7, 11, 13, ... void findPrimes(void){ prime.reserve(max_prime/10); // gestosc przy 10^6 to ok 7.8% i maleje prime.push_back(2); for(int i=3; i<=max_prime; i+=2){ if(sito[i]==0){ prime.push_back(i); for(int x=i*2; x<=max_prime; x+=i) sito[x]=i; } } } vector<int> factorize(int k) { vector<int> factors; int i=0; int p=prime[i]; while(p*p <= k){ while(k%p==0){ factors.push_back(p); k/=p; } p=prime[++i]; } if(factors.size()==0 || k>1) factors.push_back(k); return factors; } // int solutionLength[max_prime+1]; // string solutionString[max_prime+1]; // vector<int> factors[max_prime+1]; map<int,int> solutionLength; map<int,string> solutionString; map<int, vector<int> > factors; pair<int,string> find(int k, bool doRecurse){ // printf("find: %d\n", k); if(solutionLength.count(k)) return make_pair(solutionLength[k], solutionString[k]); int length=0; string str=""; if(factors[k].size()==0) factors[k] = factorize(k); REP(i,factors[k].size()) { int p = factors[k][i]; if(solutionLength.count(p)) { length += solutionLength[p]; if(str.length()==0) str = solutionString[p]; else str += "*" + solutionString[p]; } else { pair<int,string> S1 = find(p-1, 1); length += S1.first + 1; if(str.length()==0) str = "(" + S1.second + "+1)"; else str += "*(" + S1.second + "+1)"; } } solutionLength[k] = length; solutionString[k] = str; // printf("%d %s\n", length, str.c_str()); if(doRecurse) { find(k-1, 0); if (length > solutionLength[k-1]+1) { solutionLength[k] = solutionLength[k-1]+1; solutionString[k] = "("+solutionString[k-1]+"+1)"; } } return make_pair(solutionLength[k], solutionString[k]); } void solve(void){ int k; scanf("%d ", &k); if(k==1){ printf("1\n"); return; } pair<int,string> solution = find(k,1); printf("%s\n", solution.second.c_str()); // printf("%d %s\n", solution.first, solution.second.c_str()); // printf("%d =%s\n", solution.first, solution.second.c_str()); // printf("%d\n", solution.first); // printf("\n"); return; } int main(void){ int t; scanf("%d ", &t); findPrimes(); solutionLength[2] = 2; solutionString[2] = "(1+1)"; solutionLength[3] = 3; solutionString[3] = "(1+1+1)"; solutionLength[4] = 4; solutionString[4] = "(1+1+1+1)"; solutionLength[5] = 5; solutionString[5] = "(1+1+1+1+1)"; while(t--) solve(); 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | // Lupus Nocawy 22 XI 2015, PA2016 // http://potyczki.mimuw.edu.pl/ // Runda 2 // Zadanie: JED // Jedynki [B] #include <cstdio> #include <iostream> #include <cassert> #include <cstring> #include <algorithm> #include <vector> #include <deque> #include <queue> #include <stack> #include <list> #include <set> #include <map> #include <cmath> using namespace std; #define REP(i,n) for(int i=0, _n=n; i<_n; ++i) #define FOR(i,a,b) for(int i=(a), _b=(b); i<=_b; ++i) #define FORD(i,a,b) for(int i=(a), _b=(b); i>=_b; --i) #define IT(i,x) __typeof__(x) i=x #define FOREACH(it,x) for(__typeof__((x).begin()) it=(x).begin(); it!=(x).end(); ++it) #define ALL(x) x.begin(),x.end() #define MP make_pair #define PB push_back #define DEBUG(x) if(debug) cout << x << endl; typedef long long int lli; typedef unsigned long long int llu; typedef pair<int,int> pii; const int debug=0; const int INF = 2147483647; const int max_k = 1000000000; // 10^9 // The 3,500th prime is 32,609, it's square is 1,063,346,881 > 10^9 const int max_prime = 32609; int sito[max_prime+1]; vector<int> prime; // 2, 3, 5, 7, 11, 13, ... void findPrimes(void){ prime.reserve(max_prime/10); // gestosc przy 10^6 to ok 7.8% i maleje prime.push_back(2); for(int i=3; i<=max_prime; i+=2){ if(sito[i]==0){ prime.push_back(i); for(int x=i*2; x<=max_prime; x+=i) sito[x]=i; } } } vector<int> factorize(int k) { vector<int> factors; int i=0; int p=prime[i]; while(p*p <= k){ while(k%p==0){ factors.push_back(p); k/=p; } p=prime[++i]; } if(factors.size()==0 || k>1) factors.push_back(k); return factors; } // int solutionLength[max_prime+1]; // string solutionString[max_prime+1]; // vector<int> factors[max_prime+1]; map<int,int> solutionLength; map<int,string> solutionString; map<int, vector<int> > factors; pair<int,string> find(int k, bool doRecurse){ // printf("find: %d\n", k); if(solutionLength.count(k)) return make_pair(solutionLength[k], solutionString[k]); int length=0; string str=""; if(factors[k].size()==0) factors[k] = factorize(k); REP(i,factors[k].size()) { int p = factors[k][i]; if(solutionLength.count(p)) { length += solutionLength[p]; if(str.length()==0) str = solutionString[p]; else str += "*" + solutionString[p]; } else { pair<int,string> S1 = find(p-1, 1); length += S1.first + 1; if(str.length()==0) str = "(" + S1.second + "+1)"; else str += "*(" + S1.second + "+1)"; } } solutionLength[k] = length; solutionString[k] = str; // printf("%d %s\n", length, str.c_str()); if(doRecurse) { find(k-1, 0); if (length > solutionLength[k-1]+1) { solutionLength[k] = solutionLength[k-1]+1; solutionString[k] = "("+solutionString[k-1]+"+1)"; } } return make_pair(solutionLength[k], solutionString[k]); } void solve(void){ int k; scanf("%d ", &k); if(k==1){ printf("1\n"); return; } pair<int,string> solution = find(k,1); printf("%s\n", solution.second.c_str()); // printf("%d %s\n", solution.first, solution.second.c_str()); // printf("%d =%s\n", solution.first, solution.second.c_str()); // printf("%d\n", solution.first); // printf("\n"); return; } int main(void){ int t; scanf("%d ", &t); findPrimes(); solutionLength[2] = 2; solutionString[2] = "(1+1)"; solutionLength[3] = 3; solutionString[3] = "(1+1+1)"; solutionLength[4] = 4; solutionString[4] = "(1+1+1+1)"; solutionLength[5] = 5; solutionString[5] = "(1+1+1+1+1)"; while(t--) solve(); return 0; } |