// scroll down until '// code starts here' #include <bits/stdc++.h> using namespace std; const int inf = 1000000001; #define REP(i, n) for (int i = 0; i < (n); ++i) #define EACH(it, cont) for (auto &it : cont) #define FOR(i, b, e) for (int i = (b); i <= (e); ++i) #define FORD(i, b, e) for (int i = (b); i >= (e); --i) #define FORQ(x, Q) for (decltype((Q).front()) x; !(Q).empty() && (x = (Q).front(), (Q).pop(), true); ) #define dump fancy_dumper::DumpFirst<&std::cerr>(), #define say fancy_dumper::DumpFirst<&std::cout>(), #define $dump fancy_dumper::Dumper<&std::cerr> #define $say fancy_dumper::Dumper<&std::cout> #define $(x) #x, fancy_dumper::ToggleSeparator(), (x),\ fancy_dumper::ToggleSeparator() namespace fancy_dumper { struct ToggleSeparator { }; template<ostream *Out> struct DumpFirst { }; template<ostream *Out> struct Dumper { static void *&last() { static void *p; return p; } static const char *&separator() { static const char *s = " "; return s; } static const char *&separatorNewLine() { static const char *s = "\n"; return s; } static const char *&unseparator() { static const char *s = "="; return s; } Dumper() { last() = this; } ~Dumper() { if (this == last()) { *Out << separatorNewLine(); last() = 0; } } Dumper operator, (ToggleSeparator) { std::swap(separator(), unseparator()); return Dumper(); } }; template<ostream *Out, class T> Dumper<Out> operator,(Dumper<Out>, const T &e) { *Out << Dumper<Out>::separator() << e; return Dumper<Out>(); } template<ostream *Out, class T> Dumper<Out> operator, (DumpFirst<Out>, const T &e) { *Out << e; return Dumper<Out>(); } } template<class T, class Y> ostream &operator<< (ostream &o, const pair<T, Y> &p) { return o << p.first << ' ' << p.second; } template<class T> ostream &operator<< (ostream &o, const vector<T> &v) { bool isBegin = true; EACH (e, v) { if (!isBegin) { o << ' '; } isBegin = false; o << e; } return o; } template<class T> ostream &operator<< (ostream &o, const set<T> &v) { bool isBegin = true; EACH (e, v) { if (!isBegin) { o << ' '; } isBegin = false; o << e; } return o; } template<class T> ostream &operator,(ostream &o, const T &t) { return o << t; } struct StdinReader { StdinReader() { ios::sync_with_stdio(0); } } stdinReader; #define get stdinReader, template<class T> StdinReader &operator, (StdinReader &sr, T &t) { cin >> t; return sr; } template<class T> StdinReader &operator, (StdinReader &sr, vector<T> &v) { EACH (x, v) sr, x; return sr; } template<class T, class U> StdinReader &operator, (StdinReader &sr, pair<T, U> &p) { return sr, p.first, p.second; } template <class T, class U> bool remin(T &a, const U &b) { return b < a ? a = b, true : false; } template <class T, class U> bool remax(T &a, const U &b) { return b > a ? a = b, true : false; } template <class T> int size(const T &t) { return (int) t.size(); } // code starts here int count1(const string &s) { return count(s.begin(), s.end(), '1'); } const int M = 15; const int N = 1 << M; int D[N]; int Primes[3513]; // raw array, because used in tigth loop void calcPrimes(int n = N) { for (int i = 2; i * i < n; ++i) { if (D[i]) { continue; } for (int j = i * i; j < n; j += i) { D[j] = i; } } int *out = Primes; FOR (i, 2, n-1) { if (!D[i]) { *out++ = i; } } *out = N+1; } string F[N+1]; int cnt[N+1]; void prep() { calcPrimes(); F[0] = "0"; F[1] = "1"; cnt[1] = 1; FOR (n, 2, N) { cnt[n] = cnt[n-1] + 1; int bestd = 0; for (int i = 0; Primes[i] < n; ++i) { const int d = Primes[i]; if (n % d == 0) { if (remin(cnt[n], cnt[d]+cnt[n/d])) { bestd = d; } } } if (!bestd) { F[n] = "("+F[n-1]+"+1)"; } else { F[n] = F[bestd]+"*"+F[n/bestd]; } } } string f(int n) { if (n <= N) { return F[n]; } // works for any N, eg for N=1024 we get n = x + 1024 * (y + 1024 * z), but this could yield up to 106 ones // with N == 2^15, recursion here is one step flatter, we get n = x + 2^15 * y auto s = f(n >> M); const auto x = n & (N-1); if (s == "1") { s = F[N]; } else { s += "*"+F[N]; } if (x) { s = "("+s+"+"+F[x]+")"; // parens necessary for M<15 } return s; } int main() { prep(); int tests; get tests; FOR (tt, 1, tests) { int x; get x; say f(x); } }
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | // scroll down until '// code starts here' #include <bits/stdc++.h> using namespace std; const int inf = 1000000001; #define REP(i, n) for (int i = 0; i < (n); ++i) #define EACH(it, cont) for (auto &it : cont) #define FOR(i, b, e) for (int i = (b); i <= (e); ++i) #define FORD(i, b, e) for (int i = (b); i >= (e); --i) #define FORQ(x, Q) for (decltype((Q).front()) x; !(Q).empty() && (x = (Q).front(), (Q).pop(), true); ) #define dump fancy_dumper::DumpFirst<&std::cerr>(), #define say fancy_dumper::DumpFirst<&std::cout>(), #define $dump fancy_dumper::Dumper<&std::cerr> #define $say fancy_dumper::Dumper<&std::cout> #define $(x) #x, fancy_dumper::ToggleSeparator(), (x),\ fancy_dumper::ToggleSeparator() namespace fancy_dumper { struct ToggleSeparator { }; template<ostream *Out> struct DumpFirst { }; template<ostream *Out> struct Dumper { static void *&last() { static void *p; return p; } static const char *&separator() { static const char *s = " "; return s; } static const char *&separatorNewLine() { static const char *s = "\n"; return s; } static const char *&unseparator() { static const char *s = "="; return s; } Dumper() { last() = this; } ~Dumper() { if (this == last()) { *Out << separatorNewLine(); last() = 0; } } Dumper operator, (ToggleSeparator) { std::swap(separator(), unseparator()); return Dumper(); } }; template<ostream *Out, class T> Dumper<Out> operator,(Dumper<Out>, const T &e) { *Out << Dumper<Out>::separator() << e; return Dumper<Out>(); } template<ostream *Out, class T> Dumper<Out> operator, (DumpFirst<Out>, const T &e) { *Out << e; return Dumper<Out>(); } } template<class T, class Y> ostream &operator<< (ostream &o, const pair<T, Y> &p) { return o << p.first << ' ' << p.second; } template<class T> ostream &operator<< (ostream &o, const vector<T> &v) { bool isBegin = true; EACH (e, v) { if (!isBegin) { o << ' '; } isBegin = false; o << e; } return o; } template<class T> ostream &operator<< (ostream &o, const set<T> &v) { bool isBegin = true; EACH (e, v) { if (!isBegin) { o << ' '; } isBegin = false; o << e; } return o; } template<class T> ostream &operator,(ostream &o, const T &t) { return o << t; } struct StdinReader { StdinReader() { ios::sync_with_stdio(0); } } stdinReader; #define get stdinReader, template<class T> StdinReader &operator, (StdinReader &sr, T &t) { cin >> t; return sr; } template<class T> StdinReader &operator, (StdinReader &sr, vector<T> &v) { EACH (x, v) sr, x; return sr; } template<class T, class U> StdinReader &operator, (StdinReader &sr, pair<T, U> &p) { return sr, p.first, p.second; } template <class T, class U> bool remin(T &a, const U &b) { return b < a ? a = b, true : false; } template <class T, class U> bool remax(T &a, const U &b) { return b > a ? a = b, true : false; } template <class T> int size(const T &t) { return (int) t.size(); } // code starts here int count1(const string &s) { return count(s.begin(), s.end(), '1'); } const int M = 15; const int N = 1 << M; int D[N]; int Primes[3513]; // raw array, because used in tigth loop void calcPrimes(int n = N) { for (int i = 2; i * i < n; ++i) { if (D[i]) { continue; } for (int j = i * i; j < n; j += i) { D[j] = i; } } int *out = Primes; FOR (i, 2, n-1) { if (!D[i]) { *out++ = i; } } *out = N+1; } string F[N+1]; int cnt[N+1]; void prep() { calcPrimes(); F[0] = "0"; F[1] = "1"; cnt[1] = 1; FOR (n, 2, N) { cnt[n] = cnt[n-1] + 1; int bestd = 0; for (int i = 0; Primes[i] < n; ++i) { const int d = Primes[i]; if (n % d == 0) { if (remin(cnt[n], cnt[d]+cnt[n/d])) { bestd = d; } } } if (!bestd) { F[n] = "("+F[n-1]+"+1)"; } else { F[n] = F[bestd]+"*"+F[n/bestd]; } } } string f(int n) { if (n <= N) { return F[n]; } // works for any N, eg for N=1024 we get n = x + 1024 * (y + 1024 * z), but this could yield up to 106 ones // with N == 2^15, recursion here is one step flatter, we get n = x + 2^15 * y auto s = f(n >> M); const auto x = n & (N-1); if (s == "1") { s = F[N]; } else { s += "*"+F[N]; } if (x) { s = "("+s+"+"+F[x]+")"; // parens necessary for M<15 } return s; } int main() { prep(); int tests; get tests; FOR (tt, 1, tests) { int x; get x; say f(x); } } |