Regarding the question of how many trailing zeros are there in the p base of n!

Given the number n, find the number of trailing zeros in the p base of n! To simplify the problem, p is guaranteed to be a prime number.
If you use a big integer, the solution may time out.
It is easy to know that if a number can be divisible by another number, then the number that becomes a multiple of this number can also be divisible.
Using this idea, it can be deduced that "if it can be divisible by p, then the number of several multiples of this number can also be divisible", that is, "if a trailing 0 can be formed under the p-system, then the number of multiples of this number This can also be the case, and there may be more than one (proportional to the multiple)"
code is as follows:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int main()
{
    
    
    int t,n,p,sum=0;
    cin>>t;
    for(int i=0;i<t;i++){
    
     
        sum=0;
        cin>>n>>p;
        for(int k=p;k<=n;k+=p){
    
     //与p成倍数的数肯定会被p整除,所有可以加上p的倍数。枚举n中数。
            if(k%p==0){
    
    
                int te=k;
                while(te%p==0){
    
     //如果可以被整除,就除尽,得到倍数,从而得到所有的后导0。
                    te/=p;
                    sum++;
                }
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

The code is as above, welcome to discuss.

Guess you like

Origin blog.csdn.net/CAOSHUCAOSHU/article/details/110749271