zoukankan      html  css  js  c++  java
  • C

    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

    Input starts with an integer T (≤ 10000), denoting the number of test cases.

    Each case contains an integer Q (1 ≤ Q ≤ 108) in a line.

    Output

    For each case, print the case number and N. If no solution is found then print 'impossible'.

    Sample Input

    3

    1

    2

    5

    Sample Output

    Case 1: 5

    Case 2: 10

    Case 3: impossible

    其实我特别反感这种思维题目,感觉没别的方法,没什么意思,只能这样写,还有就是我太菜了(这才是原罪)

    思路::有几个0出现关键就看2和5的个数,因为是阶乘,都是乘法运算,所有要将没一个数拆开,看看他包括几个2或者几个5,,比如,10  可以分为2和5,所以只有1个5,而

    25可以分为5和5 所以有两个5。。。。。在一组数中,二的数目一定比5 多吗,所以直接找5 的个数就可以啦!

    记得设置为long long 

    AC代码:

    #include<iostream>
    #include<cstdio>
    using namespace std;
    typedef long long ll;
    ll sum(ll x){
        ll ans=0;
        while(x)
        {
            ans+=x/5;
            x=x/5;
        }
        return ans;
    }
    
    int main()
    {
        int  t;
        scanf("%d",&t);
        for(int i=1;i<=t;i++)
        {
            ll n;
            scanf("%lld",&n);
            ll left=1,right=1000000000,ans=0,mid;
            while(left<=right)
            {
                mid=(left+right)/2;
                if(sum(mid)==n){
                    ans=mid;
                    right=mid-1;
                }//找到了不一定是最小的比如11和10,阶乘都可以产生2个0
                else if(sum(mid)>n){
                    right=mid-1;
                }
                else {
                    left=mid+1;
                }
            }
            
            if(ans>0)
                printf("Case %d: %lld
    ",i,ans);
            else 
            {
                printf("Case %d: impossible
    ",i);
            }
        }
        return 0;
    }
  • 相关阅读:
    SSL证书安装指引
    SSL证书绑定成功
    我是如何将网站全站启用Https的?记录博客安装配置SSL证书全过程
    _INT_SIZEOF 规格严格
    Java: Multidimensional array vs. Onedimensional 规格严格
    Fix JDK TImer 规格严格
    Ecpilse debug视图 规格严格
    PE 分析 规格严格
    VC Unicode 规格严格
    Object Pool 规格严格
  • 原文地址:https://www.cnblogs.com/Accepting/p/11247701.html
Copyright © 2011-2022 走看看