题目:
Last non-zero Digit in N!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7929    Accepted Submission(s): 2037

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

 
LANGUAGE:C
CODE:


解题方法看不懂,先放着吧。把大神的代码给贴上


 题目意思:求N!的最后一位非零数字,例如:9!=362880,这时你应该输出8.

开始没有看清楚题目,不知道N有多大,WA了N道,后来才知道N可以非常大,不是我能解决的范围了,然后再一看吉大ACM模板,有这个模板,我就照着输了。先把最后所有的0去掉,方法就是在乘的时候统计因子2的个数,然后每遇到一个5,就去掉一个,那么乘出来就没有最后的0了~然后在乘的时候只保留最后一位就可以了,最后把统计了的2的个数乘回去(在统计的时候把2给提出来,这样就可以避免模的除法了!)




#include<stdio.h>
#include<string.h> #define maxn 10001 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';
while(len)
{
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;
}
len-=!a[len-1];
}
return ret+ret%2*5;
} int main()
{
char n[maxn];
while(scanf("%s",n)!=EOF)
{
printf("%d\n",lastdigit(n));
}
return 0;
}

数论:HDU1066-Last non-zero Digit in N!的更多相关文章

  1. POJ 1150 The Last Non-zero Digit 数论+容斥

    POJ 1150 The Last Non-zero Digit 数论+容斥 题目地址: id=1150" rel="nofollow" style="colo ...

  2. hdoj Last non-zero Digit in N! 【数论】

    找规律! 求N!最后非0位的值.比方2是120的最后一个不是0的值. 输入N比較大,要大数保存. 注意到最后0的个数是与5的因数的个数相等.设f(n)为n!的最后非0位. 那么f(n)=((n%5)! ...

  3. 2018.09.17 atcoder Digit Sum(数论)

    传送门 数论好题啊. 首先对于b<=sqrt(n)b<=sqrt(n)b<=sqrt(n)的情况直接枚举b判断一下就行了. 下面谈一谈如何解决b>sqrt(n)b>sqr ...

  4. HDU 1066 Last non-zero Digit in N!(数论,大数,wait)

    The expression N!, read as "N factorial," denotes the product of the first N positive inte ...

  5. HDU 1060 Leftmost Digit (数论,快速幂)

    Given a positive integer N, you should output the leftmost digit of N^N.  InputThe input contains se ...

  6. 10162 - Last Digit (数论+周期规律)

    UVA 10162 - Last Digit 题目链接 题意:求S=(11+22+...NN)%10 思路:打出0-9的每一个周期,发现周期为1或2或4.所以S是以20一个周期,打出表后发现20为4. ...

  7. hdu1066 Last non-zero Digit in N!(求阶乘最后一位不为0的数字)

    http://acm.hdu.edu.cn/showproblem.php?pid=1066 转自:https://blog.csdn.net/fengyu0556/article/details/5 ...

  8. HDU 1060 Leftmost Digit 基础数论

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1060   这道题运用的是数学方法. 假设S=n^n.两边同时取对数,得到lgS=nlgn.即有S=10 ...

  9. sdut 2165:Crack Mathmen(第二届山东省省赛原题,数论)

    Crack Mathmen Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  Since mathmen take securit ...

随机推荐

  1. SpringBoot | 第二十章:异步开发之异步请求

    前言 关于web开发的相关知识点,后续有补充时再开续写了.比如webService服务.发邮件等,这些一般上觉得不完全属于web开发方面的,而且目前webService作为一个接口来提供服务的机会应该 ...

  2. 一、基础知识 React API 一览

    1.10 Hooks 参考文章:https://juejin.im/post/5be3ea136fb9a049f9121014 demo: /** * 必须要react和react-dom 16.7以 ...

  3. app后台管理系统框架metronic的学习笔记

    先来看效果: 给出当前页面的代码: <!DOCTYPE html> <!--[if IE 8]> <html lang="zh" class=&quo ...

  4. LeetCode Sort List 链表排序(规定 O(nlogn) )

    Status: AcceptedRuntime: 66 ms 题意:根据给出的单链表,用O(nlogn)的时间复杂度来排序.由时间复杂度想到快排.归并这两种排序.本次用的是归并排序.递归将链表的规模不 ...

  5. COGS 898. [咲 -Saki-] 天才麻将少女什么编

    ★☆   输入文件:sakinani.in   输出文件:sakinani.out   简单对比时间限制:1 s   内存限制:256 MB 题目背景 二十一世纪,世界上的麻将竞技人数超过一亿,日本每 ...

  6. IDA逆向:数组的逆向

    阅读<IDA Pro权威指南>第八章,整理的一些笔记,作为逆向的基础,可能有很多认识不足. //全局分配数组 *************************************** ...

  7. linux 命令——2 cd (转)

    Linux cd 命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的. 所以,学习Linux 常用命令,首先就要学好 cd 命令的使用方法技巧. 1.  ...

  8. systemd初始化进程(转)

    Systemd初始化进程 Linux操作系统开机过程首先从BIOS开始→进入"Boot Loader"→加载内核→内核的初始化→启动初始化进程,初始化进程作为系统第一个进程,它需要 ...

  9. mac 远程桌面提示: 证书或相关链无效

    左上角 RDC -->首选项-->安全性-->即使验证失败,也始终链接

  10. python_49_三种编程方式及面向过程与面向函数区别.py

    ''' 三种编程方式:1.面向对象 (类:class)2.面向过程 (过程:def)3.函数式编程(函数:def) 编程语言中函数的定义:函数是逻辑结构化和过程化的一种编程方法 过程与函数的区别,过程 ...