I was tasked to find a digit of kth position after the decimal point of a fraction(a/b).
Yesterday I found out this algorithm.
To get any digit after the decimal point, I generate a variable called rem and make a loop
for (int i = 1; i <= k+1; i++)
{
rem = a%b;
a = rem*10;
}
cout << a/b;
the loop will return a value being the kth digit after the decimal point.
However the task require me to calculate with a,b,k being very large number ( less or equal to 10e18), so it's sure that the code will exceed the time limit.
What your for loop calculates is effectively 10 * (a*10k % b) / b. We can do this more efficiently by doing exponentiation by squaring. We have to be careful not to overflow at every point though:
int kth_digit_frac(uint64_t a, uint64_t b, uint64_t k) {
return 10 * mulmodu64(a, powmod(10, k, b), b) / b;
}
// a*b % m, overflow safe
inline uint64_t mulmodu64(uint64_t a, uint64_t b, uint64_t m) {
#if defined(__GNUC__) && defined(__x86_64__)
uint64_t q, r;
asm("mulq %3;"
"divq %4;"
: "=a"(q), "=d"(r)
: "a"(a), "d"(b), "rm"(m)
: "cc");
return r;
#else
a %= m;
b %= m;
// No overflow possible.
if (a == 0) return 0;
if (b <= std::numeric_limits<uint64_t>::max() / a) return (a*b) % m;
uint64_t res = 0;
while (a != 0) {
if (a & 1) {
if (b >= m - res) res -= m;
res += b;
}
a >>= 1;
if (b >= m - b) b += b - m;
else b += b;
}
return res;
#endif
}
// b^e % m, overflow safe
inline uint64_t powmod(uint64_t b, uint64_t e, uint64_t m) {
uint64_t r = 1;
b %= m;
while (e) {
if (e % 2 == 1) r = mulmodu64(r, b, m);
b = mulmodu64(b, b, m);
e >>= 1;
}
return r;
}
It runs in the blink of an eye for any a,b,k
that fit in 64 bit integers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With