#include <algorithm>
#include <cstdint>
#include <iostream>
#include <string>
#include <vector>
////////////////////////////////////////////////////////////////////////////////
std::vector<int32_t> FF(int32_t n) {
std::vector<int32_t> a;
int32_t z = 2;
while (z * z <= n) {
if (n % z == 0) {
a.push_back(z);
n /= z;
} else {
z++;
}
}
if (n > 1)
a.push_back(n);
return a;
}
////////////////////////////////////////////////////////////////////////////////
std::string RR(int32_t n) {
std::string s = "(1+";
std::vector<int32_t> a = FF(n - 1);
for (size_t i = 0; i < a.size(); i++) {
if (i + 1 == a.size())
s += RR(a[i]);
else
s += RR(a[i]) + "*";
}
if (n == 3)
return "(1+1+1)";
if (n == 2)
return "(1+1)";
if (n == 1)
return "(1)";
return s + ")";
}
////////////////////////////////////////////////////////////////////////////////
int main(void) {
int32_t t, r;
scanf("%d", &t);
for (size_t i = 0; i < t; i++) {
scanf("%d", &r);
if(r == 1) {
printf("1\n");
continue;
}
std::vector<int32_t> a = FF(r);
for (size_t j = 0; j < a.size(); j++) {
if (j + 1 == a.size())
printf("%s", RR(a[j]).c_str());
else
printf("%s*", RR(a[j]).c_str());
}
printf("\n");
}
}
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 | #include <algorithm> #include <cstdint> #include <iostream> #include <string> #include <vector> //////////////////////////////////////////////////////////////////////////////// std::vector<int32_t> FF(int32_t n) { std::vector<int32_t> a; int32_t z = 2; while (z * z <= n) { if (n % z == 0) { a.push_back(z); n /= z; } else { z++; } } if (n > 1) a.push_back(n); return a; } //////////////////////////////////////////////////////////////////////////////// std::string RR(int32_t n) { std::string s = "(1+"; std::vector<int32_t> a = FF(n - 1); for (size_t i = 0; i < a.size(); i++) { if (i + 1 == a.size()) s += RR(a[i]); else s += RR(a[i]) + "*"; } if (n == 3) return "(1+1+1)"; if (n == 2) return "(1+1)"; if (n == 1) return "(1)"; return s + ")"; } //////////////////////////////////////////////////////////////////////////////// int main(void) { int32_t t, r; scanf("%d", &t); for (size_t i = 0; i < t; i++) { scanf("%d", &r); if(r == 1) { printf("1\n"); continue; } std::vector<int32_t> a = FF(r); for (size_t j = 0; j < a.size(); j++) { if (j + 1 == a.size()) printf("%s", RR(a[j]).c_str()); else printf("%s*", RR(a[j]).c_str()); } printf("\n"); } } |
English