求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. js 复制内容到粘贴板的兼容性

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs& ...

  2. Android Shell Cmd

    1. view android version: grep ro.build.version.sdk= system/build.prop getprop ro.build.version.relea ...

  3. XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)

    XML序列化   #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...

  4. NFS介绍

    一.NFS服务介绍 NFS是 Network File system的缩写 NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机 ...

  5. unity, Destroy注意事项

    Destroy不是立即发生作用,而是推迟到帧末,所以下面代码是错误的: void OnTriggerEnter(Collider other){   if (other.gameObject.tag  ...

  6. apache设置头

    Apache 及开启压缩及Header信息隐藏:http://centilinux.blog.51cto.com/1454781/792820

  7. 如何通过 AAR 形式集成 leakcanary-android 服务

    如何通过 AAR 形式集成 leakcanary-android 服务 如何通过在线引用的方式拿到所有相关的依赖项文件? #1.禁用 Gradle 离线模式 #2.按照文档要求添加项目依赖 #3.Sy ...

  8. 环境搭建基础知识2(sublime text3中配置verilog语法高亮)

    需求说明:Verilog设计 内容       :verilog开发环境搭建 来自       :时间的诗 1 软件下载 1.1 官方下载 地址http://www.sublimetext.com/3 ...

  9. C# Lpt 并口热敏小票打印机打印位图

    class LptControl { private string LptStr = "lpt1"; public LptControl(string l_LPT_Str) { L ...

  10. python学习之range()和xrange()

    在python2中,xrange()返回一个xrange对象,注意这个对象并不是生成器,也不是迭代器,但是是迭代对象. 而range()则返回列表对象. >>> range(10) ...