Codeforces Round #538 (Div. 2) ---- C Trailing Loves (or L'oeufs?)(分解质因数)

链接:http://codeforces.com/contest/1114/problem/C
来源:Codeforces


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≤1018, 2≤b≤1012).

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


input
6 9
output
1


input
38 1
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.
You can read more about bases here.


这个题的意思就是说求n!在b进制下有多少个零


我们先来看几个例子:
100(10)=100(10)可以表示成1·102
16(10)=10000(2)可以表示成1·24
200(3)=18(10)可以表示成2*32
我们可以发现一个数在b进制下可以表示为x=a·bk,那么n!就可以表示成x
即n!=a·bk
也就是说n!%bk==0,所以就有下面的式子
b=p1k1·p2k2·······pmm
n!=p1K1·p2K2·······pmKm
我们可以这样表示:
a·(p1K1·p2K2·······pmKm)=a·(p1k1·p2k2·······pmm)k
接下来我们必定会得到这样一个式子:
p1K1·p2K2·······pmKm=p1k·k1·p2k·k2·······pmk·m
这时就可以取min( ∑ i = 1 m \sum_{i=1}^m i=1m(Ki/ki))
为什么是最小值呢?
这样才能保证K1,K2…Km共同的因子就是k


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<map> 
#define pi 3.1415926
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;

//typedef pair<int,int> Node;
typedef long long  LL;
const LL Max_n=10000005;
vector<LL>prime;
map<LL,LL>cnt;

void get_count(LL b){
	for(int i=2;i<=sqrt(b);++i){
		if(b%i==0){//找的b的每一个素因子保存下来并且保存每个素因子的个数
			prime.push_back(i);
			while(b%i==0) cnt[i]++,b/=i; 
		}
	}
	if(b>1){//如果b是素数
		prime.push_back(b);
		cnt[b]++;
	}
}

LL solve(LL n,LL p){//n!分解出的p的数量(文末简述)
	LL res=0;
	while(n) res+=n/p,n/=p;
	return res;
}

int main(){
	LL n,b;
	scanf("%lld%lld",&n,&b);
	get_count(b);
	LL len=prime.size();
	LL ans=1e18+5;
	for(int i=0;i<len;++i)
		ans=min(ans,solve(n,prime[i])/cnt[prime[i]]);
	printf("%lld\n",ans); 
    return 0;
}

n!有多少个p
现在我们来计算9!里面有多少个3
1 2 3 4 5 6 7 8 9
0 0 1 0 0 0 0 0 1
0 0 0 0 0 0 0 0 1
第一次的时候n为9.[1,n]之间9的倍数有2个数,此时3的个数是2(n/p),所以有res+=n/p
然后我们有n/=p,接下来我们就进行第二轮,此时n=3,在满足条件的里面3的倍数的个数是1(n/p)个,综上所述9!中3的倍数个数时3个

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值