SGU154——Factorial (poj1401变形题——数论+二分)

原创 2013年12月03日 18:33:31

154. Factorial

time limit per test: 0.5 sec.
memory limit per test: 4096 KB
input: standard input
output: standard output



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
One number Q written in the input (0<=Q<=10^8).

Output
Write "No solution", if there is no such number N, and N otherwise.

Sample test(s)

Input

2
Output

10
 
 
 
 
题目描述:输入一个数n、求某个数的阶乘后面有n个0的数p;
 
会了poj1401就好办了、直接来个二分搜素。
 
 
 
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<math.h>
#include<vector>
#include<map>
#include<deque>
#include<list>
#define N 0x7fffffff
using namespace std;
int num0(int n)
{
    int count=0;
    while(n)
    {
        count+=n/5;
        n/=5;
    }
    return count;
}
void find(int w)
{
    int left=0,right=N,mid;
    while(left<=right)
    {
        mid=(left+right)>>1;//位运算速度快
//        mid=(left+right)/2;
        if(num0(mid)>=w)
        right=mid-1;
        else
        left=mid+1;
    }
    if(num0(left)==w)
    printf("%d\n",left);
    else
    puts("No solution");
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
        printf("1\n");
        else
        {
            find(n);
        }
    }
    return 0;
}










版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

ACM-简单题之Factorial——poj1401

ACM 简单题 Factorial poj1401

SGU - 154 - Factorial (数论)

154. Factorial time limit per test: 0.25 sec. memory limit per test: 4096 KB input: standar...

POJ - 1401 SPOJ - FCTRL Factorial

阶乘

UVALive2158 POJ1401 HDU1124 ZOJ2024 Factorial【分析思维】

Factorial Time Limit: 1500MS   Memory Limit: 65536K Total Submissions: 16314   Accepted: 10052...

poj 1401 Factorial

DescriptionThe most important part of a GSM network is so called Base Transceiver Station (BTS). The...

POJ 1775 sum of Factorial (数论)

链接:http://poj.org/problem?id=1775 Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1...

数论基础(POJ1401)

Factorial Time Limit: 1500MS   Memory Limit: 65536K Total Submissions: 16192   Accepted: 9984 ...

poj1080——Human Gene Functions(LCS变形)

DescriptionIt is well known that a human gene can be considered as a sequence, consisting of four nu...

poj2412——Constructing Roads(最小生成树变形)

DescriptionThere are N villages, which are numbered from 1 to N, and you should build some roads suc...

poj(2184)——Cow Exhibition(01背包变形)

其实我想说这道题我觉得我自己并没有深刻的理解。但是今天做了一下,先把现在的想法记录下来 。 题目的大致意思是: 有N头牛,每头牛都有一个s值代表智商值,f值代表它的幽默值。 然后问你智商值和幽默...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)