Code: Select all
if(d > 1000000000) d /= 1000000000
And in the end print the first three digits (it's easy in Java, at least it's good for something).
Moderator: Board moderators
Code: Select all
if(d > 1000000000) d /= 1000000000
I m getting TLE when using the fast exponentiation method for Leading part alsoDarko wrote:All I did was something like (within the pow function):where d is a double.Code: Select all
if(d > 1000000000) d /= 1000000000
Code: Select all
while (n > Integer.MAX_VALUE)
n /= 1000000000;
No I do fast exponentiation now and geting WADarko wrote:I lied, this is what I did:And - how do you get TLE if you do logk operations?Code: Select all
while (n > Integer.MAX_VALUE) n /= 1000000000;
Hmm, within the set of (2^32 - 1)*10^7 (approx. 4*10^16) possible input combinations, there will be an expected 40000 such cases, and an equal number of cases that get erroniously rounded down. I know of problems that test special cases with slimmer chances...Cho wrote:I think the 4th to 15th digit can be considered as some sort of pseudo-random number, then the probability that all these digits being 9 is exponentially small.little joey wrote:I also calculated the first three digits using <math.h> and got accepted, but I still have some doubts about precision.
How can we be sure that a 100-million digit number that starts off "123999999999999999999999999..." is not printed as "124..." without doing bigint calculus?
Code: Select all
FUNCTION USED TO FIND b^p mod m:
=========================
long long bigmod (long long b, long long p, long long m)
{
if (p == 0)
return 1;
else if (p % 2 == 0)
return (bigmod (b, p / 2, m) * bigmod (b, p / 2, m)) % m;
else
return ((b % m) * bigmod(b, p - 1, m)) % m;
}
Code: Select all
FUNCTION USED TO FIND base^power:
==========================
long long square (long long n)
{
return n * n;
}
long long fastexp (long long base, int power)
{
long long r;
char digits[10];
if (power == 0)
return 1;
else if (power % 2 == 0)
{
r = (square (fastexp (base, power / 2)));
if (r > BIG)
{
sprintf(digits, "%lld", r);
digits[4] = NULL;
r = atol(digits);
}
return r;
}
else
{
r = (base * ( fastexp (base, power - 1)));
if (r > BIG)
{
sprintf(digits, "%lld", r);
digits[4] = NULL;
r = atol(digits);
}
return r;
}
}
BIG is defined as #define BIG 10000l
Everytime the resulting number gets bigger than BIG, I try to truncate the following digits[taking only the higher ones]. [IS THIS METHOD OKAY TO GET LEADING DIGITS ???]
if p is even you are computing bigmod b, p/2 twice instead of once in each debth of recursion. write something likenymo wrote:Hi, I have submitted this problem several times but only managed to get T.L.E. I find the trailing digits by bigmod function and leading digits by fast exponentiation. Followings are the part of my code:Code: Select all
FUNCTION USED TO FIND b^p mod m: ========================= long long bigmod (long long b, long long p, long long m) { if (p == 0) return 1; else if (p % 2 == 0) return (bigmod (b, p / 2, m) * bigmod (b, p / 2, m)) % m; else return ((b % m) * bigmod(b, p - 1, m)) % m; }