HDU 3988 Harry Potter and the hide story (number theory-integer and prime number)

Source: Internet
Author: User
Harry Potter and the hide storyproblem descriptionisea is tired of writing the story of Harry Potter, so, lucky you, solving the following problem is enough.

 
Inputthe 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 00
3. 1 <= n <= 1 000 000 000 000 000
 
Outputfor 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
22 210 10
 
Sample output
Case 1: 1Case 2: 2
 
Author [email protected]
Source2011 multi-university training contest 15-host by whu
Recommendlcy

Question:

Given N and K, evaluate n! Maximum Value of I when it can be divisible by K ^ I.


Solution:

Change K into a qualitative factor, (1 × 2 × 3 ×... ×n) to be (P1 ^ (I * A1) × P2 ^ (I * A2) ×... ×pn ^ (I * an) Division, that is, the power of the prime factor of each denominator in the numerator is greater than or equal to the denominator.

Therefore, based on each qualitative factor of K, find the power numerator that satisfies each prime factor> = the limit of the denominator relationship I, and calculate the maximum I.

Unsigned long is used for this question ..


Reference code:

#include <iostream>#include <cstring>#include <cmath>#define INF 9223372036854775807ULLusing 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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.