light oj 1138

 
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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

 

题意:寻找最小的自然数N,使其阶乘的末尾有Q个0.
思路:只有2和5相乘时末尾才会出现0,在阶乘过程中,因子2的个数足够多,所以每遇到一个5,末尾就会出现一个0,因此问题转化为求1到N这N个整数中包含了多少个因子5。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<stdio.h>
#include<string.h>
#define MAX 0x3f3f3f
#define LL long long
LL fun(LL x)
{
    LL ans=0;
    while(x)
    {
        ans+=x/5;
        x=x/5;
    }
    return ans;
}
int main()
{
    int n,k=1;
    LL m;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%lld",&m);
        LL left=0;
        LL right=400000010;
        LL mid;
        while(left<=right)
        {
            mid=(left+right)>>1;
            if(fun(mid)>=m)
                right=mid-1;
            else
                left=mid+1;
        }
        printf("Case %d: ",k++);
        if(fun(left)!=m)
        printf("impossible\n");
        else
        printf("%lld\n",left);
    }
    return 0;
}

  

posted @   非我非非我  阅读(337)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· Vite CVE-2025-30208 安全漏洞
· 《HelloGitHub》第 108 期
· MQ 如何保证数据一致性?
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
点击右上角即可分享
微信分享提示