So there's this question 1995C - Квадратирование
It basically asks to count how many times we need to square elements at different indexes .. such that the whole array becomes non decreasing..
I've reduced it down to a math formula that goes like this
k[i] = k[i-1] + (ceil(log2(log2(a[i-1])) - log2(log2(a[i])))
here k[i] is the number of times i need to square at index i..
i reached here by writing an inequality in terms of a[i] powered (2^k[i]).. and same for a[i-1] and then took log2 of both sides. below is my code that implements this solution..
352427954
heres the main block of code from my solution:
int cnt = 0;
int prev = 0;
for(int i=2;i<=n;i++) {
if(arr[i]==1 && arr[i-1]>1) {
cout<<-1<<"\n";
return;
}
if(arr[i-1]==1) continue;
long double a = log2(arr[i-1]);
long double b = log2(arr[i]);
int ax2 = prev + ((int)ceil(log2(a) - log2(b)));
ax2 = max(0ll, ax2);
cnt += ax2;
prev = ax2;
}
cout<<cnt<<"\n";
I however am getting a WA on some test case when i submit this solution... Is there some flaw in my logic? I'm pretty sure I understood the question correctly.. Perhaps there's some cases I am not considering? I know that the computer is not accurate when it comes to floating point precisions.. Is that the problem? I'd appreciate any help.. Thank you in advance.
Полный текст и комментарии »