• UVA10780 Again Prime? No time.


              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;求n!可以整除m的个数

    tip:计算它的质因子,然后求其最小幂

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    
    using namespace std;
    
    int main()
    {
        int t,c=1;
        cin>>t;
        while(t--)
        {
            int m,n,p,ans=1000000,a,b;
            cin>>m>>n;
            for(int i=2;m!=1;i++)
            {
                int p=0;
                while(m%i==0)
                {
                    m/=i;
                    p++;
                }
    
                if(p)
                {
                    a=n;///开始用n
                    b=0;///出现次数
                    while(a)
                    {
                        b+=a/i;
                        a/=i;
                    }
                    ans=min(ans,b/p);
                }
            }
            cout<<"Case "<<c++<<":"<<endl;
            if(ans==0)
                cout<<"Impossible to divide
    ";
            else
                cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    BZOJ1058:[ZJOI2007]报表统计(Splay,堆)
    BZOJ3224:普通平衡树(Splay)
    BZOJ3781:小B的询问(莫队)
    21. [HAOI2005] 希望小学 (wa1)
    cogs 2509. 森林大礼包
    libreoj #119. 最短路
    libreoj #514. 「LibreOJ β Round #2」模拟只会猜题意
    cogs 1647. 油田[uva572]
    css的部分应用示例
    html之表格
  • 原文地址:https://www.cnblogs.com/moqitianliang/p/4679314.html
Copyright © 2020-2023  润新知