FCTRL4 - Last Non-Zero Digit of Factorials

no tags 

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  

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

Last edit: 2018-01-22 16:38:25
akmal_24: 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.

rk: 2015-05-17 12:29:56

is there a O(1) soln bcz people got ac in 0.00 where as i got 0.04 if yes then plz share.

Abhilash: 2014-11-03 14:25:20

do not follow the input specification.
go with format in example.

Last edit: 2014-11-03 14:26:12
Infinity: 2014-09-12 22:27:21

delimited by white space means ??
and how many inputs ??

John and the cows: 2013-08-13 07:28:28

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

Last edit: 2014-06-17 09:11:11
Shubham Choudhary: 2013-02-02 12:44:36

getting runtime nzec error while using python
please can anyone help me

strings: 2013-01-04 21:48:20

awesum prblm !! :)

pardeep kumar: 2012-12-31 04:54:29


Last edit: 2013-04-17 06:39:06

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