UVA-10780 Again Prime? No Time. (数论-质因子分解)

UVA-10780 Again Prime? No Time. (数论-质因子分解)

题意

给一个m,给一个n,求m的最大次方数ans,能被n整除。

思路来源

https://blog.****.net/u011345136/article/details/38658977

题解

将m质因数分解m=UVA-10780 Again Prime? No Time. (数论-质因子分解)

对于每个质因子pi,其在n!中出现的次数为

sum=UVA-10780 Again Prime? No Time. (数论-质因子分解)+…+UVA-10780 Again Prime? No Time. (数论-质因子分解),其中max是p的不超过n的最大幂,

显然,pi的出现了1次,

pi²的出现了2次,在pi中被统计一次,在pi²被统计一次,不重不漏。

pi的更高次幂同上。

那么需要的pi的ans值即为sum/ai,

短板效应,取ans最小即可。

诶,这题让我学会怎么用map的遍历了

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
const int maxn=3e3+10;
using namespace std;
typedef long long ll;
int t;
map<ll,ll>q;
map<ll,ll>fac(ll n)
{
	map<ll,ll>res;
	for(ll i=2;i*i<=n;++i)
	{
	    while(n%i==0)
		{
			++res[i];
			n/=i;
		}
	}
	if(n!=1)res[n]=1;
	return res;
}
int main()
{
	scanf("%d",&t);
	for(int k=1;k<=t;++k)
	{
		ll n,m,cnt=0;
		ll ans=1e18;
		q.clear();
		scanf("%lld%lld",&m,&n);
		q=fac(m);
		for(map<ll,ll>::iterator it=q.begin();it!=q.end();++it)
		{
			ll t=it->first,num=it->second,tmp=0;
			for(ll i=t;i<=n;i*=t)tmp+=n/i;
			tmp/=num;
			ans=min(ans,tmp);
		}
	    printf("Case %d:\n",k);
	    if(ans)printf("%lld\n",ans);
	    else puts("Impossible to divide");
	}
	return 0;
}