找规律!

求N!最后非0位的值。比方2是120的最后一个不是0的值。

输入N比較大,要大数保存。

注意到最后0的个数是与5的因数的个数相等。设f(n)为n!的最后非0位。

那么f(n)=((n%5)!* f(n/5) *2^(n/5))%10

因数2的个数始终大于5,从1開始每连续5个划分为1组,当中5的倍数仅仅提取出一个因数5后,

组成一个新的数列1到n/5,我们有1*2*3*4*5=6*7*8*9*5=2(取最后一个非0位),这里就是2^(n/5)。

再乘上剩下来的几个数字就可以

(比方n是123,那么第一次会剩下121,122,123三个数没有被分配)。

比如:23 就能够变为 f(23) = ((3)! * f(4) * 2^(4))%10; f(4) = 4;

故f(23) = 4; 參考http://blog.csdn.net/yihuikang/article/details/7721875

Last non-zero Digit in N!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5908    Accepted Submission(s): 1471

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. 1
  2. 2
  3. 26
  4. 125
  5. 3125
  6. 9999
 
Sample Output
  1. 1
  2. 2
  3. 4
  4. 8
  5. 2
  6. 8
 

 
  1. #include<stdio.h>
  2. #include<string.h>
  3. const int di[4] = { 6, 2, 4, 8};//这是2的次幂最后一位的循环;
  4. const int pre[10] = { 1, 1, 2, 6, 4,2,2,4,2,8};//前十个数的最后一位;
  5. int a[200], ls;
  6. char s[200];
  7. void tran( int ls )//转换 将个位放在a[0]处
  8. {
  9. for( int i =ls-1; i >= 0; i -- )
  10. a[ls-i-1] = s[i]-'0';
  11. }
  12. void mult( )
  13. {
  14. int i, t=0;//t是借位;
  15. for( i = ls-1; i >= 0; i -- )
  16. {
  17. int q = t*10+a[i];
  18. a[i] = q/5;
  19. t = q%5;
  20. }
  21. while( ls > 0&&a[ls-1] == 0 ) --ls;//排除后面的0 细致考虑一下
  22. }
  23. int la_no_num( )
  24. {
  25. if( ls == 1 ) return pre[a[0]]; //假设仅仅有一位直接输出或返回
  26. int x1 = pre[a[0]%5]; //这是f(n%5)
  27. mult( );
  28. int x2 = di[(a[0]+a[1]*10)%4];//这是2^(n/5) 为什么仅仅算前两位(提示:同余定理)
  29. int ans = (x1*x2*la_no_num())%10;//f(n)=((n%5)!* f(n/5) *2^(n/5))%10
  30. return ans;
  31. }
  32. int main()
  33. {
  34. int la, i;
  35. while( ~scanf( "%s", s ) )
  36. {
  37. ls = strlen(s);
  38. tran(ls);
  39. printf( "%d\n", la_no_num() );
  40. }
  41.  
  42. }

hdoj Last non-zero Digit in N! 【数论】的更多相关文章

  1. 2018.09.17 atcoder Digit Sum(数论)

    传送门 数论好题啊. 首先对于b<=sqrt(n)b<=sqrt(n)b<=sqrt(n)的情况直接枚举b判断一下就行了. 下面谈一谈如何解决b>sqrt(n)b>sqr ...

  2. 【HDOJ】1061 Rightmost Digit

    这道题目可以手工打表,也可以机器打表,千万不能暴力解,会TLE. #include <stdio.h> #define MAXNUM 1000000001 ][]; int main() ...

  3. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  4. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  5. 数论 HDOJ 5407 CRB and Candies

    题目传送门 题意:求LCM (C(N,0),C(N,1),...,C(N,N)),LCM是最小公倍数的意思,C函数是组合数. 分析:先上出题人的解题报告 好吧,数论一点都不懂,只明白f (n + 1) ...

  6. POJ 1150 The Last Non-zero Digit 数论+容斥

    POJ 1150 The Last Non-zero Digit 数论+容斥 题目地址: id=1150" rel="nofollow" style="colo ...

  7. hdoj 1061 Rightmost Digit【快速幂求模】

    Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  8. HDOJ 1061 Rightmost Digit(循环问题)

    Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...

  9. HDOJ 1061 Rightmost Digit

    找出数学规律 原题: Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

随机推荐

  1. iOS: plist实例

    // // main.m // OSXDemo0601_plist // // Created by yao_yu on 14-6-3. // Copyright (c) 2014年 yao_yu. ...

  2. GCC编译器的安装

    1.GCC简介 GCC(GNU Compiler Collection)是一套功能强大.性能优越的编程语言编译器,它是GNU计划的代表作品之一.GCC是Linux平台下最常用的编译器,GCC原名为GN ...

  3. 机器视觉工具箱-Machine Vision Toolbox for Matlab

    发现了一个机器视觉的Matlab工具箱,分享一下. 机器视觉工具箱(MVT的)规定,在机器视觉和基于视觉的控制有益的多种功能.这是一个有点折衷收集反映作者在光度学,摄影测量,色度学 方面的个人利益.它 ...

  4. Uva_11021 Tribles

    题目链接 题意: 现在有k只麻球, 每只麻球只能存活一天, 第二天就会死去, 死去之前可能生下x只小麻球(x = 0,1,2,...,n  1), 概率分别为P[0], P[1], ... , P[n ...

  5. socket、tcp、http

    第一部分.概念的理解 1.什么是Socket? Socket又称之为“套接字”,是系统提供的用于网络通信的方法.它的实质并不是一种协议,没有规定计算机应当怎么样传递消息,只是给程序员提供了一个发送消息 ...

  6. 【Java】WEB-INF目录与META-INF目录的作用

    /WEB-INF/web.xml Web应用程序配置文件,描述了 servlet 和其他的应用组件配置及命名规则. /WEB-INF/classes/包含了站点所有用的 class 文件,包括 ser ...

  7. ThreadPoolExecutor介绍

    ThreadPoolExecutor的说明 ThreadPoolExecutor常见的操作主要有以下几个方法: getPoolSize():返回线程池实际的线程数. getActiveCount(): ...

  8. Junit4学习笔记

    一.初始化标注 在老Junit4提供了setUp()和tearDown(),在每个测试函数调用之前/后都会调用. @Before: Method annotated with @Before exec ...

  9. Delphi 版本号(D1到XE6),发现一个delphi.wikia.com网站

    Borland Compiler Conditional Defines  Edit  Talk1 2,909PAGES ONTHIS WIKI   Product Name Version Cond ...

  10. bzoj1560

    首先这种题目肯定是要先排序,以x为第一关键字,y为第二关键字不难想到O(n2)的dp,下面显然要优化不难发现,由于两点的耗费是坐标差的平方的和,不带根号,因此,不难发现一个很有用的性质,如果从A点能到 ...