Trailing Loves (or L'oeufs?)
The number "zero" is called "love" (or "l'oeuf" to be precise, literally means "egg" in French), for example when denoting the zero score in a game of tennis.
Aki is fond of numbers, especially those with trailing zeros. For example, the number 9200 has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is.
However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers.
Given two integers n and b (in decimal notation), your task is to calculate the number of trailing zero digits in the b-ary (in the base/radix of b) representation of n! (factorial of n).
Input
The only line of the input contains two integers n and b (1≤n≤10^18 , 2<=b<=10^12
).
Output
Print an only integer — the number of trailing zero digits in the b-ary representation of n!
Examples
Input
6 9
Output
1
Input
38 11
Output
3
Input
5 2
Output
3
Input
5 10
Output
1
Note
In the first example, 6!(10)=720(10)=880(9).
In the third and fourth example, 5!(10)=120(10)=1111000(2).
The representation of the number x in the b-ary base is d1,d2,…,dk if x=d1bk−1+d2bk−2+…+dkb0, where di are integers and 0≤di≤b−1. For example, the number 720 from the first example is represented as 880(9) since 720=8⋅92+8⋅9+0⋅1.
思路:把b分解质因数,然后看对n!献出了多少贡献,即(n!%(a1^k+a2^k......)==0
我们需要去求k,就需要先把b分解,并且记录下它的质因子的指数数,然后用n进行迭代求,然后每次缩小一次指数,最后除本身的指数就ok了,注意minn开的一定要尽可能的大
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<cmath>
typedef long long ll;
using namespace std;
ll cnt=0;
ll num[4000005];
void primeFactor(ll n) {
while(n % 2 == 0) {
num[cnt++]=2;
n /= 2;
}
for(ll i = 3; i <= sqrt(n); i += 2) {
while(n % i == 0) {
num[cnt++]=i;
n /= i;
}
}
if(n > 2)
num[cnt++]=n;
}
int main() {
ll n,b;
ll ans;
ll sss;
scanf("%lld%lld",&n,&b);
primeFactor(b);
ll s;
ll ss;
ll k=1;
ll minn=999999999999999999;
for(int t=0; t<cnt; t++) {
if(num[t]!=num[t+1]) {
s=0;
ans=n;
ss=num[t];
while(ans>=ss) {
s+=(ans/ss);
ans/=ss;
}
minn=min(minn,s/k);
k=1;
} else {
k++;
}
// cout<<num[t]<<endl;
}
printf("%lld",minn);
return 0;
}
Trailing Loves (or L'oeufs?)的更多相关文章
- CF 1114 C. Trailing Loves (or L'oeufs?)
C. Trailing Loves (or L'oeufs?) 链接 题意: 问n!化成b进制后,末尾的0的个数. 分析: 考虑十进制的时候怎么求的,类比一下. 十进制转化b进制的过程中是不断mod ...
- CF#538(div 2) C. Trailing Loves (or L'oeufs?) 【经典数论 n!的素因子分解】
任意门:http://codeforces.com/contest/1114/problem/C C. Trailing Loves (or L'oeufs?) time limit per test ...
- C. Trailing Loves (or L'oeufs?) (质因数分解)
C. Trailing Loves (or L'oeufs?) 题目传送门 题意: 求n!在b进制下末尾有多少个0? 思路: 类比与5!在10进制下末尾0的个数是看2和5的个数,那么 原题就是看b进行 ...
- C. Trailing Loves (or L'oeufs?)
题目链接:http://codeforces.com/contest/1114/problem/C 题目大意:给你n和b,让你求n的阶乘,转换成b进制之后,有多少个后置零. 具体思路:首先看n和b,都 ...
- Codeforces Round #538 (Div. 2) C. Trailing Loves (or L'oeufs?) (分解质因数)
题目:http://codeforces.com/problemset/problem/1114/C 题意:给你n,m,让你求n!换算成m进制的末尾0的个数是多少(1<n<1e18 ...
- Trailing Loves (or L'oeufs?) CodeForces - 1114C (数论)
大意: 求n!在b进制下末尾0的个数 等价于求n!中有多少因子b, 素数分解一下, 再对求出所有素数的最小因子数就好了 ll n, b; vector<pli> A, res; void ...
- Codeforces - 1114C - Trailing Loves (or L'oeufs?) - 简单数论
https://codeforces.com/contest/1114/problem/C 很有趣的一道数论,很明显是要求能组成多少个基数. 可以分解质因数,然后统计各个质因数的个数. 比如8以内,有 ...
- 【Codeforces 1114C】Trailing Loves (or L'oeufs?)
[链接] 我是链接,点我呀:) [题意] 问你n!的b进制下末尾的0的个数 [题解] 证明:https://blog.csdn.net/qq_40679299/article/details/8116 ...
- Codeforces1114C Trailing Loves (or L'oeufs?)
链接:http://codeforces.com/problemset/problem/1114/C 题意:给定数字$n$和$b$,问$n!$在$b$进制下有多少后导零. 寒假好像写过这道题当时好像完 ...
随机推荐
- the install of mysql in Linux System
一.下载MySql 浏览器打开 https://www.mysql.com/downloads/mysql/#downloads 下载 我下载的版本是Red Hat 5 版本的 https://www ...
- 在Windows 8上安装SQL Server2012
SQL Server 2012 的安装方法跟2008差不多,基本上都是点击下一步,不过在安装的时候可能会进度条一直停留在“正在启动操作系统功能”NetFx3””处不动,出现这个问题的原因是在Windo ...
- c语言split的实现代码
我们知道在其他语言中有split函数可以把一个字符串按你自己想要的分隔符分割成多个字符串并以列表的形式返回.但是对于c语言来说,是没有这样一个函数接口可以直接调用的.但是有时候在项目工作中,又会用到这 ...
- Spring--入门第二天
一.通过工厂方法配置bean Spring 中有两种类型的 Bean, 一种是普通Bean, 另一种是工厂Bean, 即FactoryBean. 工厂 Bean 跟普通Bean不同, 其返回的对象不是 ...
- 编写高质量代码改善C#程序的157个建议——建议51:具有可释放字段的类型或拥有本机资源的类型应该是可释放的
建议51:具有可释放字段的类型或拥有本机资源的类型应该是可释放的 在建议50中,我们将C#中的类型分为:普通类型和继承了IDisposable接口的非普通类型.非普通类型除了包含那些托管资源的类型外, ...
- 编写高质量代码改善C#程序的157个建议——建议4: TryParse比Parse好
建议4: TryParse比Parse好 如果注意观察除string外的所有基元类型,会发现它们都有两个将字符串转型为本身的方法:Parse和TryParse.以类型double为例,这两个方法最简单 ...
- 开源IMS平台中间件Mobicents
下面内容来自百度百科 Mobicents 是一个高伸缩性.事件驱动的应用服务器.是一款专业的.开放源代码的 VoIP 中间件平台.Mobicents是首个采用JAIN SLEE标准的开放式源代码电信应 ...
- HackNine 避免在EditText中验证日期
1.概要: 为什么不直接为EditTText设置一个点击监听器,而非要使用Button呢? 答案是:使用Button更安全,因为用户无法修改Button的文本内容.如果使用EditTex ...
- select2的搜索框不能输入搜索内容
按照select2官网配置完后,搜索框弹出后无法输入内容,究竟怎么回事,于是在其他页面尝试了select2,发现可以啊,为什么在这个地方不可以,终于找到了造成这个问题的不同之处:select2在模态对 ...
- Unity5.5.2 CD旋转 顺时针逆时针
UGUI 下 Sprite_CD 在Inspector下 Image(Script) 下 Clock wise 勾选 决定 CD是顺时针还是逆时针 默认是顺时针 勾选则为逆时针