Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1153 

In this problem you will be given two decimal integer number N, M. You will have to find the last non-zero digit of the P N M . This means no of permutations of N things taking M at a time. Input The input file contains several lines of input. Each line of the input file contains two integers N (0 ≤ N ≤ 20000000), M (0 ≤ M ≤ N). Input is terminated by end-of-file. Output For each line of the input file you should output a single digit, which is the last non-zero digit of P N M . For example, if P N M is 720 then the last non-zero digit is 2. So in this case your output should be 2.

Sample Input 10 10 10 5 25 6

Sample Output 8 4 2

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m) != EOF) {
        long long res = 1;
        for(int i=n;i>n-m;i--) {
            res *= i;
            while(res%10 == 0) res/=10;
            res %= 1000000000;
        }
        printf("%d\n",res%10);
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
10 10
10 5
25 6

Output

x
+
cmd
8
4
2
Advertisements

Demonstration


UVA Online Judge solution - 10212 - The Last Non - zero Digit - UVA Online Judge solution C,C++,Java

Previous
UVA Online Judge solution - 102 - Ecological Bin Packing - UVA Online Judge solution C,C++,Java
Next
UVA Online Judge solution - 10218 - Let's Dance - UVA Online Judge solution C,C++,Java