| 30 | } |
| 31 | |
| 32 | void calc_fact(int n){ // Function to calculate factorial |
| 33 | int res = 0, carry = 0; // Store the result of n in array for reusing. |
| 34 | vec_2d[n].resize(0); |
| 35 | for(int i: vec_2d[n-1]){ // Iterating each digit of the immediate prev factorial result (e.g., for num = 5, immediate prev factorial will be 24 (4*3*2*1) ) |
| 36 | res = (n*i) + carry; // In each iteration, calculating the new value (obtained by multiplying n with each digit) starting with unit digit and adding it with the previous carry value |
| 37 | vec_2d[n].push_back(res%10); // Store the unit digit of the new value in array |
| 38 | carry = res/10; // Calculate the carry of the new value |
| 39 | } |
| 40 | while(carry>0){ // if carry is not 0 |
| 41 | vec_2d[n].push_back(carry%10); // Store the carry value in array |
| 42 | carry/=10; // Calculate carry if possible |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void solve(){ |
| 47 | // solution starts from here |