#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int M = 32000; int d[M], p[M]; void pri(int n) { //printf("{%d %d}", n, p[n]); if (n<=0) return; if (n==1) { printf("1"); return; } if (d[n] == n) { printf("("); FOR(i,n) { if (i!=0) printf("+"); printf("1"); } printf(")"); return; } int v=p[n]; if (d[n] == d[n-v] + d[v]) { printf("("); pri(n-v); printf("+"); pri(v); printf(")"); } else { printf("("); pri(n/v); printf("*"); pri(v); printf(")"); } } void test() { int n; scanf("%d", &n); int m = sqrt(n); for (int a=-3; a<=3; a++) for (int b=a; b<=3; b++) { int sa=m+a, sb=m+b; if (sa<0 || sb<0) continue; if (n < sa*sb) continue; if (n-sa*sb >= M) continue; if (d[sa] + d[sb] + d[n-sa*sb] <= 100) { if (sa==0 || sb==0) { pri(n); printf("\n"); } else { pri(sa); printf("*"); pri(sb); if (n != sa*sb) { printf("+"); pri(n-sa*sb); } printf("\n"); } return; } } } void testall() { int sq=1; int mx=1, bad=0, who=1; for (int i=1; i<=1000000000; i++) { if ((sq+1)*(sq+1) <= i) sq++; int cur = 1e9; for (int a=-3; a<=3; a++) for (int b=a; b<=3; b++) { int sa=sq+a, sb=sq+b; if (i < sa*sb) continue; if (i-sa*sb >= M) continue; cur = min(cur, d[sa]+d[sb]+d[i-sa*sb]); } if (cur > 100) bad++; if (cur > mx) { mx = cur; who=i; } } printf("worst %d %d, %d wrong\n", mx, who, bad); } int main() { d[1]=1; p[1]=1; for (int x=2; x<M; x++) { d[x]=x; p[x]=x; for (int y=2; y*y <= x; y++) if (x%y==0 && d[y]+d[x/y] < d[x]) { d[x] = d[y]+d[x/y]; p[x] = y; } for (int y=1; 2*y <= x; y++) if (d[y]+d[x-y] < d[x]) { d[x] = d[y] + d[x-y]; p[x] = y; } /*for (int y=1; y<=100 && y<=x; y++) if (d[x-y] + y < d[x]) { d[x] = d[x-y] + y; p[x] = y; }*/ //printf("%d %d\n", x, d[x]); } fprintf(stderr, "generated\n"); fflush(stdout); //testall(); //return 0; int tt; scanf("%d", &tt); while (tt--) test(); 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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int M = 32000; int d[M], p[M]; void pri(int n) { //printf("{%d %d}", n, p[n]); if (n<=0) return; if (n==1) { printf("1"); return; } if (d[n] == n) { printf("("); FOR(i,n) { if (i!=0) printf("+"); printf("1"); } printf(")"); return; } int v=p[n]; if (d[n] == d[n-v] + d[v]) { printf("("); pri(n-v); printf("+"); pri(v); printf(")"); } else { printf("("); pri(n/v); printf("*"); pri(v); printf(")"); } } void test() { int n; scanf("%d", &n); int m = sqrt(n); for (int a=-3; a<=3; a++) for (int b=a; b<=3; b++) { int sa=m+a, sb=m+b; if (sa<0 || sb<0) continue; if (n < sa*sb) continue; if (n-sa*sb >= M) continue; if (d[sa] + d[sb] + d[n-sa*sb] <= 100) { if (sa==0 || sb==0) { pri(n); printf("\n"); } else { pri(sa); printf("*"); pri(sb); if (n != sa*sb) { printf("+"); pri(n-sa*sb); } printf("\n"); } return; } } } void testall() { int sq=1; int mx=1, bad=0, who=1; for (int i=1; i<=1000000000; i++) { if ((sq+1)*(sq+1) <= i) sq++; int cur = 1e9; for (int a=-3; a<=3; a++) for (int b=a; b<=3; b++) { int sa=sq+a, sb=sq+b; if (i < sa*sb) continue; if (i-sa*sb >= M) continue; cur = min(cur, d[sa]+d[sb]+d[i-sa*sb]); } if (cur > 100) bad++; if (cur > mx) { mx = cur; who=i; } } printf("worst %d %d, %d wrong\n", mx, who, bad); } int main() { d[1]=1; p[1]=1; for (int x=2; x<M; x++) { d[x]=x; p[x]=x; for (int y=2; y*y <= x; y++) if (x%y==0 && d[y]+d[x/y] < d[x]) { d[x] = d[y]+d[x/y]; p[x] = y; } for (int y=1; 2*y <= x; y++) if (d[y]+d[x-y] < d[x]) { d[x] = d[y] + d[x-y]; p[x] = y; } /*for (int y=1; y<=100 && y<=x; y++) if (d[x-y] + y < d[x]) { d[x] = d[x-y] + y; p[x] = y; }*/ //printf("%d %d\n", x, d[x]); } fprintf(stderr, "generated\n"); fflush(stdout); //testall(); //return 0; int tt; scanf("%d", &tt); while (tt--) test(); return 0; } |