D - 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/i+n/i/i+n/i/i/i...表示n的i的指数,n/i是I的倍数个数,N/I/I是I*I的倍数,以此类推,由于I*I的个数包含在了I的倍数里,相当于已经算过一次了,所以N/i/i不用乘以2,同理N/i/i/i不用乘以3,这个应该是数学初等数论里面的,听人说过。是非常基础的。

#include<bits/stdc++.h>
using namespace std;
const int minn=0x3f3f3f3f;
int initial=2;int ddcase=1;
int main()
{
    int n,m;
    int t;
    cin>>t;
    while(t--)

    {cin>>m>>n;initial=2;
    int p=0;int ans=minn;
    while(m!=1)
    {
        p=0;
        while(m%initial==0)
        {
            p++;
            m/=initial;
        }
        if(p)
        {
            int temp=n;int total=0;
            while(temp!=0)
            {
                total+=temp/initial;
                temp/=initial;
            }
            ans=min(ans,total/p);

        }
        initial++;
    }
    printf("Case %d:\n",ddcase++);
    if(ans) cout<<ans<<endl;
    else cout<<"Impossible to divide"<<endl;}
    return 0;
}

还有0x3f3f3f3f表示很大值

附上稍微具体点的https://blog.csdn.net/qq_37867156/article/details/81837545

猜你喜欢

转载自blog.csdn.net/qq_42865713/article/details/84876181