求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. UIScrollerView当前显示3张图

    代码地址如下:http://www.demodashi.com/demo/11173.html WSLScrollView 功能描述:这是在继承UIView的基础上利用UIScrollerView进行 ...

  2. linux磁盘满时,如何定位并删除文件

    原文链接: http://www.cnblogs.com/yinxiangpei/articles/4211743.html @1.一般情况   一般情况下先df看一下,然后cd到要满的盘,执行: d ...

  3. font-size<12 chrome不支持解决

    今天,群里有人问道font-size<12 chrome不支持的问题.说实话,这个我一直都没留意过,正好借个机会给自己补习一下. 自己亲自试过,确实如此,当font-size<12 chr ...

  4. php中数据类型自动转换

    1.1 转为布尔型(即返回值为0) 空字符串''或"" 数字0或0.0 字符'0'或"0" 空值NULL 没有成员的数组    其余都转换成布尔型true,包含 ...

  5. Bootstrap手动打开隐藏模态框

    开发的时候遇到一个bug,关闭模态框后背景灰色图层依旧还在.原来是用错了隐藏模态框的代码. 正确的调用方式如下: $("#id").modal("show"); ...

  6. SQL SERVER 2005中利用XML对字符串拆分的方法

    1.常规方法(可运用于SQL SERVER 2000中) DECLARE @str varchar(1000) DECLARE @idoc int; DECLARE @doc xml;set @str ...

  7. redis windows 下安装及使用

    1.下载redis https://github.com/MSOpenTech/redis 2.解压下载的文档,比如D:\devSoft\redis-2.8.19 redis-benchmark.ex ...

  8. JVM虚拟机(二):堆、栈、方法区概念区别

    Java 堆 Java堆是和Java应用程序关系最密切的内存空间,几乎所有的对象都放在其中,并且Java堆完全是自动化管理,通过垃圾收集机制,垃圾对象会自动清理,不需自己去释放. 根据垃圾回收机制的不 ...

  9. c语言中数组名和指针变量的区别

    编译器工作原理:在64位的计算机中,当创建一个指针变量时,计算机会为它分配8个字节的存储空间.但如果创建的是数组呢?计算机会为数组分配存储空间,但不会为数组变量分配任何空间,编译器仅在出现它的地方把它 ...

  10. IE首页被篡改(手动修复)

    所谓手动修复,即以不变应万变,修改注册表,其实工具软件也只是帮你代工而已. win + R 打开 “运行”,键入 “regedit.exe” 打开注册表编辑器,在 路径 HKEY_LOCAL_MACH ...