Hello guys, I hope you are doing well in this quarantine period. I want to share some small things from which my some question got WA instead of Accepted which needs to be taken care of while coding and I note down those points and also want to share with you. Maybe you already know them and maybe, for someone these are helpful.
- When we use pow function then it may give the wrong answer when we are using it for big values so instead, always add some changes while using it like instead of using directly, add 0.5 to it given value and then typecast into it int or long long as per the requirement. Example avoid writing pow(2,20), instead write (long long)(pow(2,20)+0.5). This will surely give the correct result.
[Edit]: please avoid using pow function as discussed in comments. Instead, use the divide and conquer method:
long long power(long long a,long long b)
{
if(b==0) return 1;
long long p = power(a, b/2);
if(b&1) return p*p*a;
else return p*p;
}
Don't use sqrt function in a loop for comparison like this
for(int i = 0; i <= sqrt(n); i++){}
but precalculate the value of sqrt(n) and store it in different variable then use it in for loop. This is because if you don't precalculate the value then it may give TLE because every time loop will execute then every time it calculates sqrt(n).When we use v.size() for vector so this gives runtime error once in my solution because maybe I subtract some bigger value from its returned value which I didn't able to know during the contest then someone told me that .size() return unsigned long long value due to which this happens so from onwards I always start to typecast it into long long also such that that type of error will not come again. This is that solution: link
Always typecast first value into a long double then use it in log function because otherwise, it may give the wrong answer due to some precision error in big values. Use log like this
log((log double)z)
.
[Edit]: Instead of typecase you can also directly use logl function which automatically typecast and help to find the desired output.
These are some things that I learned during this quarantine period and this seems very small mistakes but very difficult to debug.
Thanks to all and also welcome other tips and suggestions. And please correct me if I am wrong somewhere.
I really though this was going to be a life lesson or something... :)
lol...no, these are things only because of which sometimes I almost solved the question but not able to complete it and my rank goes down very much so I thought this will help others.
Very important suggestions.
for(int i=1; i<=sqrt(n); i++)
you can also write asfor(int i=1; i*i<=n; i++)
and also many times the value of n changes during the loop so precalculation won't work.Please, long long i = 1LL; This gave me a lot of headaches in the past LOL
Your pow solution doesn't work always. e.g.:
Output: -9223372036854775808 9223372036854775807
(long long)(pow(3, 39) + 0.5)
gives4052555153018976256
on my computer, which is an even number. In fact, $$$3^{39} = 4052555153018976267$$$. Don't usepow
for exact computations.One can also write
for (int i = 0; i * i <= n; ++i)
orfor (long long i = 0; i * i <= n; ++i)
ifn
is large. I'm not sure thatsqrt(n)
can always handle a sufficient precision.In your solution
factor.size() - 1
can beULLONG_MAX
iffactor
is empty. If you want to handle this separately, you can also handle the case whenfactor
is of size 1 as well. One can instead write(int)factor.size() - 2
or create a define#define sz(x) (int)(x).size()
.There is indeed a
log
function of signaturelong double log(long double)
, but instead of casting an argument tolong double
one can use the functionlogl
. By the way, adding a letterl
to many other double functions make them work with long doubles as well (for example,powl
,sinl
etc).You can also do
Where MOD is some number.