uva 10780 Again Prime? No Time.

原题:
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 < m < 5000) and n
(0 < n < 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
中文:
给你两个数,m和n,现在求最大的整数k使得m^k是n!的约数。

#include <bits/stdc++.h>
using namespace std;
int p[10001];
bool tag[100010];

void get_prime()
{
    int cnt=0;
    for(int i=2;i<10010;i++)
    {
        if(!tag[i])
            p[cnt++]=i;
        for(int j=0;j<cnt&&p[j]*i<10010;j++)
        {
            tag[i*p[j]]=1;
            if(i%p[j]==0)
                break;
        }
    }
}
void fac_decomposition(int x,int v[])
{
    for(int i=2;i<=x;i++)
    {
        int k=0;
        int t=i;
        while(t>1)
        {
            if(t%p[k]==0)
            {
                t/=p[k];
                v[p[k]]++;
            }
            else
                k++;
        }
    }
}
void decomposition(int x,int v[])
{
    int k=0;
    while(x>1)
    {
        if(x%p[k]==0)
        {
            x/=p[k];
            v[p[k]]++;
        }
        else
            k++;
    }
}
int mfac[10001],nfac[10001];
int main()
{
    ios::sync_with_stdio(false);
    get_prime();
    int t,n,m,k=1;
    cin>>t;
    while(t--)
    {
        memset(mfac,0,sizeof(mfac));
        memset(nfac,0,sizeof(nfac));
        cin>>m>>n;
        fac_decomposition(n,nfac);
        decomposition(m,mfac);
        int flag=0;
        int ans=INT_MAX;
        for(int i=2;i<=5000;i++)
        {
            if(mfac[i]>nfac[i])
            {
                flag=1;
                break;
            }
            if(mfac[i]!=0)
            {
                ans=min(ans,nfac[i]/mfac[i]);
            }
        }
        cout<<"Case "<<k++<<":"<<endl;
        if(!flag)
            cout<<ans<<endl;
        else
            cout<<"Impossible to divide"<<endl;
    }
    return 0;
}

解答:
很简单的入门题目,首先筛个素数表,然后使用唯一分解定理把n!和m分解出来存在nfac和mfac当中。如果m当中存在的质因子而n!当中没有那么n!肯定除不开m,就更别提m^k了。现在设Pi是n!和m都有的质因子,设Pi^x和Pi^y,x是n!当中Pi的指数幂,y是m的Pi的指数幂,用x/y取整得到k值,枚举Pi,找到最小的那个k就是答案。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值