N!转换成B进制,末尾0的个数

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。然后搞了一个从2到n的循环,可以整除b,就ans++。大于b,就对b取模。

正解:将b分解因子,比如b可以分解成2个3,4个7。那么ans=min(n!中3的个数/2,n!中7的个数/4);

#include <bits/stdc++.h>
using namespace std;
#define ll long long int 
#define res register int 
#define inf 0x3f3f3f3f
const int maxn=1e5;
ll a[maxn],num[maxn];

ll cal(ll y,ll x)
{
	ll ress=0;
	while(x){
		ress+=x/y;
		x/=y;
	}
	return ress;
}

int main()
{
	ll n,b,temp,cnt=0;
	cin>>n>>b;
	for(ll i=2;i*i<=b;i++){
		if(0==b%i){
			a[++cnt]=i;
			while(0==b%i){
				num[cnt]++;
				b/=i;
			}
		}
	}
	if(b>1) num[++cnt]=1,a[cnt]=b;
	ll ans=0;
	for(ll i=1;i<=cnt;i++){
		if(1==i) ans=cal(a[i],n)/num[i];
		else ans=min(ans,(ll)(cal(a[i],n)/num[i]));
	} 
	cout<<ans; 
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值