找规律!

求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
2
26
125
3125
9999
 
Sample Output
1
2
4
8
2
8
 

 
#include<stdio.h>
#include<string.h>
const int di[4] = { 6, 2, 4, 8};//这是2的次幂最后一位的循环;
const int pre[10] = { 1, 1, 2, 6, 4,2,2,4,2,8};//前十个数的最后一位;
int a[200], ls;
char s[200];
void tran( int ls )//转换 将个位放在a[0]处
{
for( int i =ls-1; i >= 0; i -- )
a[ls-i-1] = s[i]-'0';
}
void mult( )
{
int i, t=0;//t是借位;
for( i = ls-1; i >= 0; i -- )
{
int q = t*10+a[i];
a[i] = q/5;
t = q%5;
}
while( ls > 0&&a[ls-1] == 0 ) --ls;//排除后面的0 细致考虑一下
}
int la_no_num( )
{
if( ls == 1 ) return pre[a[0]]; //假设仅仅有一位直接输出或返回
int x1 = pre[a[0]%5]; //这是f(n%5)
mult( );
int x2 = di[(a[0]+a[1]*10)%4];//这是2^(n/5) 为什么仅仅算前两位(提示:同余定理)
int ans = (x1*x2*la_no_num())%10;//f(n)=((n%5)!* f(n/5) *2^(n/5))%10
return ans;
}
int main()
{
int la, i;
while( ~scanf( "%s", s ) )
{
ls = strlen(s);
tran(ls);
printf( "%d\n", la_no_num() );
} }

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. JS类型、值和变量 笔记

    非数字 JavaScript中的非数字值有一点特殊.他们和任何值都不相等,包括和本身也不相等. NaN == NaN => false NaN != NaN => true isNaN(N ...

  2. php中遇到include_path='.;C:\php5\pear'的错误

    所有面页,包括空白的都会报类似下面的错误. Warning: Unknown: failed to open stream: No such file or directory in Unknown  ...

  3. crontab 基本用法

    crontab格式:第1列分钟1-59第2列小时1-23(0表示子夜)第3列日1-31第4列月1-12第5列星期0-6(0表示星期天)第6列要运行的命令 还可以用一些特殊符号: *: 表示任何时刻 , ...

  4. 使用Autofac部署IIS6.0时未能加载文件或程序集“System.Core, Version=2.0.5.0...“

    错误信息 .net4.0项目中使用autofac这个IOC容器,在部署在win2003+iis6时出现以下错误. “/”应用程序中的服务器错误. --------------------------- ...

  5. MFC 之ActiveX控件学习

    本文将介绍ActiveX控件的应用与工作原理,读者可以把ActiveX控件看成一个极小服务器的应用程序,它不能独立运行,必须要嵌入到容器程序中与容器一起运行,就像电脑主机中的显卡,它自己在电脑硬件系统 ...

  6. TCP连接建立和关闭中的疑难点

    TCP连接建立和关闭中的疑难点 作者:夏语岚    撰写日期:2011-10-29 近日在阅读<Unix网络编程>,以前在<计算机网络>课程中学到TCP,当时只是简单了解了TC ...

  7. django中的事务管理

    在讲解之前首先来了解一下数据库中的事务. 什么是数据库中的事务? 热心网友回答: ():事务(Transaction)是并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不 ...

  8. win7+ubuntu双系统安装方法

    转自win7+ubuntu双系统安装方法 前段时间又安装一下win7+ubuntu双系统,过段时间就会忘记,这次自己写下来,以便以后查看. 1.      先准备一个分区来安装ubuntu.在win7 ...

  9. 直接将视频文件原码流转换成YUV,输出到屏幕显示

    #include "stdafx.h" #define inline _inline#ifndef INT64_C#define INT64_C(c) (c ## LL)#defi ...

  10. 嵌入式C语言头文件的建立与使用

    如何正确编写 C 语言头文件和与之相关联的 c 源程序文件,这首先就要了解它们的各自功能. 要理解 C 文件与头文件(即.h)有什么不同之处,首先需要弄明白编译器的工作过程. 一般说来编译器会做以下几 ...