HDU - 3988 Harry Potter and the Hide Story

题目:


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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值