CodeForces

题目链接

题目大意

  给你一个10进制数a,问a的阶乘在b进制下末尾有几个0。

解题思路

  结尾有几个0其实就是能被b整除几次,很显然,这个阶乘每乘出一个b的倍数,末尾的0就会增加。我们将b分解质因数,求出这个阶乘中包含质因数的个数,然后除以它们在b中的次数然后取最小就行了。

代码

const int maxn = 2e5+10;
const int maxm = 2e5+10;
vector<ll> tmp;
int cnt[100];
ll solve(ll x, ll y) {
	ll sum = 0;
	while(x) {
		sum += x/y;
		x /= y;
	}
	return sum;
}
int main() {
	ll a, b; cin >> a >> b;
	int tot = 0;
	for (ll i = 2; i*i<=b; ++i) 
		if (b%i==0) {
			tmp.push_back(i);
			while(b%i==0) {
				b /= i;
				++cnt[tot];
			}
			++tot;
		}
	if (b>1) {
		tmp.push_back(b);
		++cnt[tot];
		++tot;
	}
	ll ans = LLONG_MAX;
	for (int i = 0; i<tot; ++i) ans = min(ans, solve(a, tmp[i])/cnt[i]);
	cout << ans << endl;
	return 0;
} 
原文地址:https://www.cnblogs.com/shuitiangong/p/14447564.html