求N个数阶乘末尾除0后的数值。

主要的难点在于要把这个N个数所含的2和5的队数去掉。

网上方法很多很好。 不多说

Last non-zero Digit in N!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5454    Accepted Submission(s): 1348

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
 
Recommend
JGShining
 
#pragma comment(linker, "/STACK:102400000,102400000") 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff int n;
int save[]={,,,,,,,,,};
char s[];
int d[];
int i,ti;
int tmp1;
int cnt; int dfs(int len)
{
int tmp=;
for(i=len;i>=;i--)
if(d[i]!=) break;
if(i<)
{
return ;
}
if(i==)
{
return save[d[]];
}
if(d[]%==) tmp=;
else tmp=; tmp = ( tmp*save[d[]] )%; ti=i;
for(;i>=;i--)
{
tmp1=d[i]%;
d[i]/=;
if(i!=)
d[i-]+=tmp1*; //将余数向下推
} return (tmp*dfs(ti))%;
} int main()
{
//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","w",stdout);
while(~scanf("%s",s))
{
//主要是将所有数2和5因子提取出来就可以了,剩下来的取最后一个数即可
//然后就是最后一位怎么看了,
int len=strlen(s);
memset(d,,sizeof(d));
cnt=;
for(int i=;i<len;i++)
d[len--i]=s[i]-'';
printf("%d\n",dfs(len-));
}
return ;
}

2015.11.20.。。

又走到这步。 今天看数论的是看不小心瞄到了这题,心想多年前都能做出来,现在怎么没什么想法了。

然后推了半天,自己想出一个解法。

我们可以发现,每乘5个数(1-5,或6-10),相当于乘2.(然后再往5^2 ,5^3...推)

然后就很好做了,把n装化为5进制,然后一下就可以出结果了。

留个代码纪念下。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std; char str[];
int num[];
int ans[]; int chg(char c)
{
return c-'';
} int main()
{
while( scanf("%s",str) != EOF )
{
int len=strlen(str);
for(int i=;i<len;i++)
num[i] = chg( str[i] );
int cnt=;
for(int i=;i<len;i++)
if(num[i]==) cnt++;
else break;
int wei=;
int from=,to=;
while( cnt < len )
{
//然后做一次除法
for(int i=cnt;i<len;i++)
{
num[ i+ ] += (num[i]%to)*from;
num[ i ] /= to;
}
ans[ wei++ ] = num[len]/from;
num[len]=;
for(int i=cnt;i<len;i++)
{
if(num[i]==) cnt++;
else break;
}
}
/*
for(int i=wei-1;i>=0;i--)
{
printf("%d",ans[i]);
}
printf("\n");
*/
int tmp = ;
int sum = ;
for(int i=;i<wei;i++)
{
int some=ans[i+]%==?:;
for(int j=+some;j<=ans[i]+some;j++)
{
sum = (sum*j*tmp);
sum = sum%;
}
tmp = tmp*;
tmp = tmp%;
}
printf("%d\n",sum);
}
return ;
}

hdu1066(经典题)的更多相关文章

  1. poj 1611:The Suspects(并查集,经典题)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 De ...

  2. Hihicoder 题目1 : Trie树(字典树,经典题)

    题目1 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编 ...

  3. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  4. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

  5. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  6. hdu 1247:Hat’s Words(字典树,经典题)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  8. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  9. poj 1006:Biorhythms(水题,经典题,中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Des ...

随机推荐

  1. java 状态对象

    package cn.itcast.hibernate.sh.state; import org.hibernate.Session; import org.hibernate.Transaction ...

  2. classloader常见问题总结

    h tp://yourenyouyu2008.iteye.com/blog/779707看到一些ClassNoFindException ,ClassCastException等异常首先应该想到是不是 ...

  3. mac 使用apache开启https功能,实现ios局域网内测(一)

    笔者尝试了网上各种办法最后还是找到了方法解决了一系列局域网内测的问题,随手做个笔记记录下,亲测可行. 一.先生成证书 1.进入apache web 的根目录处理证书命令 cd /Library/Web ...

  4. 弹出键盘windowsoftinputmode属性设置值

    windowSoftInputMode属性设置值 2012-08-30 16:49 1592人阅读 评论(0) 收藏 举报 androidattributes活动 (1).AndroidManifes ...

  5. STL容器分析--list

    就是一双向链表,可高效地进行插入删除元素.

  6. 1-wire单总线DS18B20

    要想实现单总线通信,每一个挂在总线上的从机必须拥有开路或3态输出.单总线DS18B20的DQ引脚用内部电路实现了开漏输出,其等效电路如下图: 当单片机IO引脚配置为 mcu IO引脚 电流流向 DS1 ...

  7. 论Top与ROW_NUMBER读取第一页的效率问题及拼接sql查询条件

    http://www.cnblogs.com/Leo_wl/p/4921799.html SELECT TOP * FROM users WHERE nID> And nID< ORDER ...

  8. log4cxx在linux下的编译使用

    最近在linux下使用log4cxx库,按照其官方文档提供的方法来进行编译,不能成功,又利用google搜索了好几个中文博客上讲述在linux下编译使用log4cxx库的方法,依然不能成功,在这里我奉 ...

  9. MYSQL备份与恢复精华篇

    数据备份原理 数据备份属于数据容灾保护中的内容,所有的数据备份系统设计都基于这五个元素,备份源.备份目标.传输网络.备份引擎和备份策略.用户按照需要制定备份策略,使用定时任务执行备份脚本,使用备份引擎 ...

  10. ActiveMQ从源代码构建

    众多开源项目.我们一般都是直接拿过来用之而后快. 只是我们也应该知道这些项目是怎样从源代码构建而来的. 既然代码是写出来的,就不能避免有BUG存在,话说没有完美的软件,也没有无漏洞的程序. 事实上从源 ...