Harry Potter and the Hide Story
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2934 Accepted Submission(s): 755
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
Author
iSea@WHU
Source
Recommend
lcy
题意:给两个证整数n和k求出n的阶乘整除k^i最大整数i
k的范围小于1e14,素数表打到1e7即可,将k唯一分解,求出n的阶乘对应因子除以k唯一分解的结果求最最小值
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
#define pi acos(-1.0)
#define eps 1e-10
#define pf printf
#define sf scanf
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define e tree[rt]
#define _s second
#define _f first
#define all(x) (x).begin,(x).end
#define mem(i,a) memset(i,a,sizeof i)
#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)
#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)
#define mi ((l+r)>>1)
#define sqr(x) ((x)*(x))
const int inf=0x3f3f3f3f;
const int Max=1e7+10;
int p[Max/10],t;
ll k,n;
bool vis[Max+1];
void prime()
{
mem(vis,0);
p[0]=0;
for(int i=2;i<=Max;i++)
{
if(!vis[i])p[++p[0]]=i;
for(int j=1;j<=p[0]&&(ll)i*p[j]<=Max;j++)
{
vis[i*p[j]]=1;
if(!(i%p[j]))break;
}
}
}
ll out(ll x,ll y)//求阶乘对应素数y的因子和
{
ll sum=0;
while(x)
{
x/=y;
sum+=x;
}
return sum;
}
int main()
{
prime();
sf("%d",&t);
for1(i,t)
{
sf("%I64d%I64d",&n,&k);
pf("Case %d: ",i);
if(k==1)
{
pf("inf\n");
continue;
}
ll ans=-1;
for(int i=1;i<=p[0]&&(ll)p[i]*p[i]<=k;i++)
{
if(!(k%p[i]))
{
ll c=0;
while(!(k%p[i]))
c++,k/=p[i];
ll tmp=out(n,p[i])/c;//阶乘因子和除以k含的因子和
if(ans==-1)ans=tmp;
else ans=min(ans,tmp);
}
}
if(k>1)
{
ll tmp=out(n,k);
if(ans==-1)ans=tmp;//k为大于1e7的素数,直接求因子和
else ans=min(ans,tmp);
}
pf("%I64d\n",ans);
}
return 0;
}