HDUOJ-----1066Last non-zero Digit in N!
Last non-zero Digit in N!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5596 Accepted Submission(s): 1382
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.
#include<stdio.h>
int main()
{
int n,i;
_int64 ans;
while(scanf("%d",&n)!=EOF)
{
ans=;
for(i=;i<=n;i++)
{
ans*=i;
while((ans%)==) ans/=;
ans%=;
}
while((ans%)==) ans/=;
ans%=;
printf("%I64d\n",ans);
}
return ;
}
代码精简,但是复杂度为O(n)。。。提交的时候果断的tle了,爱,好忧伤呀~~~!,后来想了想,能否将其优化勒!
代码:
#include<stdio.h>
#include<string.h>
#define maxn 1000
const int mod[]={,,,,,,,,,,,,,,,,,,,};
char str[maxn];
int a[maxn];
int main()
{
int len,i,c,ret;
while(scanf("%s",str)!=EOF)
{
len=strlen(str);
ret=;
if(len==) printf("%d\n",mod[str[]-'']);
else
{
for(i=;i<len;i++)
a[i]=str[len--i]-''; //将其转化为数字以大数的形式
for( ; len>; len-=!a[len-])
{
ret=ret*mod[a[]%*+a[]]%;
for(c=, i=len- ;i>=;i--)
{
c=c*+a[i];
a[i]=c/;
c%=;
}
}
printf("%d\n",ret+ret%*);
}
}
return ;
}
HDUOJ-----1066Last non-zero Digit in N!的更多相关文章
- [LeetCode] Nth Digit 第N位
Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...
- [LeetCode] Number of Digit One 数字1的个数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- [Leetcode] Number of Digit Ones
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- 【Codeforces715C&716E】Digit Tree 数学 + 点分治
C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...
- kaggle实战记录 =>Digit Recognizer
date:2016-09-13 今天开始注册了kaggle,从digit recognizer开始学习, 由于是第一个案例对于整个流程目前我还不够了解,首先了解大神是怎么运行怎么构思,然后模仿.这样的 ...
- hduoj 1455 && uva 243 E - Sticks
http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...
- [UCSD白板题] The Last Digit of a Large Fibonacci Number
Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...
- 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 (Jav ...
- POJ3187Backward Digit Sums[杨辉三角]
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6350 Accepted: 36 ...
- Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
随机推荐
- mac securecrt无法记住密码的解决方法
打开secureCRT,菜单preferences--general,找到mac options.然后去掉Use KeyChain选项,这样每次连接服务器后就会自动保存密码了.不同的版本可能这个选项的 ...
- [11] 楔形体(Wedge)图形的生成算法
顶点数据的生成 bool YfBuildWedgeVertices ( Yreal width, Yreal length, Yreal height, YeOriginPose originPose ...
- 第六章 memcached剖析
注:本篇博客参考于两本书. <memcached全面剖析>,该书籍市面上应该没有,我传到了百度云盘,链接如下:http://pan.baidu.com/s/1qX00Lti <大型网 ...
- Pow(x,n) leetcode java
题目: Implement pow(x, n). 题解: pow(x,n)就是求x的n次方.x的N次方可以看做:x^n = x^(n/2)*x^(n/2)*x^(n%2).所以利用递归求解,当n==1 ...
- smtp发送带附件的邮件(直接将string类型结果保存为附件)
该方式直接保存为HTML文件,也可以是文本文件,其它格式效果不是很好 MailMessage mmsg = new MailMessage(); mmsg.Subject = " ...
- 浅谈jQuery easyui datagrid操作单元格样式
今天项目上遇到问题,就是表格风格统一的问题,由于用了2个不同的框架,所以如果要大修比较麻烦,考虑到修改表格样式工作量会少很多,所以考虑修改jQuery easyui datagrid数据网格的样式. ...
- CSS深入了解border:利用border画三角形等图形
三角形实际上是border的产物 我们正常使用的border都是四边一个颜色,当我们把四边换上不同颜色 那么你就会发现,三角来了~! <!DOCTYPE html> <html la ...
- 协定须要双工,可是绑定“WSHttpBinding”不支持它或者因配置不对而无法支持它
协定须要双工,可是绑定"WSHttpBinding"不支持它或者因配置不对而无法支持它 下面两种情况,我都遇到过. 一, < endpoint address =" ...
- WordPress 后台评论如何自定义搜索条件
大家都知道WordPress 作为一个非常成熟的博客系统,功能可以说是非常强大,几乎整个网站都可以进行定制开发,已经不算是一个博客系统了而应该是一个成熟的开发框架 最近就用WP给客户开发了一个网站,但 ...
- ElasticSearch关闭重启命令
很多人学习elasticSearch都是自学,想百度一下如何重启es也是没有答案,我硬着头皮,算是琢磨出来了,借此写博,希望能帮助您. 1.如何关闭ES,elasticsearch关闭办法 1.使用h ...