The last Non-zero DigitTime
limit:MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64u SubmitStatusPracticePOJ 1150Appoint Description:System Crawler (2015-03-30)
Description
In this problem you'll be given the number of decimal integer number N, M. You'll have to find the last non-zero digit of the NP m.this means no of permutations of N things taking M at a time.
Input
The input contains several lines of input. Each line of the input file contains the integers n (0 <= n<= 20000000), M (0 <= m <= N).
Output
For each line of the input should output a single digit, which was the last non-zero digit of NP M. For example, if NP M are 720 then the last Non-zero digit is 2. So in this case your output should is 2.
Sample Input
10 1010 525 6
Sample Output
842
Test instructions: Ask for the last non-zero of C (n,m).
Idea: Weak just started to do math, these entry questions have been made of the dead, reference to the giant idea and code, ORZ, with links to find factorial last non-zero
The equivalent of seeking n!/(N-M)! The last non-zero, first to understand n! The last non-zero how to beg, for example to 10! :
Step1: First of all, 10! All 2,5 factors are removed;
Step2: Then find the number that is the end of the remaining string multiplied by the number. Focus
Step3: Due to the removal of more than 2:5, and finally to consider the extra portion of the 2 effect on the results.
Step4:output your answer!
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include < iostream> #include <sstream> #include <algorithm> #include <set> #include <queue> #include <stack> #include <map>using namespace std;typedef long long ll;const int inf=0x3f3f3f3f;const double pi= ACOs ( -1.0) int get_2 (int x)//Calculate the number of occurrences of the factorial medium factor 2 {if (x==0) return 0; else return (X/2+get_2 (X/2));} int get_5 (int x)//calculates the number of occurrences of the factorial medium factor 5 {if (x==0) return 0; else return (X/5+get_5 (X/5));} int get (int n,int x)//calculates the number of occurrences of x at the end of the odd series in F (1) to f (n) {if (n==0) return 0; else return (n/10+ (n%10>=x) +get (n/5,x));} int get_f (int n,int x)//calculates the number of occurrences of x at the end of F (1) to F (n), {if (n==0) return 0; Return Get_f (n/2,x) +get (n,x);} int mp[4][4]={{6,2,4,8},//2^n%10 The Loop section, note that if the number of 2 is 0, the result should be 1, special treatment. {1,3,9,7},//3{1,7,9,3},//7{1,9,1,9}//9};//3,7,9 is the first in the Cycle section, just 1, so there is no need to consider the number of 0 occurrences. int main () {int n,m; int res; While~SCANF ("%d%d", &n,&m)) {int n2=get_2 (n)-get_2 (n-m); int n5=get_5 (n)-get_5 (n-m); int N3=get_f (n,3)-get_f (n-m,3); int N7=get_f (n,7)-get_f (n-m,7); int N9=get_f (n,9)-get_f (n-m,9); Res=1; if (n5>n2) {printf ("5\n"); } else{if (n2!=n5) {res*=mp[0][(N2-N5)%4]; res%=10; }//If NUM2==NUM5, then 2^0 mod 10 should be 1 instead of 6 in table, so special handling is needed. RES*=MP[1][N3%4]; res%=10; RES*=MP[2][N7%4]; res%=10; RES*=MP[3][N9%4]; res%=10; printf ("%d\n", res); }} return 0;}
POJ 1150-the last Non-zero Digit (for factorial final non-zero)