题目:
1. 1 <= T <= 500
2. 1 <= K <= 1 000 000 000 000 00
3. 1 <= N <= 1 000 000 000 000 000 000
思路:
对k进行质因子分解,对于每一个质因子pi,k中出现的次数为cnt,计算出n!中包括该质因子的个数sum,那么对于pi的最大值就应该是sum/cnt。
当k=1时,答案是inf
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<algorithm>
#include<ctime>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<numeric>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define mm(a,b) memset(a,b,sizeof(a))
#define PP puts("*********************");
template<class T> T f_abs(T a){ return a > 0 ? a : -a; }
template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
// 0x3f3f3f3f3f3f3f3f
const int maxn=1e7+5;
bool isprime[maxn];
int prime[maxn],tol;
void make_prime(int n){
for(int i=0;i<=n;i++)
isprime[i]=true;
isprime[0]=isprime[1]=false;
tol=0;
for(int i=2;i<=n;i++){
if(isprime[i])
prime[tol++]=i;
for(int j=0;j<tol;j++){
if(i*prime[j]<=n)
isprime[i*prime[j]]=false;
else
break;
if(i%prime[j]==0)
break;
}
}
}
LL get_sum(LL n,LL p){
LL sum=0;
while(n){
n/=p;
sum+=n;
}
return sum;
}
int main(){
int T,cas=0;
LL K,N;
make_prime(maxn-1);
scanf("%d",&T);
while(T--){
scanf("%lld%lld",&N,&K);
if(K==1){
printf("Case %d: inf\n",++cas);
continue;
}
LL ans=-1;
for(int i=0;i<tol&&(LL)prime[i]*prime[i]<=K;i++){
if(K%prime[i]==0){
LL cnt=0;
while(K%prime[i]==0){
cnt++;
K/=prime[i];
}
LL tmp=get_sum(N,(LL)prime[i])/cnt;
if(ans==-1)
ans=tmp;
else
ans=min(ans,tmp);
}
}
if(K>1){
LL tmp=get_sum(N,K);
if(ans==-1)
ans=tmp;
else
ans=min(ans,tmp);
}
printf("Case %d: %lld\n",++cas,ans);
}
return 0;
}