I wrote the solution in many ways, this one being the most foolproof:
Code: Select all
#include <stdio.h>
#include <iostream>
using namespace std;
long double d1, d2, p3, p6;
int i1, i2, m = 1000000;
int cases, depth;
int main()
{
cin >> cases;
p3 = m / 3.0;
p6 = m * 2 / 3.0;
for(int cc = 0; cc < cases; cc++){
cin >> d1 >> d2;
i1 = d1 * m + 0.4;
i2 = d2 * m + 0.4;
for(depth = 0; ; depth++){
if(i1 > p3 && i1 < p6 && i2 > p3 && i2 < p6){
printf("Case %d: %d\n", cc + 1, depth);
break;
}
if(depth >= 200){
printf("Case %d: %d\n", cc + 1, -1);
break;
}
if(i1 < p3 || i1 > p6) i1 *= 3;
i1 %= m;
if(i2 < p3 || i2 > p6) i2 *= 3;
i2 %= m;
}
}
}
I tried on my computer all possible inputs. 29 should be the highest the depth can be (ie for 0.991325).
I've heard that sometimes the standard input functions can get doubles wrong. I tried getting the input with getc, but was still WA.
Update: when I used double and the input was 0.3 0.3 then i1 and i2 were 299999. I haven't found an input that would do that with long double yet, though I guess i should look into doing manual input in a again (maybe I made a mistake before).
Update 2: Added +0.4 when converting from double to integer. Had the program go through all possible inputs again (0.000001 - 0.999999), everything seems to have processed correctly. Still, no luck uploading though