UVa 10780 - Again Prime? No Time

Posted pisceskkk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVa 10780 - Again Prime? No Time相关的知识,希望对你有一定的参考价值。

Description

The problem statement is very easy. Given a number n you have to determine the largest power of m, not necessarily prime, that divides n!.

Input

The input file consists of several test cases. The first line in the file is the number of cases to handle. The following lines are the cases each of which contains two integers m ((1 lt m lt 5000)) and n ((0 lt n lt 10000)). The integers are separated by an space. There will be no invalid cases given and there are not more that 500 test cases.

Output

For each case in the input, print the case number and result in separate lines. The result is either an integer if m divides n! or a line "Impossible to divide" (without the quotes). Check the sample input and output format.

Sample Input

2
2 10
2 100

Sample Output

Case 1:
8
Case 2:
97

Resume

对给定的数n,求出m的最大幂次,使得m的该幂次可以整除n!。

Analysis

考虑到标准分解[ x=prod_{i = 1}^n {{p_i} ^{alpha_i} quad ext{where $p_i$ is a prime number.}} ]以及指数的乘法法则,我们只需要对小于等于n的每一个数标准分解,求出每个素数对应指数之和,与m的标准分解指数作比较即可得到最大幂次。
其中需要用素数筛(线性筛)预处理素数集数组。
为了降低一点时间复杂度,还破天荒地提前读入每一组数据做了个排序(逃。

Code

//////////////////////////////////////////////////////////////////////
//Target: UVa 10780 - Again Prime? No Time
//@Author: Pisceskkk
//Date: 2019-2-16
//////////////////////////////////////////////////////////////////////

#include<cstdio>
#include<algorithm>
#define N 10005
#define Min(a,b) (a<b?a:b)
#define Max(a,b) (a>b?a:b)

using namespace std;

int t,lm[N],ln[N],num[N],pow_m[N],pow_n[N],ans[N],max_j;

int primes[N],num_p;
bool is_prime[N];
bool comp(int a,int b){return ln[a]<ln[b];}
void sieve(int x){
    num_p = 0;
    is_prime[0] = is_prime[1] = 1;
    for(int i=1;i<=x;i++){
        if(is_prime[i] == 0){
            primes[num_p++] = i;
        }
        for(int j=0;j<num_p && i*primes[j] <= x;j++){
            is_prime[i*primes[j]] = 1;
            if(i%primes[j] == 0)break;
        }
    }
}



int main(){
    scanf("%d",&t);
    for(int c=1;c<=t;c++){
        scanf("%d %d",&lm[c],&ln[c]);
        num[c]=c;
    }
    sieve(N-1);
    sort(num+1,num+1+t,comp);
    for(int x=1;x<=t;x++){
        int m=lm[num[x]],n=ln[num[x]];
        ans[num[x]] = N;
        max_j = 0;
        for(int i=0;i<=n;i++)pow_m[i]=0;
        for(int i=ln[num[x-1]]+1;i<=n;i++){
            int x = i;
            for(int j=0;primes[j]<=x && j < num_p;j++){
                int k=0,p=primes[j];
                while(x%p == 0)p*=primes[j],k++;
                x = 1ll*x*primes[j]/p;
                pow_n[j]+=k;
                max_j = Max(max_j,j);
            }
        }
        for(int j=0;primes[j]<=m && j < num_p;j++){
            int k=0,p=primes[j];
            while(m%p == 0)p*=primes[j],k++;
            pow_m[j]=k;
            max_j = Max(max_j,j);
        }
        for(int j=0;j <= max_j;j++){
            if(pow_m[j] != 0){
                ans[num[x]] = Min(ans[num[x]],pow_n[j]/pow_m[j]);
            }
        }
    }
    for(int i=1;i<=t;i++){
        if(ans[i]){
            printf("Case %d:
%d
",i,ans[i]);
        }
        else{
            printf("Case %d:
Impossible to divide
",i);
        }
    }
    return 0;
}







以上是关于UVa 10780 - Again Prime? No Time的主要内容,如果未能解决你的问题,请参考以下文章

UVa10780

uva10780 质因子分解

UVA10617 Again PalindromeDP

Fractions Again?! UVA - 10976

UVa 10976 - Fractions Again?!

UVA 11076 Add Again