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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章