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

题意:

给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;
}
时间: 2024-12-16 15:42:29

[数论] hdu 3988 Harry Potter and the Hide Story的相关文章

HDU 3988 Harry Potter and the Hide Story(数论-整数和素数)

Harry Potter and the Hide Story 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. Eac

HDU3988-Harry Potter and the Hide Story(数论-质因数分解)

Harry Potter and the Hide Story Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2193    Accepted Submission(s): 530 Problem Description iSea is tired of writing the story of Harry Potter, so,

HDU 3988 n!质因数分解

Harry Potter and the Hide Story Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2324    Accepted Submission(s): 569 Problem Description iSea is tired of writing the story of Harry Potter, so, l

Harry Potter and the Hide Story(hdu3988)

Harry Potter and the Hide Story Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2809    Accepted Submission(s): 715 Problem Description iSea is tired of writing the story of Harry Potter, so, l

hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割

view code//hdu 3987 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> using namespace std; typedef long long ll; const ll INF = 1LL<<59; const ll E = 100001; const int N = 10

hdu:4771Stealing Harry Potter&#39;s Precious(bfs + 全排列)

题目:hdu:4771Stealing Harry Potter's Precious 题目大意:给出n* m的矩阵,代表n * m间room,然后每个房间又有脆弱和坚固之分,分别用'.'和'#'代替.'@'代表Dudely所在的起点. 题目说Dudely想要偷Harry的宝物,他知道宝物的位置,但是他的魔法只能穿过脆弱的room.愚蠢的他又想偷完harry所有的宝物,并不在乎如何人出去.问最短移动的步数偷完所有的宝物.没法偷完所有的宝物就输出-1. 解题思路:这里要求的是最短路径问题.可以将各

hdu 3987 Harry Potter and the Forbidden Forest【网路流最小割模型】

Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1549    Accepted Submission(s): 528 Problem Description Harry Potter notices some Death Eaters try to slip

HDU 3987 Harry Potter and the Forbidden Forest(最小割中的最少割边)经典

Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1791    Accepted Submission(s): 596 Problem Description Harry Potter notices some Death Eaters try to slip

【Dijstra堆优化】HDU 3986 Harry Potter and the Final Battle

http://acm.hdu.edu.cn/showproblem.php?pid=3986 [题意] 给定一个有重边的无向图,T=20,n<=1000,m<=5000 删去一条边,使得1~n的最短路最长 求最短路最长是多少 [思路] 一定是删最短路上的边 可以先跑一个Dijkstra,求出最短路,然n后枚举删边 朴素的Dijkstra为n^2,枚举删边(n条)需要的总时间复杂度是n^3 堆优化Dijkstra(nlogn),总复杂度为n^2logn 有多重边,用邻接矩阵不方便,用邻接表方便,