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;

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

black-hole6

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值