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$进制下有多少后导零. 寒假好像写过这道题当时好像完 ...
随机推荐
- iframe 模拟ajax文件上传and formdata ajax 文件上传
对于文件上传 有好多种方式,一直想总结 文件上传的方法 今天就来写下 iframe 的文件上传的代码 本人语言表达能里有限,不多说了 直接上代码. 首先看 总体页面. 总共就三个文件. 实际上也就是 ...
- jmap, jhat命令
jmap命令有下面几种常用的用法 jmap [pid] jmap -histo:live [pid] >a.log jmap -dump:live,format=b,file=xxx.xxx [ ...
- linux下安装sz/rz命令
参考 https://blog.csdn.net/kobejayandy/article/details/13291655
- Django-----restframework图解
- codefirst 最新策略
http://www.yunjuu.com/info/76058.html 在原有数据库中使用 CodeFirst ,除了第一次添加实体后要立即执行一次 Enable-Migrations add-m ...
- hdu 4278 Faulty Odometer(进制转换)
十进制转八进制的变形: #include<stdio.h> int main() { int n; while(scanf("%d",&n)!=EOF& ...
- 浅谈android代码保护技术_加固
可看原文: http://www.cnblogs.com/jiaoxiake/p/6536824.html 导语 我们知道Android中的反编译工作越来越让人操作熟练,我们辛苦的开发出一个apk,结 ...
- py_initialize:C调Python出错 是初始化错误?
还是pythonpath和pythonname变量没有配置正确? py_initialize()方法是什么? In an application embedding Python, this shou ...
- 编写高质量代码改善C#程序的157个建议——建议10: 创建对象时需要考虑是否实现比较器
建议10: 创建对象时需要考虑是否实现比较器 有对象的地方就会存在比较,在.NET的世界中也一样.举个最简单的例子,在UI中,有一个10个人的Salary列表.根据排序的需要,列表要支持针对基本工资来 ...
- Sqlserver风格规范
常见的字段类型选择 1.字符类型建议采用varchar/nvarchar数据类型 2.金额货币建议采用money数据类型 3.科学计数建议采用numeric数据类型 4.自增长标识建议采用bigint ...