P2388 factorial of the ride

Thank wxy seniors told me this question before, even today, a result of the cut, Gugu Gu

Topic links:

P2388 factorial of the ride

Topic ideas:

At first glance must think about the factorial is to seek and then look at the last few zero, but it will TIL ah

Think about how the emergence of 0 -> $ 10 = 2 \ times 5 $ 0 each by a factor of 2 and 5 are obtained by multiplying, just the statistics can be a multiple of 5

Why is it 5 instead of 2?

Because multiples of 2 to less than 5 lot

Code:

#include<iostream>
#include<cstdio>
#define int long long int 
using namespace std;
int n;
int ans;
signed main()
{
    cin>>n;
    int p=0;
    for(int i=1;i<=n;++i)
    {
        int x=i;
        while(x%5==0&&x!=0)
        {
            p++;
            x/=5;
        }
        ans+=p;
    }
    cout<<ans;
    return 0;
}   

Guess you like

Origin www.cnblogs.com/pyyyyyy/p/11072002.html