#include <cstdio> #include <cmath> //#include <iostream> #include <string> using namespace std; const int KMAX = 31627;//*1000; const int ADD = 1; const int MUL = 2; const int ONES = 3; int wyn[KMAX][4]; // 0 - ile minimum; 1 - ktora ostatnia pierwsza; 2 - ktora ostatnia druga; 3 - type of operation [1 +; 2 -] void init() { wyn[0][0] = 0; wyn[0][1] = 0; wyn[0][2] = 0; wyn[0][3] = ONES; wyn[1][0] = 1; wyn[1][1] = 0; wyn[1][2] = 0; wyn[1][3] = ONES; } void factor(int &number, int &newValue){ newValue++; for(int i = newValue; i * i < number; i++) { if(number % i == 0) { number /= i; newValue = i; return; } } newValue = number; number = 1; } void count(int n) { wyn[n][0] = n; wyn[n][1] = -1; wyn[n][2] = -1; wyn[n][3] = ONES; int Value = 1; int numberFst = n; int numberSnd = 1; while (numberFst > 1) { if(wyn[n][0] > wyn[numberFst][0] + wyn[numberSnd][0]) { wyn[n][0] = wyn[numberFst][0] + wyn[numberSnd][0]; wyn[n][1] = numberFst; wyn[n][2] = numberSnd; wyn[n][3] = MUL; } factor(numberFst, Value); numberSnd *= Value; } for(int i = 1; i < n && i < 3; i++) if(wyn[n][0] > wyn[i][0] + wyn[n - i][0]) { wyn[n][0] = wyn[i][0] + wyn[n - i][0]; wyn[n][1] = i; wyn[n][2] = n - i; wyn[n][3] = ADD; } } void makeOutput(int n) { if(n >= KMAX) { int number = n; int ind = 0; for(int i = 2; i * i <= n; i++) { while(number % i == 0) { number /= i; if(ind != 0) { printf("*"); } makeOutput(i); ind++; } } if(number > 1){ printf("+1+"); makeOutput(number-1); } } else { if(wyn[n][0] > 100) { printf("NIE"); return; } if(wyn[n][3] == ONES) { if(wyn[n][0] == 1) printf("1"); else { printf("("); for(int i = 0; i < n - 1; i++) { printf("1+"); } printf("1)"); } return; } printf("("); makeOutput(wyn[n][1]); if(wyn[n][3] == ADD) printf("+"); else if(wyn[n][3] == MUL) printf("*"); makeOutput(wyn[n][2]); printf(")"); } } int main() { init(); for(int i = 2; i <= KMAX - 1 ; i++) { count(i); } /*int ind = 20771; printf("|i=%d| %d %d %d %d\n",ind , wyn[ind][0], wyn[ind][1], wyn[ind][2], wyn[ind][3]); int wmax = 0; for(int i= 1; i <= KMAX -1 ; i++) if(wyn[i][0] > wmax) wmax = wyn[i][0]; printf("wmax = %d\n" ,wmax); for(int i = 1; i <= 1000 ; i++) if(wyn[i][3] == 1){ printf("|i=%d| %d %d %d %d\n",i , wyn[i][0], wyn[i][1], wyn[i][2], wyn[i][3]); makeOutput(i); printf("\n"); }*/ int t, k; scanf("%d ",&t); for(int q = 0; q < t; q++) { scanf("%d",&k); int i = k; //printf("|i=%d| %d %d %d %d\n",i , wyn[i][0], wyn[i][1], wyn[i][2], wyn[i][3]); makeOutput(k); printf("\n"); } 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 | #include <cstdio> #include <cmath> //#include <iostream> #include <string> using namespace std; const int KMAX = 31627;//*1000; const int ADD = 1; const int MUL = 2; const int ONES = 3; int wyn[KMAX][4]; // 0 - ile minimum; 1 - ktora ostatnia pierwsza; 2 - ktora ostatnia druga; 3 - type of operation [1 +; 2 -] void init() { wyn[0][0] = 0; wyn[0][1] = 0; wyn[0][2] = 0; wyn[0][3] = ONES; wyn[1][0] = 1; wyn[1][1] = 0; wyn[1][2] = 0; wyn[1][3] = ONES; } void factor(int &number, int &newValue){ newValue++; for(int i = newValue; i * i < number; i++) { if(number % i == 0) { number /= i; newValue = i; return; } } newValue = number; number = 1; } void count(int n) { wyn[n][0] = n; wyn[n][1] = -1; wyn[n][2] = -1; wyn[n][3] = ONES; int Value = 1; int numberFst = n; int numberSnd = 1; while (numberFst > 1) { if(wyn[n][0] > wyn[numberFst][0] + wyn[numberSnd][0]) { wyn[n][0] = wyn[numberFst][0] + wyn[numberSnd][0]; wyn[n][1] = numberFst; wyn[n][2] = numberSnd; wyn[n][3] = MUL; } factor(numberFst, Value); numberSnd *= Value; } for(int i = 1; i < n && i < 3; i++) if(wyn[n][0] > wyn[i][0] + wyn[n - i][0]) { wyn[n][0] = wyn[i][0] + wyn[n - i][0]; wyn[n][1] = i; wyn[n][2] = n - i; wyn[n][3] = ADD; } } void makeOutput(int n) { if(n >= KMAX) { int number = n; int ind = 0; for(int i = 2; i * i <= n; i++) { while(number % i == 0) { number /= i; if(ind != 0) { printf("*"); } makeOutput(i); ind++; } } if(number > 1){ printf("+1+"); makeOutput(number-1); } } else { if(wyn[n][0] > 100) { printf("NIE"); return; } if(wyn[n][3] == ONES) { if(wyn[n][0] == 1) printf("1"); else { printf("("); for(int i = 0; i < n - 1; i++) { printf("1+"); } printf("1)"); } return; } printf("("); makeOutput(wyn[n][1]); if(wyn[n][3] == ADD) printf("+"); else if(wyn[n][3] == MUL) printf("*"); makeOutput(wyn[n][2]); printf(")"); } } int main() { init(); for(int i = 2; i <= KMAX - 1 ; i++) { count(i); } /*int ind = 20771; printf("|i=%d| %d %d %d %d\n",ind , wyn[ind][0], wyn[ind][1], wyn[ind][2], wyn[ind][3]); int wmax = 0; for(int i= 1; i <= KMAX -1 ; i++) if(wyn[i][0] > wmax) wmax = wyn[i][0]; printf("wmax = %d\n" ,wmax); for(int i = 1; i <= 1000 ; i++) if(wyn[i][3] == 1){ printf("|i=%d| %d %d %d %d\n",i , wyn[i][0], wyn[i][1], wyn[i][2], wyn[i][3]); makeOutput(i); printf("\n"); }*/ int t, k; scanf("%d ",&t); for(int q = 0; q < t; q++) { scanf("%d",&k); int i = k; //printf("|i=%d| %d %d %d %d\n",i , wyn[i][0], wyn[i][1], wyn[i][2], wyn[i][3]); makeOutput(k); printf("\n"); } return 0; } |