(二分搜索 数论)(求阶乘里零个数对应的阶乘)light oj -- 1138

链接

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

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

#define N 0x3f3f3f3f

int Num0(int n)
{
    int sum = 0;

    while(n)
    {
        sum += n/5;
        n /= 5;
    }
    return sum;
}

void Search(int w) ///二分搜索
{
    int L=0, R=N, mid;

    while(L<=R)
    {
        mid = (L+R)>>1;

        if(w <= Num0(mid))
            R = mid - 1;
        else
            L = mid + 1;
    }

    if(Num0(L)==w)
        printf("%d\n", L);
    else
        printf("impossible\n");
}

int main()
{
    int iCase=1, n, t;
    scanf("%d", &t);
    while( t-- )
    {
        scanf("%d", &n);

        printf("Case %d: ", iCase++);
        Search(n);
    }
    return 0;
}
时间: 2024-08-29 16:40:03

(二分搜索 数论)(求阶乘里零个数对应的阶乘)light oj -- 1138的相关文章

!HDU 5317 求区间里两个数的质因数个数的gcd的最大值-预处理

题意:设一个数i的质因数个数为F(i),现给你一个区间[l~r],求max(F[i],F[j])  数据范围:10^6 分析: 预处理出所有的F[i],O(nlgn),10^6不会超时:然后查询用O(7),查询不能用O(n),因为有多个查询会超时. 区间问题减少查询时间复杂度多半类似一个区间的和用两个前缀和相减的方式,前缀和可以在预处理的时候计算,然后区间查询是用两个前缀和相减就行了.如sum[l~r]=sum[r]-sum[l-1],再比如之前的什么我想不起来一时,想起来加上. 代码: #in

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

hdu 1856 求集合里元素的个数 输出最大的个数是多少

求集合里元素的个数 输出最大的个数是多少 Sample Input41 23 45 61 641 23 45 67 8 Sample Output42 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # include <queue> 7 # define LL

算法题:阶乘尾零

题目描述 请设计一个算法,计算n的阶乘有多少个尾随零. 给定一个int n,请返回n的阶乘的尾零个数.保证n为正整数. 测试样例: 5 返回:1 #include <iostream> using namespace std; int Grial(int x) { int temp = x; int count2 = 0; int count5 = 0; while (temp) { count2 += temp / 2; temp /= 2; //这里有一个重点,如求n!中2的个数,coun

阶乘尾零

题目描述 请设计一个算法,计算n的阶乘有多少个尾随零. 给定一个int n,请返回n的阶乘的尾零个数.保证n为正整数. 测试样例: 5 返回:1 class Factor { public: int getFactorSuffixZero(int n) { // write code here int tmp = n; int count2 = 0; int count5 = 0; while(tmp){ count2 += tmp/2; tmp /= 2; } tmp = n; while(t

ACM数论-欧几里得与拓展欧几里得

ACM数论--欧几里得与拓展欧几里得 欧几里得算法: 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd(b,a%b). int gcd(int a,int b) { return b ? gcd(b,a%b) : a; } 扩展欧几里德算法: 基本算法:对于不完全为 0 的非负整数 a,b,gcd(a,b)表示 a,b 的最大公约数,必然存在整数对 x,y ,使

C. Learning Languages 求联通块的个数

C. Learning Languages 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <stack> 9 #include <queue&