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.
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分解质因素,问题变为,(1×2×3×...×n) 要被 ( p1^(i*a1) × p2^(i*a2) × ... × pn^(i*an) ) 整除,即分子中各分母的质因数的幂次要大于等于分母。

所以根据k的各质因素,求出满足各质因数的幂次 分子>=分母 的关系限制i,算出最大的i即可。

这题要用到unsigned long long,比较坑。。


参考代码:

#include <iostream>
#include <cstring>
#include <cmath>
#define INF 9223372036854775807ULL
using namespace std;

typedef unsigned long long ull;
const int MAXN = 10000010;
int T, cnt;
ull N, K, ans, factorA[MAXN], factorB[MAXN], totFactor, prime[MAXN], totPrime;
bool isPrime[MAXN];

void getPrime(ull n) {
    memset(isPrime, true, sizeof(isPrime));
    totPrime = 0;
    for (ull i = 2; i <= n; i++) {
        if (isPrime[i]) {
            prime[++totPrime] = i;
        }
        for (ull j = 1; j <= totPrime && i*prime[j] <= n; j++) {
            isPrime[i*prime[j]] = false;
            if (i % prime[j] == 0) break;
        }
    }
}

void getFactor(ull n) {
    /*
    ull now = n;
    totFactor = 0;
    for (ull i = 2; i*i <= n; i++) {
        if (now % i == 0) {
            factorA[++totFactor] = i;
            factorB[totFactor] = 0;
            while (now % i == 0) {
                factorB[totFactor]++;
                now /= i;
            }
        }
    }
    if (now != 1) {
        factorA[++totFactor] = now;
        factorB[totFactor] = 1;
    }
    */
    totFactor = 0;
    ull now = n;
    for (ull i = 1; i <= totPrime && prime[i] <= now; i++) {
        if (now % prime[i] == 0) {
            factorA[++totFactor] = prime[i];
            factorB[totFactor] = 0;
            while (now % prime[i] == 0) {
                factorB[totFactor]++;
                now /= prime[i];
            }
        }
    }
    if (now != 1) {
        factorA[++totFactor] = now;
        factorB[totFactor] = 1;
    }
}

void solve() {
    if (K == 1) {
        cout << "Case " << ++cnt << ": inf" << endl;
    } else {
        getFactor(K);
        ans = INF;
        for (ull i = 1; i <= totFactor; i++) {
            ull temp = N, sum = 0;
            while (temp > 0) {
                sum += temp / factorA[i];
                temp /= factorA[i];
            }
            if (sum / factorB[i] < ans) {
                ans = sum / factorB[i];
            }
        }
        cout << "Case " << ++cnt << ": " << ans << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin >> T;
    getPrime(10000000);
    while (T--) {
        cin >> N >> K;
        solve();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值