当前位置:网站首页>NEFU118 n!后面有多少个0【算术基本定理】
NEFU118 n!后面有多少个0【算术基本定理】
2022-07-27 13:50:00 【51CTO】
题目链接:
http://acm.nefu.edu.cn/JudgeOnline/problemshow.php?problem_id=118
题目大意:
问:计算N!末尾0的个数。(1 <= N <= 1000000000)。
思路:
N是100000000规模的数,直接计算结果,再统计0的个数显然不科学。将末尾0分解为2*5。
每一个0必然和一个因子5对应,但是一个数的因式分解中一个因子5不一定对应一个0。因为
还需要一个因子2,才能实现一一对应。
对于N!,在因式分解中,因子2的个数明显大于因子5的个数。所以如果存在一个因子5,那么
必然对应着N!末尾的一个0。这道题就变为了求N!中因子5的个数。由算术基本定理的性质(5)
可知:N!在素因子分解中素数p的幂为[N/p] + [N/p^2] + [N/p^3] + …,可以直接循环计算。
此时,5的幂就是N!中因子5的个数,也就是N!末尾0的个数。
附算术基本定理的定义和性质:
定理:
每个大于1的正整数N都可以被唯一地写成素数的乘积,在乘积中的素因子按照非降序排列。
正整数N的分解式 N = p1^α1 * p2^α2 * p3^α3 * … * pk^αk 称为N的标准分解式,其中
p1,p2,…,pk是素数,p1 < p2 < p3 < … < pk,且α1,α2,α3,…,αk是正整数。
性质:
(1)若N的标准素因子分解表达式为:N = p1^α1 * p2^α2 * p3^α3 * … * pk^αk,设d(N)
为N的挣银子个数, Φ(N)为N的所有因子之和,则有
d(N) = (α1 + 1) * (α2 + 1) * (α3 + 1) * … * (αk + 1)
Φ(N) = ( p1^(α1 + 1) )/(p1 - 1) * ( p2^(α2 + 1) )/(p2 - 1) * … * ( pk^(αk + 1) )/(pk - 1)
(2)设a = α1 * p2^α2 * … * pk^αk,b = p1^ β1 * p2^β2 * … * pk^βk,则有
gcd(a,b) = p1^min(α1,β1) * p2^min(α2,β2) * … * pk^min(αk,βk)
lcm(a,b) = p1^max(α1,β1) * p2^max(α2,β2) * … * pk^max(αk,βk)
(3)如果a和b为实数,则
max( gcd(a,b) ) + min( gcd(a,b) ) = a + b
(4)如果a和b是正整数,则
lcm(a,b) = a*b/gcd(a,b)
(5)N!的素因子分解中的素数p的幂为
[N/p] + [N/p^2] + [N/p^3] + …
AC代码:
边栏推荐
- [cache series] completely solve the problems of cache avalanche, breakdown and penetration
- 面试官问:如何判断一个元素是否在可视区域?
- 通过VN1630/VN7640的I/O功能来确认电源设置电压的时间精确度
- Navicate reports an error access violation at address 00000000
- lc marathon 7.26
- What you want most is the most comprehensive summary of C language knowledge. Don't hurry to learn
- Timestamp of AAC, h264, etc
- How to help enterprises optimize office management
- Interprocess communication
- Slam overview Reading Note 7: visual and visual intangible slam: state of the art, classification, and empirical 2021
猜你喜欢
随机推荐
Summary of basic knowledge of C language
What is the execution method of the stand-alone parallel query of PostgreSQL?
Hard deduction SQL statement exercises, wrong question records
Get the data of the first frame of unity's open camera
idea打jar包与引入jar包
PROFINET simulator tutorial
如何帮助企业优化Office管理
2022牛客多校二_ E I
Toward fast, flexible, and robust low light image enhancement cvpr2022
Research on multi label patent classification based on pre training model
PROFINET 模拟器使用教程
国信证券手机开户安全吗 中山证券靠谱吗
一文搞懂 Redis 架构演化之路
How to do well in enterprise system vulnerability assessment
Slam overview Reading Note 7: visual and visual intangible slam: state of the art, classification, and empirical 2021
SLAM综述阅读笔记七:Visual and Visual-Inertial SLAM: State of the Art, Classification,and Experimental 2021
Slam overview Reading Note 6: slam research based on image semantics: application-oriented solutions for autonomous navigation of mobile robots 2020
Is there a regular and safe account opening platform for gold speculation
解气!哈工大被禁用MATLAB后,国产工业软件霸气回击
Graphical SQL is too vivid








