In the hint it is given that we can use string_multiplication or bit int but when i am using string multiplication it is giving time limit exceeded can you tell me the reason
Virat And Factorial problem
@amangoel987357 Use array for storing the digits of the multiplication. Use the basic childhood method we use for multiplication using carrys and remainders.
@amangoel987357 You can take this for your help:
ll multiply( ll x , vector& v , ll dig) {
ll carry = 0;
for ( int i = 0; i < dig; i++) {
ll prod = v[i] * x + carry;
v[i] = prod % 10;
carry = prod / 10;
}
while (carry > 0) {
v[dig] = carry % 10;
carry /= 10;
dig++;
}
return dig;
}
void fact ( ll n) {
vector v(500, 0);
v[0] = 1;
v[1] = 1;
ll dig = 1;
for ( int i = 2; i <= n; i++) {
dig = multiply(i, v, dig);
}
for ( int i = dig - 1; i >= 0; i–) {
cout << v[i] << “”;
}
}
Hey buddy i am using childhood method only
Did you get that? I have already sent you the approach.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.