input with this test is true
10 # the number of digits in the bottom
4 2 2 2 2 2 # first digit number of degrees in this row -1
1 2 2
1 3 2
1 2 3
3 2 2 2 2
2 2 2 2
1 3 3
3 3 3 3 3
2 4 3 3
2 2 3 4
output
2 4 3 6 7 5 9 10 1 8
Here the task is to raise to a power and then compare their numbers to derive the index in the order of increasing numbers
exponentiation is from the end
2 2 3 4
is raised as 3 ^ 4 = 81
Now 2 ^ 81 = 2417851639229258349412352
if you submit such numbers, it works incorrectly
input
3
4 5 2 49 4 5
4 3 2 7 2 11
4 2 2 49 32 2
output
3 2 1 should be
my conclusion 3 1 2
#include <fstream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
using namespace std;
class tower_t {
public:
int num;
int height;
double val[11]; double cache[11];
tower_t() {
for (int i = 0; i < 11; i++) {
val[i] = 1;
cache[i] = 0;
}
height = 0;
}
double head(int level) {
if(cache[level] == 0) cache[level] = (log2(log2(val[level])) + log2(val[level + 1]) * val[level + 2]);
return cache[level];
}
void normalize() {
while(height > 1 && (log2(val[height - 2]) * val[height - 1]) < 50) {
val[height - 2] = pow(val[height - 2], val[height - 1]);
val[height - 1] = 1;
height--;
}
}
void print() {
#ifdef _DEBUG
printf("%2d: {", num);
for (int i = 0; i < height; i++) {
if (i > 0) printf(", ");
if(val[i] < 1000000000) {
printf("%0.0f", val[i]);
} else {
printf("%0.3e", val[i]);
}
}
printf("}\n");
#endif
}
};
bool compare(tower_t& t1, tower_t& t2) {
int level = ((t1.height > t2.height) ? t1.height : t2.height) - 3;
if (level < 0) level = 0;
if(t1.height == t2.height) { for (int i = t1.height - 1; i >= 0; i--) {
if (abs(t1.val[i] - t2.val[i]) > (t1.val[i] * 1e-14)) {
if (i < level) { return t1.val[i] < t2.val[i];
}
break;
}
}
}
return t1.head(level) < t2.head(level);
}
int main()
{
ifstream in ("input.txt");
int cnt;
in >> cnt;
tower_t* towers = new tower_t[cnt];
for (int i = 0; i < cnt; i++) {
int len;
in >> len;
towers[i].num = i + 1;
bool write = true;
for (int j = 0; j <= len; j++) {
int val;
in >> val;
if (val <= 1) write = false; if(write) {
towers[i].val[j] = val;
towers[i].height = j + 1;
}
}
towers[i].print();
towers[i].normalize();
}
sort(towers, towers + cnt, compare);
ofstream out("output.txt");
for (int i = 0; i < cnt; i++) {
out << towers[i].num << " ";
towers[i].print();
}
out << endl;
out.close();
delete[] towers;
return 0;
}
What I have tried:
number theory - Complexity class of comparison of power towers - Mathematics Stack Exchange[
^]