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. 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



PROBLEM SETTER: JANE ALAM JAN

题意:给你一个数字,这个数字代表N!后面有几个0。给出这个数字,计算N的值。

解题思路:

任何质因数都可以写成素数相乘的形式。所以计算一个数的阶乘后面几个0,只需计算这个数包含多少5即可。(关于这点不清楚的点:点击打开链接)。

可以用二分法,找出这个点。想到用二分这道题也就没什么难度了。

AC代码;

<span style="font-size:18px;">#include <stdio.h>
#include <math.h>
#include <vector>
#include <queue>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long LL;

LL solve(LL n)
{
    LL num=0;
    while(n){
        num+=n/5;
        n/=5;
    }
    return num;
}

LL er(LL n)
{
    LL x=1;
    LL y=1844674407370;
    LL mid;
    LL res=-1;
    while(y>=x){
        mid=(x+y)/2;
        LL ans=solve(mid);
        if(ans==n){
            res=mid;
            y=mid-1;
            //return mid;
        }
        else if(ans>n){
            y=mid-1;
        }
        else if(ans<n){
            x=mid+1;
        }
    }
    return res;
}
int main()
{

    int t;
    scanf("%d",&t);
    int xp=1;
    while(t--){
        LL n;
        scanf("%lld",&n);
        LL ans=er(n);
        if(ans==-1)  printf("Case %d: impossible\n",xp++);
        else printf("Case %d: %d\n",xp++,ans);
    }
    return 0;
}
</span>

版权声明:本文为博主原创文章,转载请注明出处。

时间: 2024-10-11 17:49:02

LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】的相关文章

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

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.

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

172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (1) class Solution { public: int trailingZeroes(int n) { int ans = 0; for(long long i = 5; n / i; i *= 5) { ans += n / i; }

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)(阶乘末尾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)

LightOJ 1340 - Story of Tomisu Ghost 阶乘分解素因子

http://www.lightoj.com/volume_showproblem.php?problem=1340 题意:问n!在b进制下至少有t个后缀零,求最大的b. 思路:很容易想到一个数通过分解素因子可以得到最大的指数.那么问题关键在于求得n!的素因子的指数,找到指数大于t的所有素因子,再将那些指数除去t,剩下的数就是最大的b了.分解阶乘时,对n不断除素数p,直到n为0时,此时商的和即该素因子的指数. /** @Date : 2016-11-30-19.35 * @Author : Lw

Trailing Zeroes (III)

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)