【CodeForces - 1114C】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 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

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)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的个数。

思路:

唯一分解定理,设b分解后第i个质因子的为pi有ci个。

假设n!的因子中包含t_i个pi^ci。

能凑出多少0就是min(t_i)

对于找数组中的最小值,除非确定自己的数一定比数组所有元素都大,否则就把minn赋值为数组中的第一个数。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
ll f[10000];
ll fn[10000];
ll num[10000]={0};
int cnt=0;
void ff(ll n){
	ll t=n;
	
	for(ll i=2;i*i<=t;i++){
		if(n%i==0)
		{
			f[cnt]=i;
			fn[cnt]=0;
			while(n%i==0){
				fn[cnt]++;
				n/=i;
			}
			cnt++;
		}
	}
	if(n!=1){
		f[cnt]=n;fn[cnt]=1;
		cnt++;
	} 
}

int main(){
	ll n,b;
	scanf("%lld%lld",&n,&b);
	ff(b);
	ll t=n;
	for(int i=0;i<cnt;i++){
		n=t;
		while(n){
			num[i]+=n/f[i];
			n/=f[i];
		}
		num[i]/=fn[i];
	}
	ll minn=num[0];//这个地方开始取得数不够大,wa了一次
	for(int i=0;i<cnt;i++){
		minn=min(minn,num[i]);
	}
	printf("%lld\n",minn);
	return 0;	
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值