#include <cstdio> #include <cmath> using namespace std; typedef unsigned int uint_t; const static uint_t MAX_X = 1000 * 1000 * 20; uint_t PRIME_SUM[MAX_X]; bool SUB_SUM[MAX_X]; uint_t factor_sum(uint_t x) { if (x <= 5) return x; if (x < MAX_X && PRIME_SUM[x]) return PRIME_SUM[x]; uint_t prime_sum = 0, prime_sum2 = 0; uint_t i = 2; uint_t p = x; uint_t e = (uint_t)sqrt(p); while (i <= e) { while ((p % i) == 0) { p /= i; e = (uint_t)sqrt(p); prime_sum += factor_sum(i); } i++; } if (p != x) { if (p > 1) prime_sum += factor_sum(p); } else prime_sum = x; if (x & 1) { prime_sum2 = factor_sum(x - 1) + 1; if (prime_sum2 < prime_sum) { prime_sum = prime_sum2; if (x < MAX_X) SUB_SUM[x] = true; } } if (x < MAX_X) PRIME_SUM[x] = prime_sum; return prime_sum; } void print_factor(uint_t x); void print_sub(uint_t x) { printf("(1+"); print_factor(x - 1); printf(")"); } void print_simple(uint_t x) { uint_t prime_sum = 0, prime_sum2 = 0; uint_t i = 2; uint_t p = x; uint_t e = (uint_t)sqrt(p); printf("("); while (i <= e) { while ((p % i) == 0) { p /= i; e = (uint_t)sqrt(p); print_factor(i); if (p > 1) printf("*"); } i++; } if (p > 1) print_factor(p); printf(")"); } void print_factor(uint_t x) { switch (x) { case 1: printf("1"); return; case 2: printf("(1+1)"); return; case 3: printf("(1+1+1)"); return; case 4: printf("(1+1+1+1)"); return; case 5: printf("(1+1+1+1+1)"); return; } if (x < MAX_X) { if (SUB_SUM[x]) return print_sub(x); return print_simple(x); } if (x & 1 == 0) return print_simple(x); uint_t prime_sum = 0, prime_sum2 = 0; uint_t i = 2; uint_t p = x; uint_t e = (uint_t)sqrt(p); while (i <= e) { while ((p % i) == 0) { p /= i; e = (uint_t)sqrt(p); prime_sum += factor_sum(i); } i++; } if (p != x) { if (p > 1) prime_sum += factor_sum(p); } else prime_sum = x; prime_sum2 = factor_sum(x - 1) + 1; if (prime_sum2 < prime_sum) return print_sub(x); return print_simple(x); } int main() { uint_t t, k, prime_sum; scanf("%u", &t); for (uint_t i = 0; i < t; ++i) { scanf("%u", &k); prime_sum = factor_sum(k); if (prime_sum > 100) printf("NIE\n"); else { print_factor(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 | #include <cstdio> #include <cmath> using namespace std; typedef unsigned int uint_t; const static uint_t MAX_X = 1000 * 1000 * 20; uint_t PRIME_SUM[MAX_X]; bool SUB_SUM[MAX_X]; uint_t factor_sum(uint_t x) { if (x <= 5) return x; if (x < MAX_X && PRIME_SUM[x]) return PRIME_SUM[x]; uint_t prime_sum = 0, prime_sum2 = 0; uint_t i = 2; uint_t p = x; uint_t e = (uint_t)sqrt(p); while (i <= e) { while ((p % i) == 0) { p /= i; e = (uint_t)sqrt(p); prime_sum += factor_sum(i); } i++; } if (p != x) { if (p > 1) prime_sum += factor_sum(p); } else prime_sum = x; if (x & 1) { prime_sum2 = factor_sum(x - 1) + 1; if (prime_sum2 < prime_sum) { prime_sum = prime_sum2; if (x < MAX_X) SUB_SUM[x] = true; } } if (x < MAX_X) PRIME_SUM[x] = prime_sum; return prime_sum; } void print_factor(uint_t x); void print_sub(uint_t x) { printf("(1+"); print_factor(x - 1); printf(")"); } void print_simple(uint_t x) { uint_t prime_sum = 0, prime_sum2 = 0; uint_t i = 2; uint_t p = x; uint_t e = (uint_t)sqrt(p); printf("("); while (i <= e) { while ((p % i) == 0) { p /= i; e = (uint_t)sqrt(p); print_factor(i); if (p > 1) printf("*"); } i++; } if (p > 1) print_factor(p); printf(")"); } void print_factor(uint_t x) { switch (x) { case 1: printf("1"); return; case 2: printf("(1+1)"); return; case 3: printf("(1+1+1)"); return; case 4: printf("(1+1+1+1)"); return; case 5: printf("(1+1+1+1+1)"); return; } if (x < MAX_X) { if (SUB_SUM[x]) return print_sub(x); return print_simple(x); } if (x & 1 == 0) return print_simple(x); uint_t prime_sum = 0, prime_sum2 = 0; uint_t i = 2; uint_t p = x; uint_t e = (uint_t)sqrt(p); while (i <= e) { while ((p % i) == 0) { p /= i; e = (uint_t)sqrt(p); prime_sum += factor_sum(i); } i++; } if (p != x) { if (p > 1) prime_sum += factor_sum(p); } else prime_sum = x; prime_sum2 = factor_sum(x - 1) + 1; if (prime_sum2 < prime_sum) return print_sub(x); return print_simple(x); } int main() { uint_t t, k, prime_sum; scanf("%u", &t); for (uint_t i = 0; i < t; ++i) { scanf("%u", &k); prime_sum = factor_sum(k); if (prime_sum > 100) printf("NIE\n"); else { print_factor(k); printf("\n"); } } return 0; } |