#include<iostream>
#define MAX 10000000
#define PRIME 700000
#define INF 9223372036854775807LL
using namespace std;
long long m,n,ans;
bool isprime[MAX+10];
long long prime[PRIME+10],top=0;
void init()
{
int i,j;
memset(prime,0,sizeof(prime));
memset(isprime,0,sizeof(isprime));
for(i=2;i<=MAX;i++)
{
if(!isprime[i])
{
prime[top]=i;
top++;
for(j=1;j*i<=MAX;j++)
{
isprime[i*j]=true;
}
}
}
}
long long getnum(long long x)
{
long long temp = n,ret = 0;
while (temp)
{
temp /= x;
ret += temp;
}
return ret;
}
int main()
{
long long t,i,now,p,temp,s;
init();
cin>>t;
for (s=1;s<=t;s++)
{
cin >> n >> m;
printf("Case %lld: ",s);
ans = INF;
if(m==1)
{
printf("inf\n");
continue;
}
else if(n==1)
{
printf("0\n");
continue;
} for(i=0;i<top&&prime[i]*prime[i]<=m;i++)
{
now=prime[i],p=0;
while(m%now==0)
m/=now,p++;
if(p)
{
temp=getnum(now)/p;
if(temp < ans)
ans = temp;
}
}
if(m != 1)
{
temp = getnum(m);
if(temp < ans)
ans = temp;
}
if (ans == INF)
printf("inf\n");
else
cout << ans << endl;
}
}
hdu 3988 Harry Potter and the Hide Story
最新推荐文章于 2024-05-06 13:03:08 发布