TopCoder problem "KLastNonZeroDigits" used in SRM 341 (Division I Level One , Division II Level Two)



Problem Statement

    

You are given an int N. The factorial of N is defined as N*(N-1)*(N-2)*...*1. Compute the factorial of N and remove all of its rightmost zero digits. If the result is more than K digits long, return the last K digits as a string. Otherwise, return the entire result as a string.

 

Definition

    
Class:KLastNonZeroDigits
Method:getKDigits
Parameters:int, int
Returns:String
Method signature:String getKDigits(int N, int K)
(be sure your method is public)
    
 

Constraints

-N will be between 1 and 20, inclusive.
-K will be between 1 and 9, inclusive.
 

Examples

0)
    
10
3
Returns: "288"
You would first compute the factorial of 10, which is 10*9*8*7*6*5*4*3*2*1=3628800. You would then remove all rightmost zeros to get 36288. Finally, you would return the last 3 digits as a string: "288".
1)
    
6
1
Returns: "2"
The factorial of 6 is 720.
2)
    
6
3
Returns: "72"
3)
    
7
2
Returns: "04"
The factorial of 7 is 5040. We remove the last zero to get "504". The last 2 digits of "504" are "04".
4)
    
20
9
Returns: "200817664"
5)
    
1
1
Returns: "1"

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=7397

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10665&pm=7397

Writer:

pure_

Testers:

PabloGilberto , brett1479 , Olexiy , Andrew_Lazarev

Problem categories:

Simple Math