任意门:http://codeforces.com/contest/1114/problem/C

C. Trailing Loves (or L'oeufs?)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 92009200 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 nn and bb (in decimal notation), your task is to calculate the number of trailing zero digits in the bb-ary (in the base/radix of bb) representation of n!n! (factorial of nn).

Input

The only line of the input contains two integers nn and bb (1≤n≤10181≤n≤1018, 2≤b≤10122≤b≤1012).

Output

Print an only integer — the number of trailing zero digits in the bb-ary representation of n!n!

Examples
input

Copy
6 9
output

Copy
1
input

Copy
38 11
output

Copy
3
input

Copy
5 2
output

Copy
3
input

Copy
5 10
output

Copy
1
Note

In the first example, 6!(10)=720(10)=880(9)6!(10)=720(10)=880(9).

In the third and fourth example, 5!(10)=120(10)=1111000(2)5!(10)=120(10)=1111000(2).

The representation of the number xx in the bb-ary base is d1,d2,…,dkd1,d2,…,dk if x=d1bk−1+d2bk−2+…+dkb0x=d1bk−1+d2bk−2+…+dkb0, where didi are integers and 0≤di≤b−10≤di≤b−1. For example, the number 720720 from the first example is represented as 880(9)880(9) since 720=8⋅92+8⋅9+0⋅1720=8⋅92+8⋅9+0⋅1.

You can read more about bases here.

题意概括:

求 n! 转化为 b 进制下末尾有多少个 0.

解题思路:

原题:swjtuOJ 2090

这是一道很好的数论题。

首先转换一下思路:

要求 n! 在 b 进制下有多少个尾 0 就相当于 求 n! % (b^k) == 0 的最大 k。

那么我们现在把 n! 看作一个数 A。问题就是 求 A % (b^k) == 0 的最大 k;

我们知道有素数分解定理: b = p1^a1 * p2^a2 * p3^a3 ...;

那么我们如果可以求得 A 里面 p1^b1 * p2^b2 * p3^b3 ...  的 b1, b2, b3...

那么答案 ans = min(ans, ai/bi ) 了也就是要整除,首先要满足最小的那个能整除。

(1)首先对 b 进行素因子分解,直接暴力(log b), 用一个数组离散化形成该素因子的编号和该素因子的幂的映射 或者 用map存储该素因子的幂,得到所有素因子以及素因子的幂

(2)对于每一个素因子p,计算对应的 A(即 n! ) 中素因子p的幂,两者相除取所有p幂的最小值就是对应的最大整数。

这里求 n! 下 素因子 p 的幂 用累除法,因为存在推论:

n! 下 p 的幂 = [ n/p ] + [ n/(p^2) ] + [ n/(p^3) ]  ...

学习:

https://blog.csdn.net/Wen_Yongqi/article/details/86976902

AC code:

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 2e6+;
LL N, B;
vector<LL>prime;
//LL num[MAXN];
map<LL, int>mmp;
void get_p(LL n)
{
LL len = sqrt((double)n);
//printf("len %lld\n", len);
for(LL i = ; i <= len; i++){
if(n%i == ){
prime.push_back(i);
while(n%i == ){
n/=i;
//num[prime.size()-1]++;
mmp[i]++;
}
}
}
if(n != ){
prime.push_back(n);
// num[prime.size()-1]++;
mmp[n]++;
}
} LL calc(LL n, LL p)
{
LL res = ;
while(n){
res += n/p;
n/=p;
//puts("zjy");
}
return res;
} int main()
{
LL sum = 1LL;
scanf("%I64d %I64d", &N, &B);
get_p(B);
LL ans = (1LL<<);
for(LL i = ; i < prime.size(); i++){
// ans = min(ans, calc(N, prime[i])/num[i]);
//puts("zjy");
ans = min(ans, calc(N, prime[i])/mmp[prime[i]]);
} printf("%I64d\n", ans);
return ;
}

CF#538(div 2) C. Trailing Loves (or L'oeufs?) 【经典数论 n!的素因子分解】的更多相关文章

  1. 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    ...

  2. Codeforces - 1114C - Trailing Loves (or L'oeufs?) - 简单数论

    https://codeforces.com/contest/1114/problem/C 很有趣的一道数论,很明显是要求能组成多少个基数. 可以分解质因数,然后统计各个质因数的个数. 比如8以内,有 ...

  3. CF 1114 C. Trailing Loves (or L'oeufs?)

    C. Trailing Loves (or L'oeufs?) 链接 题意: 问n!化成b进制后,末尾的0的个数. 分析: 考虑十进制的时候怎么求的,类比一下. 十进制转化b进制的过程中是不断mod ...

  4. C. Trailing Loves (or L'oeufs?) (质因数分解)

    C. Trailing Loves (or L'oeufs?) 题目传送门 题意: 求n!在b进制下末尾有多少个0? 思路: 类比与5!在10进制下末尾0的个数是看2和5的个数,那么 原题就是看b进行 ...

  5. CF#538 C - Trailing Loves (or L'oeufs?) /// 分解质因数

    题目大意: 求n!在b进制下末尾有多少个0 https://blog.csdn.net/qq_40679299/article/details/81167283 一个数在十进制下末尾0的个数取决于10 ...

  6. Trailing Loves (or L'oeufs?)

    The number "zero" is called "love" (or "l'oeuf" to be precise, literal ...

  7. C. Trailing Loves (or L'oeufs?)

    题目链接:http://codeforces.com/contest/1114/problem/C 题目大意:给你n和b,让你求n的阶乘,转换成b进制之后,有多少个后置零. 具体思路:首先看n和b,都 ...

  8. Trailing Loves (or L'oeufs?) CodeForces - 1114C (数论)

    大意: 求n!在b进制下末尾0的个数 等价于求n!中有多少因子b, 素数分解一下, 再对求出所有素数的最小因子数就好了 ll n, b; vector<pli> A, res; void ...

  9. 【Codeforces 1114C】Trailing Loves (or L'oeufs?)

    [链接] 我是链接,点我呀:) [题意] 问你n!的b进制下末尾的0的个数 [题解] 证明:https://blog.csdn.net/qq_40679299/article/details/8116 ...

随机推荐

  1. SQL Server 2008中的MERGE(数据同步)

    OK,就像标题呈现的一样,SQL Server 2008中的MERGE语句能做很多事情,它的功能是根据源表对目标表执行插入.更新或删除操作.最典型的应用就是进行两个表的同步. 下面通过一个简单示例来演 ...

  2. 开窗函数over()

    使用方法 如:select name,avg(shengao)from xinxi group by name //我们都知道使用聚合函数要使用分组,如果不分组怎么办 Selct name,avg(s ...

  3. Shiro - 关于session

    Shiro Session session管理可以说是Shiro的一大卖点. Shiro可以为任何应用(从简单的命令行程序还是手机应用再到大型企业应用)提供会话解决方案. 在Shiro出现之前,如果我 ...

  4. android 动态库死机调试方法 .

    原地址:http://blog.csdn.net/andyhuabing/article/details/7074979 这两种方法都不是我发明了,都是网上一些高手公共出来的调试方法,无奈找不到出处的 ...

  5. 四层协议和Socket编程

    <四层协议图> <Soclet编程模型图>

  6. SQL Server UDF to pad a string

    http://www.mssqltips.com/sqlservertip/1738/sql-server-udf-to-pad-a-string/ declare @l varchar(50) se ...

  7. chrome调试工具DevTools的使用 以及 localhost在移动端不能访问的问题

    1.手机和pc 都需要装 chrome浏览器 2.手机端打开开发者模式和usb调试 (华为nova的手机小坑,需要选择usb 配置为可传输文件的状态) 3.经过以上操作打开chrome://inspe ...

  8. 把连接中传的参数截取出来变成一个json对象

    获取url function test() { var url=window.location.search; if(url.indexOf("?")!=-1) { var str ...

  9. 多尺度几何分析(Ridgelet、Curvelet、Contourlet、Bandelet、Wedgelet、Beamlet)

    稀疏基的讨论已经持续了近一个月了,这次讨论多尺度几何分析.但由于下面讨论的这些变换主要面向图像,而本人现在主要关注于一维信号处理,所以就不对这些变换深入讨论了,这里仅从众参考文献中摘抄整理一些相关内容 ...

  10. 【Machine Learning】决策树之ID3算法 (2)

    决策树之ID3算法 Content 1.ID3概念 2.信息熵 3.信息增益 Information Gain 4. ID3 bias 5. Python算法实现(待定) 一.ID3概念 ID3算法最 ...