[ Luogu 3927 ] Factorial
\(\\\)
\(Description\)
求 \(N!\) 在 \(K\) 进制表示下末尾 \(0\) 的个数。
- \(N,K\in [1,10^{12}]\)
\(\\\)
\(Solution\)
我又NC了
考虑何种情况\(K\)进制下会产生\(0\),可以类比十进制下的情况,发现\(2\)和\(5\)的因数各一个就会产生一个\(0\),这是因为\(10=2^1\times 5^1\)。类比的,我们将\(K\)分解质因数:
\]
那么构成一个\(0\)的代价就是对于分解得到的每一个\(p_i\),消耗\(t_i\)个\(p_i\)。
然后对分解得到的每一个质因数求一下\(N!\)里含有多少个即可,这个套路很常见,每次加上\(N/p_i\),同时让\(N=N/p_i\)至\(N=0\)即可,加入统计出的\(N!\)里含有\(g_i\)个\(p_i\)。
\]
\(\\\)
\(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的更多相关文章
- 【Luogu】P3927 SAC E#1 - 一道中档题 Factorial
[题目]洛谷10月月赛R1 提高组 [题意]求n!在k进制下末尾0的个数,n<=1e18,k<=1e16. [题解]考虑10进制末尾0要考虑2和5,推广到k进制则将k分解质因数. 每个质因 ...
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- CodeForces 515C. Drazil and Factorial
C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Luogu 魔法学院杯-第二弹(萌新的第一法blog)
虽然有点久远 还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题 沉迷游戏,伤感情 #include <queue> ...
- luogu p1268 树的重量——构造,真正考验编程能力
题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...
- [CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数
LeetCode上的原题,讲解请参见我之前的博客Factorial Trailing Zeroes. 解法一: int trailing_zeros(int n) { ; while (n) { re ...
- [codeforces 516]A. Drazil and Factorial
[codeforces 516]A. Drazil and Factorial 试题描述 Drazil is playing a math game with Varda. Let's define ...
- LeetCode Factorial Trailing Zeroes
原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...
- 【LeetCode】172. Factorial Trailing Zeroes
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...
随机推荐
- reader dc
https://get.adobe.com/cn/reader/otherversions/
- SQL PATINDEX检索
语法格式:PATINDEX ( '%pattern%' , expression ) 返回pattern字符串在表达式expression里第一次出现的位置,起始值从1开始算. pattern字符串在 ...
- 一步步搭建java信息管理系统00 - 前言
开发前,先上效果图吧 信息管理系统,个人认为,以下几个因素是不可缺少的 多tab 因菜单比较多,右侧的树形一定要考虑,如果菜单还是多,那么顶部就要考虑起来了 以后想到什么,再添加吧. 看到easyui ...
- JS获取地址栏并拼接參数
比方地址栏是这种:http://www.aa.com/detail.aspx?code=1&start=2014-12-01&end=2014-12-23&name=abc 要 ...
- LinQ开篇介绍
语言集成查询(LINQ)是 Visual Studio2008中引入的一组功能. 可为 C# 和 Visual Basic 语言语法提供强大的查询功能. LINQ引入了标准易学的数据查询和更新模式,能 ...
- C语言++a与a++的实现机制与操作符结合优先级
看到一道"经典Linux C"面试题,关于左值和右值的. 华为笔试题 1.写出推断ABCD四个表达式的是否正确, 若正确, 写出经过表达式中 a的值(3分) int a = 4; ...
- javascript下将字符类型转换成布尔值
办不到!孙子,我告诉你,这办不到! 比如说, var sb = "false"; alert(!!sb);//结果显示true! 事前是想得很美的: sb == "fal ...
- JavaScript基础 -- ECMAscript
ECMAScript是一种由Ecma国际(前身为欧洲计算机制造商协会,英文名称是European Computer Manufacturers Association)通过ECMA-262标准化的脚本 ...
- 利用函数计算构建微信小程序的Server端
10分钟上线 - 利用函数计算构建微信小程序的Server端-博客-云栖社区-阿里云 https://yq.aliyun.com/articles/435430 函数计算 读写 oss import ...
- exception log
except Exception as e: l = [str(i) for i in [dbid, f_mp3, e]] log_s = '||'.join(l) logging.exception ...