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

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
 
Source
经过细致的观察,发现n!的阶乘,要求其最后一位非0,便是要去掉所有的0 ...比如
6!=720..
我们在循环的时候,只需要取其长度取摸就可以了,ans%strlen(itoa(6));
代码如下.
 #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!的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [Leetcode] Number of Digit Ones

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  4. 【Codeforces715C&716E】Digit Tree 数学 + 点分治

    C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...

  5. kaggle实战记录 =>Digit Recognizer

    date:2016-09-13 今天开始注册了kaggle,从digit recognizer开始学习, 由于是第一个案例对于整个流程目前我还不够了解,首先了解大神是怎么运行怎么构思,然后模仿.这样的 ...

  6. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  7. [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_ ...

  8. 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 ...

  9. POJ3187Backward Digit Sums[杨辉三角]

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6350   Accepted: 36 ...

  10. Number of Digit One

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

随机推荐

  1. phpExcel导出文件时内存溢出的问题

    在使用PHPExcel导出文件时,经常会因为文件过大导致PHP内存溢出报错,为了解决这个问题,可以使用PHPExcel提供的参数进行优化.这里说的Excel文件过大并不一定是文件大小,更关键的在于文件 ...

  2. strcmp实现

    #include<stdio.h> #include<assert.h> int my_strcmp(const char*a,const char*b) { while(*a ...

  3. UCN(User-Centric Networks,用户中心网络)

    UCN(User-Centric Networks,以用户为中心的网络)是下一代移动通信网络(5G)的发展方向,目前尚处于初级发展阶段.2016年11月,IEEE SDN研究组(聚焦研发SDN.NFV ...

  4. 如何读取抓取的wifi包内容

    有密码的WIFI,WIFI的密码会生成一个临时会话密钥,这个临时会话密钥可以用来加密会话内容,也就是说.比如你在浏览网页,用有密码的WIFI,连接上以后,浏览的网页流量是加密了的,所以更安全.无密码的 ...

  5. vijos p1729 Knights

    描述 在一个N*N的正方形棋盘上,放置了一些骑士.我们将棋盘的行用1开始的N个自然数标记,将列用'A'开始的N个大写英文字母标记.举个例子来说,一个标准的8*8的国际象棋棋盘的行标记为1..8,列标记 ...

  6. 混沌数学之ASin模型

    相关软件:混沌数学之离散点集图形DEMO 相关代码: class ASinEquation : public DiscreteEquation { public: ASinEquation() { m ...

  7. Insert Interval leetcode java

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  8. Realm Swift

    Realm Swift 当前这个翻译,主要是方便我自己查阅api,有非常多地方写的比較晦涩或者没有翻译,敬请谅解 version 0.98.7 官方文档 參考文献 Realm支持类型 String,N ...

  9. 手机WiFi万能钥匙查看破解的password和手机查询命令收集

    手机须要网络利用WiFi万能钥匙破解了WIFI的password.手机就能够上网了,但假设想在电脑上使用手机破解的Wifi热点上网就须要password,此时须要知道手机破解的password,WiF ...

  10. Transformer中引用iqd作为数据源的时候数据预览出现乱码

    在cognos开发利用transform建模的过程中导入iqd数据源预览乱码问题,下面先描述一下环境 操作系统版本: [root@enfo212 ~]# cat /proc/version Linux ...