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

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
    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的素因子及相应的次数筛出即可

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define N 10000005
using namespace std;
bool p[N];
vector<int> prime;
void init()
{
    for(int i=2;i<N;i++)
        if(!p[i])
        {
            prime.push_back(i);
            for(int j=i+i;j<N;j+=i)
                p[j]=true;
        }
}
long long fac[100];
int e[100];
int cnt;
void getFac(long long d)
{
    cnt=0;
    for(int i=0;(long long)prime[i]*prime[i]<=d;i++)
    {
        if(d%prime[i]==0)
        {
            fac[cnt]=prime[i];
            e[cnt]=0;
            while(d%prime[i]==0)
            {
                d/=prime[i];
                e[cnt]++;
            }
            cnt++;
        }
    }
    if(d>1)
    {
        fac[cnt]=d;
        e[cnt]=1;
        cnt++;
    }
}
long long res(long long n,long long fac)
{
    long long ans=0;
    while(n)
    {
        n/=fac;
        ans+=n;
    }
    return ans;
}
int main()
{
    int t;
    init();
    long long n,k;
    int ca=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&n,&k);
        if(k==1)
        {
           printf("Case %d: inf\n",ca++);
           continue;
        }
        getFac(k);
        long long ans=res(n,fac[0])/e[0];
        for(int i=1;i<cnt;i++)
        {
            long long temp=res(n,fac[i])/e[i];
            ans=min(ans,temp);
        }
        printf("Case %d: %lld\n",ca++,ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值