码迷,mamicode.com
首页 > 其他好文 > 详细

[数论] hdu 3988 Harry Potter and the Hide Story

时间:2014-11-11 19:15:15      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:io   ar   os   sp   for   on   代码   amp   ef   

题意:

给N、K,问满足 n!%(k^x)=0 最大的x。

思路:

首先当k=1的时候,输出inf

然后就是,因为要整除,所以我们就分解k的质因子

假设每个质因子有si个,那么对应的n!里面有sumi个

那么对于当前因子最大的x=suni/si

然后就是所有的因子找最小值了。

这里需要打表 10^7的素数表

代码:

#include"cstdlib"
#include"cstdio"
#include"cstring"
#include"cmath"
#include"stack"
#include"algorithm"
#include"iostream"
#define ll __int64
#define N 10000000
using namespace std;
int v[10000007],ss[777777],scnt;
void ssb()
{
    scnt=0;
    memset(v,0,sizeof(v));
    int lit=sqrt(N*1.0);
    for(int i=2; i<=lit; i++)
    {
        if(!v[i])
        {
            for(int j=2; j*i<=N; j++) v[j*i]=1;
        }
    }
    for(int i=2; i<=N; i++) if(!v[i]) ss[scnt++]=i;
}
ll solve(ll n,ll m)
{

    ll ans=0;
    while(n)
    {
        n/=m;
        ans+=n;
    }
    return ans;
}
int main()
{
    int t,cas=1;
    cin>>t;
    ssb();
    while(t--)
    {
        ll k,n;
        ll ans=-1;
        scanf("%I64d%I64d",&n,&k);
        printf("Case %d: ",cas++);
        if(k==1)
        {
            puts("inf");
            continue;
        }
        for(int i=0; i<scnt; i++)
        {
            if(ss[i]>k) break;
            ll tep=0;
            while(k%ss[i]==0)
            {
                tep++;
                k/=ss[i];
            }
            if(tep!=0)
            {
                ll sum=solve(n,ss[i]);
                if(ans==-1) ans=sum/tep;
                else ans=min(ans,sum/tep);
            }
        }
        if(k>1)
        {
            if(ans==-1) ans=solve(n,k);
            else ans=min(ans,solve(n,k));
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


[数论] hdu 3988 Harry Potter and the Hide Story

标签:io   ar   os   sp   for   on   代码   amp   ef   

原文地址:http://blog.csdn.net/wdcjdtc/article/details/41014129

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!