FCTRL4 - Last Non-Zero Digit of Factorials

Tom is fascinated by big numbers. Most of all he likes factorials. First, he computed N! for some small values of N. But factorials were getting longer and longer very quickly and each of them had a long sequence of zeroes at its end. Since he was unable to consider so many digits in his research and he thought that the research on zeroes would be boring, he decided to focus on the last non-zero digit of factorials.

For example 4!=24 so the last non-zero digit is 4. For N=5 we have 5!=120 and the last non-zero digit 2.

Tom needs to know the last non-zero digit of N! for several specific values of N. Help him please.

Input specification

The input file consists of several positive integers (less than 10100) delimited by whitespace.

Output specification

The output file contains the last non-zero digit of N! for each integer N from the input file. Digits should be delimited by whitespace.

Example

Input file:
1
2
3
4
5
6
7
8

Output file:
1
2
6
4
2
2
4
2  

Added by:Fudan University Problem Setters
Date:2007-12-01
Time limit:0.5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 C99 ERL JS-RHINO OBJC SQLITE
Resource:IPSC 1999

hide comments
2018-01-09 13:09:32


Last edit: 2018-01-22 16:38:25
2016-06-01 23:50:12
It is ridiculous to put time limit. it will check for any value and the higher the value, the more time it takes.
Time limit Exceeded Error.
2015-05-17 12:29:56 rk
is there a O(1) soln bcz people got ac in 0.00 where as i got 0.04 if yes then plz share.
2014-11-03 14:25:20 Abhilash
do not follow the input specification.
go with format in example.

Last edit: 2014-11-03 14:26:12
2014-09-12 22:27:21 Infinity
delimited by white space means ??
and how many inputs ??
2013-08-13 07:28:28 John and the cows


Last edit: 2014-06-17 09:11:02
2013-08-11 07:19:06 John and the cows


Last edit: 2014-06-17 09:11:11
2013-02-02 12:44:36 Shubham Choudhary
getting runtime nzec error while using python
please can anyone help me
2013-01-04 21:48:20 strings
awesum prblm !! :)
2012-12-31 04:54:29 pardeep kumar



Last edit: 2013-04-17 06:39:06
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.