HDU3988-Harry Potter and the hide story (number theory-prime factor decomposition)

Source: Internet
Author: User
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 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] Question: Give You N and K, so that you can find the largest I satisfy n factorial can be divisible by k I power.
Idea: Perform prime factor decomposition on K to find the power of each prime factor in the factorial. The answer is the smallest power.
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <string>#include <algorithm>#include <queue>using namespace std;#define LL unsigned long longconst int maxn = 10000005;bool isPrime[maxn];vector<LL> prime,digit,cnt;void getPrime(){    prime.clear();    memset(isPrime,0,sizeof isPrime);    for(LL i = 2; i < maxn; i++){        if(!isPrime[i]){            prime.push_back(i);            for(LL j = i*i; j < maxn; j += i)                isPrime[j] = 1;        }    }}void getDigit(LL k){    for(int i = 0; i < prime.size() && k >= prime[i]; i++){        if(k%prime[i]==0){            int tt = 0;            digit.push_back(prime[i]);            while(k%prime[i]==0){                tt++;                k /= prime[i];            }            cnt.push_back(tt);        }    }    if(k!=1){        digit.push_back(k);        cnt.push_back(1);    }}LL getSum(LL n,LL p){    LL res = 0;    while(n){        n /= p;        res += n;    }    return res;}int main(){    int ncase,T=1;    LL k,n;    getPrime();    cin >> ncase;    while(ncase--){        cin >> n >> k;        if(k==1){            printf("Case %d: inf\n",T++);            continue;        }        LL ans = -1;        digit.clear();        cnt.clear();        getDigit(k);        for(int i = 0; i < digit.size(); i++){            LL tk = getSum(n,digit[i])/cnt[i];            if(ans == -1) ans = tk;            else ans = min(ans,tk);        }        printf("Case %d: %I64u\n",T++,ans);    }    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.