(hdu step 2.3.7)Last non-zero Digit in N!(阶乘最后一位非零位)

在写题解之前给自己打一下广告哈~。。抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下:

http://edu.csdn.net/course/detail/209

题目:

Last non-zero Digit in N!

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 815 Accepted Submission(s): 344
 

Problem Description

The expression N!, read as "N factorial," denotes the product of the first N positive integers, where N is nonnegative. So, for example, 
N N! 
0 1 
1 1 
2 2 
3 6 
4 24 
5 120 
10 3628800

For this problem, you are to write a program that can compute the last non-zero digit of the factorial for N. For example, if your program is asked to compute the last nonzero digit of 5!, your program should produce "2" because 5! = 120, and 2 is the last nonzero digit of 120.


Input

Input to the program is a series of nonnegative integers, each on its own line with no other letters, digits or spaces. For each integer N, you should read the value and compute the last nonzero digit of N!.


Output

For each integer input, the program should print exactly one line of output containing the single last non-zero digit of N!.


Sample Input

1
2
26
125
3125
9999


Sample Output

1
2
4
8
2
8

 

Source

South Central USA 1997


Recommend

JGShining

题目分析:

求阶乘最后一位非零位。因为N很大,那种暴力的思想肯定是行不通的。这道题想了很久,到现在想的也还不是很明白。但是吉大的ACM模板有这个。所以就直接用了。暂时先放一放吧。

代码如下:

/*
 * e.cpp
 *
 *  Created on: 2015年2月5日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <string.h>
#include <cstdlib>

#define MAXN 10000

int lastdigit(char* buf) {
	const int mod[20] = { 1, 1, 2, 6, 4, 2, 2, 4, 2, 8, 4, 4, 8, 4, 6, 8, 8, 6,
			8, 2 };
	int len = strlen(buf), a[MAXN], i, c, ret = 1;
	if (len == 1){
		return mod[buf[0] - ‘0‘];
	}
	for (i = 0; i < len; i++){
		a[i] = buf[len - 1 - i] - ‘0‘;
	}
	for (; len; len -= !a[len - 1]) {
		ret = ret * mod[a[1] % 2 * 10 + a[0]] % 5;
		for (c = 0, i = len - 1; i >= 0; i--){
			c = c * 10 + a[i];
			a[i] = c / 5;
			c %= 5;
		}
	}
	return ret + ret % 2 * 5;
}

int main(){
	char n[MAXN];
	while(scanf("%s",&n)!=EOF){
		printf("%d\n",lastdigit(n));
	}

	return 0;
}
时间: 2024-10-11 11:42:16

(hdu step 2.3.7)Last non-zero Digit in N!(阶乘最后一位非零位)的相关文章

(hdu step 4.1.5)find the nth digit(求S串中的第n个位置上是什么数字)

题目: find the nth digit Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 689 Accepted Submission(s): 244   Problem Description 假设:S1 = 1S2 = 12S3 = 123S4 = 1234.........S9 = 123456789S10 = 123456789

(hdu step 2.2.8)N!Again(求N!的阶乘%2009以后的结果)

题目: N!Again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 941 Accepted Submission(s): 526   Problem Description WhereIsHeroFrom:             Zty, what are you doing ?Zty:                        

(hdu step 1.3.8)Who&#39;s in the Middle(排序)

题目: Who's in the Middle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2938 Accepted Submission(s): 1109   Problem Description FJ is surveying his herd to find the most average cow. He wants to k

(hdu step 1.3.1)FatMouse&#39; Trade(在收入需要一定的付出的情况下求最大收入)

题目: FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5092 Accepted Submission(s): 1530   Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats gua

(hdu step 6.1.2)Eddy&#39;s picture(在只给出二维坐标点的情况下,求让n个点连通的最小费用)

题目: Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 172 Accepted Submission(s): 126   Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be

(hdu step 5.1.1)A Bug&#39;s Life((ai,bi)表示ai、bi不在同一堆中,有若干对数据,判断是否有bug)

题目: A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 723 Accepted Submission(s): 277   Problem Description Background Professor Hopper is researching the sexual behavior of a rare spe

(hdu step 3.2.4)FatMouse&#39;s Speed(在第一关键字升序的情况下,根据第二关键字来求最长下降子序列)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1034 Accepted Submission(s): 526   Proble

(hdu step 3.1.5)Tiling_easy version(求用2*1、2*2两种骨格铺满2*n的网格的方案数)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: Tiling_easy version Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 515 Accepted Submission(s): 447   Prob

(hdu step 5.1.3)Segment set(求与一条线段相交的线段集合中的线段的数量)

题目: Segment set Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 177 Accepted Submission(s): 82   Problem Description A segment and all segments which are connected with it compose a segment set. T