求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. 使用scrapy进行12306车票查询

    概述 通过12306的查询API进行查询某日火车票, 结果保存在csv文件中. 详细 代码下载:http://www.demodashi.com/demo/12623.html 一.环境搭建 1. 安 ...

  2. Java教程到处都是,究竟怎样能学好Java?

    学习Java如何入门?学习教程要点是什么?如何精通?做好以下这些点,入门更快,掌握Java更轻松. Java必备基础知识 1.你需要精通面向对象分析与设计(OOA/OOD).涉及模式(GOF,J2EE ...

  3. java基础讲解07-----数组

    1.什么是数组 2.怎么使用数组 package test; public class ShuZu {            public static void main(String[] args ...

  4. To Use Genymotion

    Chinese Site:http://www.genymotion.cn/ Offical Site:http://www.genymotion.com/  Not available in Chi ...

  5. TI博客文章-4-20mA电流环路发送器入门

    TI博客文章-4-20mA电流环路发送器入门http://bbs.21ic.com/forum.php?mod=viewthread&tid=1610834&fromuid=10995 ...

  6. setTime

    var getTime = function() { var _ = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09'], //补 ...

  7. python list.remove(),del()和filter &amp; lambda

    面试题之中的一个. 下面代码能执行吗? l = [1,2,3,4,5] for i in range(0,len(l)): print i if l[i] % 2 == 0: del l[i] pri ...

  8. PHP系统学习2

    字符串操作 字符串截取 substr() 字符串格式化printf()格式化无需echo sprintf() 需要echo nl2br()可以将\n转换成<br/> wordwrap() ...

  9. sdut Mountain Subsequences 2013年山东省第四届ACM大学生程序设计竞赛

    Mountain Subsequences 题目描述 Coco is a beautiful ACMer girl living in a very beautiful mountain. There ...

  10. Scrapy shell使用

    注意:容易出现403错误,实际爬取时不会出现. response - a Response object containing the last fetched page >>>re ...