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 ...
随机推荐
- uestc 360(区间合并)
题意:有一个长度为n的序列.然后有两种操作,Q a b是输出区间a b内最长上升子序列的长度.A a b c是把区间a b内全部数字加上c. 题解:用线段树维护区间的最长上升子序列长度,那么一个区间的 ...
- 基于jQuery的Cookie操作插件--简单而又没有兼容性问题!
在网页客户端,我们经常会遇到读取或者设置cookie的情况,如果用纯生的js我们可能会遇到一些兼容性带来的麻烦,这里给大家介绍一个比较实用jquery操作cookie的插件,插件的源代码如下: 1 2 ...
- vue路由使用踩坑点:当动态路由再使用路由name去匹配跳转时总是跳转到根路由的问题
闲话少说,直接问题: 之前我的路由时这么写的 { path:'/serverInfo/:id', name:'serverInfo', component:() => import('@/vie ...
- 关于一道JS面试题的思考
题目: ; i < ; i++) { setTimeout(function() { console.log(new Date, i); }, ); } console.log(new Date ...
- 理解JavaScript私有作用域
私有作用域:跟外界的变量方法毫不冲突,豪无关系 var str ="javascript"; (function(){ alert(str); //undefined var st ...
- android 中的 window,view,activity具体关系
通过讨论这个问题,我们能够见识到google是对面向对象模式的理解,能够理解android底层的一些调用.这也是一道很常见的面试题. 我们这篇文章就来解决这四个问题: Android 中view的显 ...
- 【ES】elasticsearch学习笔记
ES学习 1 优势 1.1 简单 1.1.1 相比Solor配置部署等非常简单 1.2 高效 1.2.1 ES使用Netty作为内部RPC框架,Solor使用Jetty 1.3 插件化 1.3.1 E ...
- WPF DataTrigger的两个用法
1.用在textbox等输入控件上,验证输入是否合法.首先定义一个Converter, 2.Xaml里面这样子写,意思是输入的数字,如果小于100则显示为红色. 3.combox等列表控件里面,也可以 ...
- ECMAScript5之Object学习笔记(二)
继续第二部分 Object.freeze(obj) 看字面意思就是“把一个对象冻结”. 下面我们来看个简单的例子以作说明: // a person instance var person = { na ...
- 微信小程序中用户唯一ID的获取
折腾到半夜,搞得挺兴奋,总结一下,免得忘了: 1.微信小程序直接获得的是一些简单信息,基本无用 2.用户唯一标识是openid,还有一个unionid是关联多个公众号之类情况下用,我不大关心 3.在g ...