Just the Facts
Time Limit: 2 Seconds Memory Limit: 65536 KB

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

问题链接:ZOJ1222 Just the Facts
问题简述
    计算阶乘n!的最后一个不为0的数字。
问题分析
    这个题与参考链接类似,但是是大数计算问题,需要特殊处理。计算原理就是乘的时候统计因子2的个数,然后每遇到一个5,就去掉一个,那么乘出来就没有最后的0了。然后在乘的时候只保留最后一位就可以了,最后把统计的2的个数乘回去。
程序说明:(略)
参考链接:POJ1604 UVA568 UVALive5499 Just the Facts【模除+打表】
题记:(略)

AC的C++语言程序如下:

/* ZOJ1222 Just the Facts */#include <iostream>
#include <cstdio>
#include <cstring>using namespace std;const int N = 1000;
char s[N];
int a[N];int lastdigit(char buf[])
{const int mod[20] = {1,1,2,6,4,2,2,4,2,8,4,4,8,4,6,8,8,6,8,2};int len = strlen(buf), ret = 1;for (int i = 0; i < len; i++)a[i] = buf[len -1 - i] - '0';while(len) {len -= !a[len - 1];ret = ret * mod[a[1] % 2 * 10 + a[0]] % 10;for(int j = 0, i = len - 1; i >= 0; i--)j = j * 10 + a[i], a[i] = j / 5, j %= 5;}return ret;
}int main()
{while (scanf("%s", s) != EOF)printf("%d\n", lastdigit(s));return 0;
}

ZOJ1222 Just the Facts【大数+模除】相关推荐

  1. HDU1066 Last non-zero Digit in N!【大数+模除】

    Last non-zero Digit in N! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...

  2. UVA10929 You can say 11【大数模除】

    Your job is, given a positive number N, determine if it is a multiple of eleven. Input The input is ...

  3. 基于蒙哥马利实现大数模密的算法的硬件实现

    基于大数蒙哥马利的模乘的硬件实现上一篇博客蒙哥马利基2的算法的Verilog 硬件实现(大数模乘) 有关的高基蒙哥马利算法的硬件实现可以私聊博主进行交流. 下面介绍基于蒙哥马利算法实现大数模幂的硬件v ...

  4. 蒙哥马利基2的Python算法实现(大数模乘)

    RSA的软件算法参考算法 下面介绍基于蒙哥马利的基2的Python算法实现,即大数模乘模乘的实现,易于硬件实现 #A #p=10669721913248017310606431714870563867 ...

  5. 蒙哥马利基2的算法的Verilog 硬件实现(大数模乘)

    基于蒙哥马利基2的算法的 的python 语言参考上一篇博客 蒙哥马利基2的Python算法实现(大数模乘) 下面提供基于蒙哥马利算法实现基2 的硬件verilog语言编写,提供给大家学习,参考. ` ...

  6. POJ1604 UVA568 UVALive5499 Just the Facts【模除+打表】

    Just the Facts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9631 Accepted: 5042 Descri ...

  7. 蒙哥马利大数模乘与RSA加密算法简介

    在网上看了一圈蒙哥马利模乘相关的博客,没有看到讲得比较清晰的,趁最近闲下来自己写一篇. 首选明确三个问题: 1.蒙哥马利模乘是什么? 蒙哥马利模乘是一种数学家蒙哥马利(Montgomery)提出的一种 ...

  8. Bailian2925 大整数的因子【模除】

    2925:大整数的因子 总时间限制: 1000ms 内存限制: 65536kB 描述 已知正整数k满足2<=k<=9,现给出长度最大为30位的十进制非负整数c,求所有能整除c的k. 输入 ...

  9. UVA10494 If We Were a Child Again【大数除法】

    "Oooooooooooooooh!     If I could do the easy mathematics like my school days!!     I can guara ...

最新文章

  1. python 线性回归_用Python实现线性回归算法
  2. automation服务器不能创建对象是什么意思_从运营商角度来考虑为什么NAT不能搭建服务器?...
  3. Nginx的Gzip模块配置指令(二)
  4. 17-Flutter移动电商实战-首页_楼层区域的编写
  5. ObjectArx创建自定义实体
  6. Python深入05 装饰器
  7. Linux下搭建mysql主从服务器
  8. sqlserver中日期保存及取值
  9. 软件测试-开始软件测试
  10. python 函数重载_python中有函数重载吗
  11. hibernate教程笔记4
  12. java string 去掉两边的引号_编写一个简单的java程序
  13. VBA入门--VBA基础知识和简单技能技巧
  14. 谷歌闹别扭期间女生创办“谷姐”网(cnblogs)
  15. 周礼键君:中国福建省建瓯市之《闽郡八音字典》
  16. ACCV 结果出来了,大家来晒一晒吧~
  17. opencv 全志_Banana Pi 开发板-【BPI-M2 Berry试用】论opencv3.3.0编译的那些坑(非成功版)-电路城论坛 - 电子工程师学习交流园地...
  18. Github的远程项目如何下载到本地
  19. 算法与数据结构入门一篇就搞定
  20. centos8 解决编译安装 nginx时遇到的报错

热门文章

  1. 技术篇-符号制作-标记符号制作
  2. jsp标签使用技巧整理
  3. 计算机网闸合同,网闸三大主流技术 -电脑资料
  4. tensorflow python2迁移python3_tensorflow在python2和python3上的安装教程
  5. python repusts模块_Python tslearn包_程序模块 - PyPI - Python中文网
  6. 服务端设置忽略更新_深入理解Kafka服务端之日志对象的读写数据流程
  7. 部署SpringBoot到阿里云
  8. C++ STL map 中insert函数返回值问题
  9. 剑指offer面试题68 - I. 二叉搜索树的最近公共祖先(递归)(二叉搜索树)
  10. MIPS之u-boot流程分析