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 ...
随机推荐
- eclipse无法启动
-startupplugins/org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar-showsplashorg.eclipse.platfo ...
- VS2010+OpenCV2.4.3配置
VS2010+OpenCV2.4.3配置: 环境变量path: D:\openCV2.4.3\opencv\build\x86\vc10\bin 项目-属性-VC++目录:(vs2008中,工具- ...
- Endianess(字节次序)简介
1. 基础 在解释Endianess前,需要先明白几个基础定义 1) 数据的高位与低位是什么 以1001001为例,则从左边算起是 高位 -> 低位, 简而言之就是左边是高位,右边是低位 而内存 ...
- ActiveMQ使用示例之Queue
我们使用ActiveMQ为大家实现一种点对点的消息模型. 开发时候,要将apache-activemq-5.12.0-bin.zip解压缩后里面的activemq-all-5.12.0.jar包加入到 ...
- [11] 楔形体(Wedge)图形的生成算法
顶点数据的生成 bool YfBuildWedgeVertices ( Yreal width, Yreal length, Yreal height, YeOriginPose originPose ...
- Maximum Subarray leetcode java
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 你可能不知道的5 个强大的HTML5 API 函数
HTML5提供了一些非常强大的JavaScript和HTML API,来帮助开发者构建精彩的桌面和移动应用程序.本文将介绍5个新型的API,希望对你的开发工作有所帮助. 1. 全屏API(Fulls ...
- 【Hadoop】Combiner的本质是迷你的reducer,不能随意使用
问题提出: 众所周知,Hadoop框架使用Mapper将数据处理成一个<key,value>键值对,再网络节点间对其进行整理(shuffle),然后使用Reducer处理数据并进行最终输出 ...
- HDU 2825 Wireless Password【AC自动机+DP】
给m个单词,由这m个单词组成的一个新单词(两个单词可以重叠包含)长度为n,且新单词中包含的基本单词数目不少于k个.问这样的新单词共有多少个? m很小,用二进制表示新单词中包含基本单词的情况. 用m个单 ...
- idea丢失svn解决办法
今天打开Idea,习惯用ctrl+t来更新svn,杯具出现了,快捷键失效了,我觉得可能是其他的什么软件占用了这个快捷键,于是把qq,微信,rtx,各种软件都关掉,发现还是不好使,于是重启了一下,发现还 ...