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 ...
随机推荐
- java环境配置错误集锦
eclipse生成的文件目录 D:\eeworkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps 1.java. ...
- 《精通Ext JS 》
<精通Ext JS > 基本信息 原书名:Mastering Ext JS 作者: (巴西)Loiane Groner 译者: 卢俊祥 丛书名: 图灵程序设计丛书 出版社:人民邮电出版社 ...
- #line 的作用是改变当前行数和文件名称
#line 的作用是改变当前行数和文件名称,它们是在编译程序中预先定义的标识符命令的基本形式如下: #line number["filename"]其中[]内的文件名可以省略. ...
- Objective-C:分类(Category、extension)
分类(Category .Extension) (一)分类的划分 (2) 1.(命名的类别)类别Category:只能添加新的方法,不能添加新变量. 2.(未命名的类别)类 ...
- WampServer和phpStorm的用法
WampServer的安装 修改默认的浏览器和文本编辑器 phpStore创建一个PHP工程 在phpStore中运行我们的项目 配制一个PHP Script运行环境 配制一个PHP Web Page ...
- Cognos第三方权限认证Oracle用户库
一:概要描述 1.1:项目背景 Cognos具有强大的报表功能,但是却没有提供一个完善的用户管理体系,针对商业智能系统对数据的安全性要求,我们必须实现不同用户对不同数据的访问,确保企业级以及部门级的数 ...
- JVM总结-内存监视手段及各区域内存溢出解决
转载:https://blog.csdn.net/xuqu_volition/article/details/53786096 引言 本文仅关注一些常见的虚拟机内存监视手段,以及JVM运行时数据区各个 ...
- Discuz常见小问题-如何实现word文档转成帖子
有一些网站比如QQ空间是可以直接导入Word文件生成网页版本的,但是效果不理想 可以发现图片进来之后都是变形了的 最笨的方法是一个一个复制粘贴(当然也不需要这么麻烦,你可以打开一个word文档之后,保 ...
- Memcached--分布式缓存安装教程
Memcached的Windows版本在这里下载http://code.google.com/p/memcached/wiki/PlatformWindows(或http://memcachedpro ...
- Python面向对象编程 - 一个记事本程序范例(一)
notebook.py import datetime last_id = 0 class Note: '''Represent a note in the notebook. Match again ...