Last non-zero Digit in N!(阶乘最后非0位)

Last non-zero Digit in
N!


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

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


 1 #include <iostream>
2 #include <string.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #define MAXN 10000
6
7 int lastdigit(char* buf){
8 const int mod[20]={1,1,2,6,4,2,2,4,2,8,4,4,8,4,6,8,8,6,8,2};
9 int len=strlen(buf),a[MAXN],i,c,ret=1;
10 if (len==1)
11 return mod[buf[0]-‘0‘];
12 for (i=0;i<len;i++)
13 a[i]=buf[len-1-i]-‘0‘;
14 for (;len;len-=!a[len-1]){
15 ret=ret*mod[a[1]%2*10+a[0]]%5;
16 for (c=0,i=len-1;i>=0;i--)
17 c=c*10+a[i],a[i]=c/5,c%=5;
18 }
19 return ret+ret%2*5;
20 }
21 int main()
22 {
23 char a[1000]="\0";
24 while(scanf("%s",a)!=EOF)
25 {
26 int ans=lastdigit(a);
27 printf("%d\n",ans);
28 }
29 return 0;
30 }

时间: 2024-08-11 11:06:18

Last non-zero Digit in N!(阶乘最后非0位)的相关文章

NYOJ1026 阶乘末尾非0 【模板】

阶乘末尾非0 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描述 我们的问题很是简单,n!末尾非0数是几? 比如n=5的时候,n!=120,那么n!末尾非0数是2. 输入 多组数据, 每组数据占一行,每行一个整数0<=n<=10^1000 输出 n!末尾非0数. 样例输入 5 样例输出 2 直接用的网上的模板 /*==================================================*| 阶乘最后非零位,复杂度 O(nlogn) \*==

hdu 1066 Last non-zero Digit in N! (数论——n!中的最后一个非0数字)

Last non-zero Digit in N! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6432    Accepted Submission(s): 1593 Problem Description The expression N!, read as "N factorial," denotes the pro

算法-计算阶乘n!末尾0的个数

算法逻辑转载自计算阶乘n!末尾0的个数: 问题描述    给定参数n(n为正整数),请计算n的阶乘n!末尾所含有"0"的个数.    例如,5!=120,其末尾所含有的"0"的个数为1:10!= 3628800,其末尾所含有的"0"的个数为2:20!= 2432902008176640000,其末尾所含有的"0"的个数为4. 计算公式    这里先给出其计算公式,后面给出推导过程.    令f(x)表示正整数x末尾所含有的&q

Rightmost Digit (求n^n最后一位)

Description Given a positive integer N, you should output the most right digit of N^N. Input The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test ca

计算阶乘n!末尾0的个数

一.问题描述 给定一个正整数n,请计算n的阶乘n!末尾所含有“0”的个数.例如: 5!=120,其末尾所含有的“0”的个数为1: 10!= 3628800,其末尾所含有的“0”的个数为2: 20!= 2432902008176640000,其末尾所含有的“0”的个数为4. 二.算法分析 此类问题很显然属于数学问题,一定要找到其中的本质规律才能得到正确的数学模型. 两个大数字相乘,都可以拆分成多个质数相乘,而质数相乘结果尾数为0的,只可能是2*5.如果想到了这一点,那么就可以进一步想到:两个数相乘

N阶乘尾部的0个数

N阶乘尾部的0个数 描述 设计一个算法,计算出n阶乘中尾部零的个数 思路: 1.1 * 2 * 3 * ... * n --> 1 * 2 * 3 * (2 * 2) * 5 * (2 * 3) * 7 (2 2 * 2) * (3 * 3) (2 5) * ...化成质数相乘,只有2 * 5才可能得到结果有尾数中有0 2.因为2的个数是比5多的,求0的个数问题就转化成了求5的个数的问题 3.5 * 5 * 5 * 5 * 5 * ... * 5有n个5 ; 得到有n个5:有几个 4.- ; -

(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

N阶乘结果末尾0的个数

N阶乘的结果sum,对这个结果进行质因数分解,sum=2x * 3y * 5z * 7w....,末尾为0是由2*5=10导致的.而被2整除的数比被5整除的数多很多,因此2*5的出现的次数应该是质因数5出现的次数Z. int NumofZero(int N) { int result=0; for(int i=5;i<=N;i+=5) { int temp=i; while(temp%5==0) { result++; temp/=5; } } }

阶乘问题 n!后最右边非0的数

阶乘问题 时间限制: 1 Sec  内存限制: 128 MB提交: 66  解决: 13[提交][状态][讨论版] 题目描述 也许你早就知道阶乘的含义,N阶乘是由1到N相乘而产生,如:     12! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 x 11 x 12 = 479,001,600 12的阶乘最右边的非零位为6.     写一个程序,计算N(1<=N<=50,000,000)阶乘的最右边的非零位的值. 注意:10,000,000!有2499999