\(\\\)

\(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安装基于nethogs衍生的网络监控软件(应用实时网速监控)

    基于nethogs衍生的网络监控软件有如下所列举的: nettop显示数据包类型,按数据包的大小或数量排序. ettercap是以太网的网络嗅探器/拦截器/记录器 darkstat通过主机,协议等方式 ...

  2. how to read openstack code: Neutron architecture

    今天这一章节非常重要.我们知道neutron是一个非常复杂的系统,由很多组件构成.研究这样一个复杂的系统,正确的顺序应该是现在宏观上对其整体结构有所了解,然后再由针对性的对其组件进行深入了解.本章要做 ...

  3. Moravec算子

    Moravec在1981年提出了Moravec角点检測算子,并将它应用于立体匹配.它是一种基于灰度方差的角点检測方法.该算子计算图像中某个像素点沿着水平.垂直.对角线.反对角线四个方向的灰度方差,当中 ...

  4. Yarn 的工作流-创建一个新项目

    Microsoft Windows [版本 10.0.16299.125] (c) Microsoft Corporation.保留所有权利. C:\Users\Administrator>cd ...

  5. IDM百度云使用

    2018-8-6 (idm百度云速度很慢) Tips:如果感觉图片较小,可以ctrl+鼠标滚轮放大网页 首先下载IDM绿色版. 解压后:右键以管理员权限运行进行绿化 最后,它会在所有支持的浏览器上安装 ...

  6. 2015 Multi-University Training Contest 9-1007 Travelling Salesman Problem

    Problem Description Teacher Mai is in a maze with n rows and m columns. There is a non-negative numb ...

  7. 图像处理之基础---滤波器之高斯低通滤波器3c代码实现yuv,rgb

    ()高斯理论简介 () ()代码实现 四 使用高斯滤波器进行图像的平滑 ()高斯简介 http://research.microsoft.com/en-us/um/people/kahe/eccv10 ...

  8. 【Espruino】NO.13 蓝牙模块

    http://blog.csdn.net/qwert1213131/article/details/31830809 本文属于个人理解,能力有限.纰漏在所难免,还望指正! [小鱼有点电] [Espru ...

  9. 使用qemu

    1 获取qemu启动linux kernel的log qemu-system-x86_64 -nographic -kernel xxx -initrd xxx -append "conso ...

  10. selenium iframe 定位 qq空间说说

    selenium iframe 定位  qq空间说说