Harry Potter and the Hide Story
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2734 Accepted Submission(s): 688
Problem Description
iSea is tired of writing the story of Harry Potter, so, lucky you, solving the following problem is enough.
Input
The first line contains a single integer T, indicating the number of test cases.
Each test case contains two integers, N and K.
Technical Specification
1. 1 <= T <= 500
2. 1 <= K <= 1 000 000 000 000 00
3. 1 <= N <= 1 000 000 000 000 000 000
Each test case contains two integers, N and K.
Technical Specification
1. 1 <= T <= 500
2. 1 <= K <= 1 000 000 000 000 00
3. 1 <= N <= 1 000 000 000 000 000 000
Output
For each test case, output the case number first, then the answer, if the answer is bigger than 9 223 372 036 854 775 807, output “inf” (without quote).
Sample Input
2 2 2 10 10
Sample Output
Case 1: 1 Case 2: 2
肯定要求因子数,然后求指数和。分解 k 的因子,对于每个因子,看其指数为 c,与 n! 里面相同的因子指数为 s,求max(s-c)。
这个数论题目理解还不是很透彻啊,多刷题,多理解
注意,这的const int N = 10000005;如果多以为就爆内存,少一位就wa
#include<iostream>//HDU 3988
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 10000005;
int prime[N],flag[N],num=0;
void Init()//素数打表
{
for(int i=2;i<=N;i++)
{
if(flag[i])
continue;
prime[num++]=i;
for(int j=2;i*j<=N;j++)
flag[i*j]=1;
}
}
long long get(long long n,long long p)
{
long long ret=0;
while(n)
{
n/=p;
ret+=n;
}
return ret;
}
int main()
{
int t,ans=0;
cin>>t;
Init();
while(t--)
{
long long n,k;
scanf("%lld%lld",&n,&k);
if(k==1)//k=1时,无穷大,so
{
printf("Case %d: inf\n",++ans);
continue;
}
long long maxn=-1;
for(int i=0;i<num&&prime[i]<=k;i++)
{
if(k%prime[i]==0)
{
int len=0;
while(k%prime[i]==0)
{
len++;
k/=prime[i];
}
long long tmp=get(n,prime[i])/len;
if(maxn==-1)
maxn=tmp;
else
maxn=min(maxn,tmp);
}
}
if(k>1)
{
long long tmp=get(n,k);
if(maxn==-1)
maxn=tmp;
else
maxn=min(maxn,tmp);
}
printf("Case %d: %lld\n",++ans,maxn);
}
return 0;
}