数论:HDU1066-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
开始没有看清楚题目,不知道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!的更多相关文章
- POJ 1150 The Last Non-zero Digit 数论+容斥
POJ 1150 The Last Non-zero Digit 数论+容斥 题目地址: id=1150" rel="nofollow" style="colo ...
- hdoj Last non-zero Digit in N! 【数论】
找规律! 求N!最后非0位的值.比方2是120的最后一个不是0的值. 输入N比較大,要大数保存. 注意到最后0的个数是与5的因数的个数相等.设f(n)为n!的最后非0位. 那么f(n)=((n%5)! ...
- 2018.09.17 atcoder Digit Sum(数论)
传送门 数论好题啊. 首先对于b<=sqrt(n)b<=sqrt(n)b<=sqrt(n)的情况直接枚举b判断一下就行了. 下面谈一谈如何解决b>sqrt(n)b>sqr ...
- 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 ...
- HDU 1060 Leftmost Digit (数论,快速幂)
Given a positive integer N, you should output the leftmost digit of N^N. InputThe input contains se ...
- 10162 - Last Digit (数论+周期规律)
UVA 10162 - Last Digit 题目链接 题意:求S=(11+22+...NN)%10 思路:打出0-9的每一个周期,发现周期为1或2或4.所以S是以20一个周期,打出表后发现20为4. ...
- 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 ...
- HDU 1060 Leftmost Digit 基础数论
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1060 这道题运用的是数学方法. 假设S=n^n.两边同时取对数,得到lgS=nlgn.即有S=10 ...
- sdut 2165:Crack Mathmen(第二届山东省省赛原题,数论)
Crack Mathmen Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Since mathmen take securit ...
随机推荐
- Reactor Pattern and Non-blocking IO--reference
reference from:http://www.cs.bgu.ac.il/~spl051/Personal_material/Practical_sessions/Ps_12/ps12.html ...
- vue3.0学习笔记(二)
一.选择合适的ide 推荐使用vs code编辑器,界面清晰.使用方便,控制台功能很好用.webstorm也可以,看个人喜好. 二.ui框架选择 目前,pc端一般是选择element ui(饿了么), ...
- 从零开始的全栈工程师——js篇2.3
自加和自减 =赋值运算 他的顺序是从右往左 从后往前 var a=12; 声明一个变量并将12赋值给aa=a+2; 将a+2赋值给a简写a+=3; a=a+3a+=1; a++ 在自己原有的基础上加1 ...
- 学习笔记:《JavaScript高级程序设计》
第1章 JavaScript简介 1.一个完整的JavaScript实现应该由三部分组成:核心(ECMAScript),文档对象模型(DOM)和浏览器对象模型(BOM). 2.Web浏览器只是ECMA ...
- (2017.9.27) 自定义列表项 list-style 使用心得
今天给某公司做招聘专页.早上完成设计图,下午开始排版.页面套用了我之前做的某人才局的招聘页面,导航栏.banner 很快就出来了.这次内容里我有些地方用了列表,当然要用 <ul> < ...
- vue-cli建立的项目如何在手机端运行以及如何用charles来抓包
刚开始自己在config文件夹下的index.js中的dev下的host写成的是localhost,但是发现自己不能在手机端访问,并且也不可以在charles进行抓包处理,后来把localhost改成 ...
- Garmin APP开发之入门
Garmin开发-入门 先附上几个已经开发完成的app日历 up down 翻月 start 回到当前月(就差农历了) 秒表和定时器一体app界面比较简单,但是实用,长按菜单键可以切换秒表和定时器,有 ...
- sudo用户权限添加问题
现象:通过visudo或者vim /etc/sudoers文件添加用户权限后,该用户测试时依然需要输入密码解决:查看/etc/passwd用户id可能重复并且重复的uid排在该用户上面
- 飞塔NGFW-FortiGate-5.2(BYOL)
平台: FortiGate 类型: 虚拟机镜像 软件包: basic software FortiGate ips security UTM vpn 反垃圾邮件 网页过滤 服务优惠价: 按服务商许可协 ...
- org.hibernate.HibernateException: Unable to get the defa
今天整合SSH框架时出现了这个问题,以前一直没注意,在网上找了一下解决方案,找到了问题的解决方法,特记录如下: 1.原因:在中,javax.persistence.validation.mode默认情 ...