hdoj Last non-zero Digit in N! 【数论】

找规律!

求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! 【数论】,布布扣,bubuko.com

时间: 2024-08-08 05:21:08

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

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

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

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

HDOJ 1061 Rightmost Digit

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30543    Accepted Submission(s): 11624 Problem Description Given a positive integer N, you should output the most right digit of N

10162 - Last Digit (数论+周期规律)

UVA 10162 - Last Digit 题目链接 题意:求S=(11+22+...NN)%10 思路:打出0-9的每个周期,发现周期为1或2或4.所以S是以20一个周期,打出表后发现20为4,所以对应的40为8,60为2,80为6,100为0,100为1个周期,且为0,所以先把数字mod上100,然后在mod 20求出对应位置. 代码: #include <stdio.h> #include <string.h> const int Z2[10] = {0, 4, 8, 2

HDU 1060 Leftmost Digit (数论)

Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13680    Accepted Submission(s): 5239 Problem Description Given a positive integer N, you should output the leftmost digit of N^N.

hdoj 1492 The number of divisors(约数) about Humble Numbers 【数论】【质因子分解 求和】

定理:一个正整数 n 可以用素因子唯一表示为 p1^r1 * p2^r2 * ... pk^rk (其中 pi 为素数) , 那么这个数的因子的个数就是,(r1+1)*(r2+1)*...*(rk+1). 理解:为什么是加1之后再相乘,因为一个数的的因子数至少为1和他自身,但因为r1,r2..可以为0,所以因子的个数为(r1+1)... 拓展一下: 定理1: 一个正整数 n 可以用素因子唯一表示为 p1^r1 * p2^r2 * ... pk^rk (其中 pi 为素数) , 那么这个数的因子的

HDOJ 1163 Eddy&#39;s digital Roots(简单数论)

[思路]:http://blog.csdn.net/iamskying/article/details/4738838 求解思路: 现在分析一个问题,假设将十位数为a,个位数为b的一个整数表示为ab,则推导得 ab*ab = (a*10+b)*(a*10+b) = 100*a*a+10*2*a*b+b*b 根据上式可得:root(ab*ab) = a*a+2*a*b+b*b = (a+b)*(a+b);[公式一] 同理也可证得:root(ab*ab*ab) = (a+b)*(a+b)*(a+b)

uva 10162 - Last Digit(数论)

题目链接:uva 10162 - Last Digit 题目大意:给定n,求s的个位的数值是多少. 解题思路:对于ii,重复周期为20,这样就有 1 4 7 6 5 6 3 6 9 0 1 6 3 6 5 6 7 4 9 0 但是这个周期的值是不为0的,总的话是100为一个大周期. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxt =