分治算法(1)N!个数为0的情况

 题目地址     https://www.cnblogs.com/hao-tian/p/9274708.html

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;

long long solve(long long n)
{
    long long sum=0;
    while(n>0)
    {
        n/=5LL;
        sum+=n;
    }
    return  sum;
}


int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        long long left=1;
        long long right=500000000;
        long long ans=500000001;
        while(left<=right)
        {
            int mid=(right-left)/2+left;
            int t=solve(mid);
            if(t==n&&mid<ans)
            {
                ans=mid;
            }
            if(t>n)
            {
                right=mid-1;
            }
            else if(t<n)
            {
                left=mid+1;
            }
            else
            {
                right=mid-1;
            }
        }
        if(ans==500000001)
        {
            printf("No solution!\n");
        }
        else
        {
            printf("%d\n",ans);
        }
    }
}

相关推荐