题目:
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. Poj 1743——Musical Theme——————【后缀数组,求最长不重叠重复子串长度】

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22499   Accepted: 7679 De ...

  2. 斗鱼扩展初识Chrome扩展(一)

    看斗鱼有些时间了,也写了不少辅助的js,但是昨天不小心把硬盘分区表搞没了,自己写了好久的代码不见了,DiskGenius 也没恢复成功,所以要重写一次,大家要引以为鉴,常备份代码,github是个不错 ...

  3. CI框架更新与删除

    $this->load->database();        // $query=$this->db->get('t_repayments');        // $res ...

  4. View模块

    一.应用场景 通过View的类注释,可知,Backbone.view是一个JS构造函数,与DOM中的某一块UI相对应,通过注册模型层数据的监听,可实现视图的自动渲染. Backbone.View模块也 ...

  5. eclipse调试(转)

    step into : 单步执行,遇到子函数就进入并且继续单步执行(F5) step over: 在单步执行时,在函数内遇到子函数时不会进入子函数内单步执行,而是将子函数整个执行完在停止,也就是把子函 ...

  6. js 数组操作常用方法

    push():在数组后面加入元素,并返回数组的长度: unshift():在数组前面就如元素,并返回数组的长度: pop():删除最后一个元素: var arr =[1,2,3,4,5] ; arr. ...

  7. 创建一个自己的GitHub,创建自己的开源项目

    作者是一个大学在读学生,自己在平时的学习中,GitHub上的开源项目给自己提供了很大的帮助.GitHub是目前使用最广泛的分布式项目管理软件,GitHub上面托管了许多非常优秀的开源项目.我觉得每一个 ...

  8. IIS 处理高并发

    1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools > Advanced Settings Queu ...

  9. 【Oracle】曾经的Oracle学习笔记(4-7)多表联合查询,子查询,动态条件查询

    一.多表联合查询 二.子查询 三.动态条件查询 LESSON 4 Displaying Data from Multiple Tables------------------------------- ...

  10. python3应用例子01(进度条)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import sysimport time def bar(num, total): rate = num / ...