sgu 154 Factorial

39 篇文章 0 订阅
14 篇文章 0 订阅
154. Factorial
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
input: standard input
output: standard output



You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. For example, 5! = 120, 120 contains one zero on the trail.

Input
One number Q written in the input (0<=Q<=10^8).

Output
Write "No solution", if there is no such number N, and N otherwise.

Sample test(s)

Input
2

Output
10



问是否存在整数n,使得n!的末尾恰有Q个0.有的话输出最小满足条件的n

n!尾数0的个数就是 [n/5] + [n/25] + [n/5^3]+.....


我们可以二分枚举n ,然后做判断,不断缩小范围。。

知道AC。


贴个代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>

#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)

using namespace std;
int const nMax = 1010;
int const base = 10;
typedef long long LL;
typedef pair<LL,LL> pij;

//    std::ios::sync_with_stdio(false);

long long n;
LL f(LL a){
    LL ans=0;
    LL t=5;
    while(a/t){
        ans+=(a/t);
        t*=5;
    }
    return ans;
}
int main(){
    LL l,r;
    l=0,r=450000000;
    cin>>n;
    LL ans=r;
    while(l<=r){
        LL mid=(l+r)>>1;
        LL p=f(mid);
       // printf("%lld %lld\n",mid,p);
        if(p==n){
            if(ans>mid)
            ans=mid;
            r=mid-1;
        }else if(p>n){
            r=mid-1;
        }else {
            l=mid+1;
        }
    }
    if(ans==0)ans=1;
    if(ans==450000000){
        printf("No solution\n");
    }else
        printf("%I64d\n",ans);
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值