zoukankan      html  css  js  c++  java
  • Trailing Zeroes (III) LightOJ

    Time Limit: 2 second(s) Memory Limit: 32 MB

    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

    Output for Sample Input

    3

    1

    2

    5

    Case 1: 5

    Case 2: 10

    Case 3: impossible

    题意:求出现一个最小的数的阶乘满足末尾有连续q个0。

    思路:一个最小的数末尾要出现0一定要是能被5或10整除的,0的连续长度与数组大小正相关,故二分数字得解。

     1 #include<set>
     2 #include<map>
     3 #include<stack>
     4 #include<cmath>
     5 #include<queue>
     6 #include<cstdio>
     7 #include<string>
     8 #include<cstring>
     9 #include<iostream>
    10 #include<limits.h>
    11 #include<algorithm>
    12 #define mem(a,b) memset(a,b,sizeof(a))
    13 using namespace std;
    14 const int INF=1e9;
    15 typedef unsigned long long ll;
    16 typedef long long LL;
    17 int main()
    18 {
    19     int T,cases=1;
    20     scanf("%d",&T);
    21     while(T--)
    22     {
    23         int q;
    24         LL ans=-1;
    25         scanf("%d",&q);
    26         int l=1,r=q*5,mid;//为什么右端点是q*5,因为q*5>=ans.想一想为什么(hint:刚才那个序列。。。)
    27         while(l<=r)
    28         {
    29             mid=(l+r)/2;
    30             int m=mid,wu=5,count=0;
    31             while(m>=wu)
    32             {
    33                 int p=m;
    34                 p/=wu;
    35                 count+=p;
    36                 wu*=5;
    37             }
    38             if(count==q)
    39             {
    40                 ans=mid;
    41                 break;
    42             }
    43             else if(count>q)
    44                 r=mid-1;
    45             else
    46                 l=mid+1;
    47         }
    48         if(ans==-1)
    49             printf("Case %d: impossible
    ",cases++);
    50         else
    51             printf("Case %d: %lld
    ",cases++,ans/5*5);
    52     }
    53 }
    View Code

    代码转自(https://blog.csdn.net/duan_1998/article/details/72566106)

  • 相关阅读:
    Allegro PCB Design GXL (legacy) 使用slide无法将走线推挤到焊盘的原因
    OrCAD Capture CIS 16.6 导出BOM
    Altium Designer (17.0) 打印输出指定的层
    Allegro PCB Design GXL (legacy) 将指定的层导出为DXF
    Allegro PCB Design GXL (legacy) 设置十字大光标
    Allegro PCB Design GXL (legacy) 手动更改元器件引脚的网络
    magento产品导入时需要注意的事项
    magento url rewrite
    验证台湾同胞身份证信息
    IE8对css文件的限制
  • 原文地址:https://www.cnblogs.com/llllrj/p/9526578.html
Copyright © 2011-2022 走看看