\(\\\)

\(Description\)


求 \(N!\) 在 \(K\) 进制表示下末尾 \(0\) 的个数。

  • \(N,K\in [1,10^{12}]\)

\(\\\)

\(Solution\)


我又NC了

考虑何种情况\(K\)进制下会产生\(0\),可以类比十进制下的情况,发现\(2\)和\(5\)的因数各一个就会产生一个\(0\),这是因为\(10=2^1\times 5^1\)。类比的,我们将\(K\)分解质因数:

\[K=\prod_{i=1}^M p_i^{t_i}
\]

那么构成一个\(0\)的代价就是对于分解得到的每一个\(p_i\),消耗\(t_i\)个\(p_i\)。

然后对分解得到的每一个质因数求一下\(N!\)里含有多少个即可,这个套路很常见,每次加上\(N/p_i\),同时让\(N=N/p_i\)至\(N=0\)即可,加入统计出的\(N!\)里含有\(g_i\)个\(p_i\)。

\[ans=\min_{i=1}^M \{\lfloor\frac{g_i}{t_i}\rfloor\}
\]

\(\\\)

\(Code\)


#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1000010
#define R register
#define inf 9000000000000000ll
using namespace std;
typedef long long ll; ll n,k,tmp,res,ans=inf,fac[N],cnt[N]; int main(){
scanf("%lld%lld",&n,&k);
tmp=sqrt(k);
for(R ll i=2;i<=tmp;++i)
if(k%i==0){
fac[++fac[0]]=i;
while(k%i==0) ++cnt[fac[0]],k/=i;
}
if(k!=1) fac[++fac[0]]=k,cnt[fac[0]]=1;
for(R int i=1;i<=fac[0];++i){
tmp=n; res=0;
while(tmp) res+=tmp/fac[i],tmp/=fac[i];
ans=min(ans,res/cnt[i]);
}
printf("%lld\n",ans);
return 0;
}

[ Luogu 3927 ] Factorial的更多相关文章

  1. 【Luogu】P3927 SAC E#1 - 一道中档题 Factorial

    [题目]洛谷10月月赛R1 提高组 [题意]求n!在k进制下末尾0的个数,n<=1e18,k<=1e16. [题解]考虑10进制末尾0要考虑2和5,推广到k进制则将k分解质因数. 每个质因 ...

  2. [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  3. CodeForces 515C. Drazil and Factorial

    C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  4. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  5. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

  6. [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数

    LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes. 解法一: int trailing_zeros(int n) { ; while (n) { re ...

  7. [codeforces 516]A. Drazil and Factorial

    [codeforces 516]A. Drazil and Factorial 试题描述 Drazil is playing a math game with Varda. Let's define  ...

  8. LeetCode Factorial Trailing Zeroes

    原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...

  9. 【LeetCode】172. Factorial Trailing Zeroes

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...

随机推荐

  1. Ubuntu 16.04安装VirtualBox 5.1实现无缝模式

    个人电脑版的虚拟机推荐使用VirtualBox,因为其免费,比起VMware到处要找破解强得多,且最重要的一点是无缝模式,让其感觉不出再用两个操作系统. 下载: wget http://downloa ...

  2. 电脑无线WIFI怎么共享给手机

    点屏幕右下角的那个小电脑(网络)标志,里面有打开网络和共享中. 选左上角管理无线网络——选择添加. 选择创建临时网络——点击下一步——输入网络名称. 安全类型选择WEP——安全密钥为10位数字——然后 ...

  3. epoll 的accept , read, write

    http://www.ccvita.com/515.html 在一个非阻塞(fcntl)的socket上调用read/write函数, 返回EAGAIN或者EWOULDBLOCK(注: EAGAIN就 ...

  4. Mysql入门实战中

    前面一章主要解说了mysql的入门学习.包括数据库,表的管理,以及对数据的增删改,本章主要介绍mysql最重要的语句select的使用方法.将select的大部分使用方法进行分别解说. 全部代码下载( ...

  5. poj3511--A Simple Problem with Integers(线段树求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 60441   ...

  6. 中国第二代身份证验证js代码

    以下这部分代码截取自盛大的某个网页.详细我就不给url了.以下是相应的js代码: iW = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1); iSum ...

  7. Redis3.0--集群安装部署

    准备环境 操作系统:CentOS6.5  Redis3.0.0 192.168.3.154 192.168.3.158 192.168.3.160 192.168.3.162 一.安装 安装文件夹 / ...

  8. simple-framework(MaliSDK框架分析)

    simple-framework(Mali SDK框架分析) 1.全部的定义及实现放在同一个命名空间中.文件包括#include一般在命名空间的外面. 总的命名空间为namespace MaliSDK ...

  9. 十分简便的APK反编译(Mac 版本号 具体解释)

    之前參考了网上大神们介绍的apk for mac  的反编译的文章,里面写的十分具体而有用,可是因为apk for mac中反编译细节十分繁琐,过程也相对照较复杂,针对这个缺陷本人对其反编译的过程进行 ...

  10. iOS 保存视频AVAssetWriter

    错误的CMTime导致保存的视频无效,比如: frameTime CMTime 1122 600ths of a second value CMTimeValue 1122timescale CMTi ...