[ 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 ...
随机推荐
- Oracle表空间 ORA-01653:
--1.查看表空间USERS使用情况SELECT T.TABLESPACE_NAME,D.FILE_NAME, D.AUTOEXTENSIBLE,D.BYTES,D.MAXBYTES,D.STATUS ...
- symfony 使用原始sql
$this->get('database_connection')->fetchAll('select * from book where book.id=3')
- HTTP请求方式之GET和POST比较
什么是HTTP协议 超文本传输协议(HyperText Transfer Protocol -- HTTP)是一个设计来使客户端和服务器顺利进行通讯的协议 HTTP在客户端和服务器之间以request ...
- 戴尔iDRAC服务器远程控制设置
对于远程的服务器,我们不能经常性的去机房维护,所以远程控制对于服务器来说就显得至关重要.那么你是用什么方式对服务器进行远程控制呢?远程桌面?还是KVM切换器?NO,你OUT了!如果你用的是戴尔的服务器 ...
- outlook pst 过大,要立刻处理
用鼠標右鍵點擊Email Account,選擇“資料檔案屬性” 進入內容,再點擊“資料夾大小”, “全部大小”不要大於20,480,000KB(20GB), 如果超過請立即分拆此pst檔案, 或者點擊 ...
- 使用11g DNFS建立基于DNFS的tablespace
使用11g DNFS建立基于DNFS的tablespace 參考自: Step by Step - Configure Direct NFS Client (DNFS) on Linux (11g) ...
- 小白学开发(iOS)OC_ 字符串的获取 (2015-08-11)
// // main.m // 字符串的获取 // // Created by admin on 15/8/13. // Copyright (c) 2015年 admin. All righ ...
- easyui中点击datagrid的分页刷新按钮,数据无法更新到最新状态
原因分析:点击刷新按钮是对当前页数据进行reload,因此所传的请求参数皆为上一次加载当前页的参数即datagrid的load方法中的参数,主要是因为请求的最终时间无法更新到最新状态 解决方案:对点击 ...
- VS2012 ASP.NET 母版页的创建与使用
在做牛腩新闻公布系统的过程中,须要使用ASP.NET的母版页来抽出全部网页的公共部分,以便更好的复用自己的网页布局和设计. 首先我们来看怎样创建一个新的母版页,例如以下图所看到的: 加入之后,例如以下 ...
- 同一台机子 打开两个tomcat
由于项目測试须要.须要在机子上单独搭建一个图片server,所以就要同一时候打开两个tomcat,这里记录方法. 1. 首先复制一个新的tomcat程序: 2.在新的tomcat程序中改动server ...