light oj 1138 - Trailing Zeroes (III)(阶乘末尾0)

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

水题一发,直接二分查找。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL long long
using namespace std;
LL n;
LL erfen()
{
    LL ans=-1, l=5, r=400000020;//想想r的值为什么这样定(因为此时r!等于10^8+1)
    while(l<=r)
    {
        LL mid=(l+r)/2;
        LL sum=0, s=mid;
        while(s>0)
            sum+=s/5,s/=5;
        if(sum==n)
        {
            r=mid-1;
            ans=mid;
        }
        else if(sum>n)
            r=mid-1;
        else
            l=mid+1;
    }
    return ans;
}
int main()
{
    int T, t=1;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%lld", &n);
        LL s=erfen();
        if(s!=-1)
        printf("Case %d: %lld\n", t++, s);
        else
            printf("Case %d: impossible\n", t++);
    }
}

原文地址:https://www.cnblogs.com/zhulei2/p/8206494.html

时间: 2024-11-03 22:50:16

light oj 1138 - Trailing Zeroes (III)(阶乘末尾0)的相关文章

LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 &amp; 二分

题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出impossible 可以用二分求结果,重点是求一个数的阶乘中末尾含有0的个数,一定和因子5和2的个数有关,因子为2的明显比5多,所以我们只需要求一个数的阶乘的因子中一共有多少个5即可; LL Find(LL x) { LL ans = 0; while(x) { ans += x/5; x /=

Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】

1138 - Trailing Zeroes (III) PDF (English) Statistics Forum 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. F

Light oj 1138 - Trailing Zeroes (III) 【二分查找 &amp;&amp; N!中末尾连续0的个数】

1138 - Trailing Zeroes (III) PDF (English) Statistics Forum 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. F

Light OJ 1028 Trailing Zeroes (I) 求n因子数

今天真机调试的时候莫名其妙遇到了这样的一个问题: This product type must be built using a provisioning profile, however no provisioning profile matching both the identity "iPhone Developer" and the bundle identifier..... 具体如下图所示: 十分蛋疼, 发现不管是从网上下的demo, 还是自己的过程.凡事真机测试的时候都

[LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits:Special thanks to @ts for adding this problem and creating all test cases. 这道题并没有什么难度,是让求一个数的阶乘末尾0的个数,也就是要找乘数中10的个数,

Light OJ 1028 - Trailing Zeroes (I) (数学-因子个数)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1028 题目大意:n除了1有多少个因子(包括他本身) 解题思路:对于n的每个因子, 可以用n的所有素因子排列组合而来, n = (a1x1) * (a2 x2) * (a3x3)...*(anxn), 其中ai为n的素因子,那么n的因子的个数等同于(x1 + 1) * (x2 + 1) * (x3 + 1) ... * (xn + 1)中排列, 因为其中一种排列肯定为所有素因子

LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】

1138 - Trailing Zeroes (III) PDF (English) Statistics Forum 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. F

Trailing Zeroes (III)(lightoj 二分好题)

1138 - Trailing Zeroes (III)   PDF (English) Statistics Forum 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.

Trailing Zeroes (III) -;lightoj 1138

Trailing Zeroes (III)   PDF (English) Statistics Forum 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 ex