SGU[154] Factorial

Source: Internet
Author: User

DescriptionDescribe

You task is to find minimal natural number n, so, n! Contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*n. For example, 5! = contains one zero on the trail.

Your task is to find a minimum natural number n, so that the decimal below the n! end contains Q zeros. As you know, n! = 1 * 2 * ... * N. For example, 5! The end of = 120,120 contains 1 0.

InputInput

One number Q written in the input (0<=q<=10^8).

The input contains a number q (0 <= q <= 10^8).


OutputOutput

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

If there is no such n, output "no solution", otherwise output n.


Sample InputSample input

2


Sample OutputSample output

10

AnalysisAnalysis

Statistics n! The number of the end 0, in fact, as long as the factor 2 and 5 number of the minimum value, because only 2 * 5 will produce 0. However, the number of factor 2 is much larger than the number of factor 5, so just look at the number of factor 5.

Because the space limit given by the topic is only 4096KB, so the table can not be played, will be MLE. Baidu after the finding can be used two points.

Two minutes when statistics 1 to n this n number of factor 5 number, we use this method: ans = n/5 + n/5^2 + n/5^3 + ...

There are two ways to deal with this process, one is to play the table, the power of 5 to play the table out, there is a similar Qin algorithm using the idea, each ans + = N/5, while N/= 5.

It is important to note that when you enter 0, you want to output 1, because 0 is not a natural number. The upper and lower bounds of the two points should be [1, 2147483647].

SolutionSolution Solutions
#include <iostream>using namespace Std;const int MAX = 2147483647;int Main () {int n;while (cin >> N) {if (n = = 0) {cout << 1 << endl; continue;} int L = 1, r = MAX, ans = -1;while (l <= r) {int nmid = (L + R) >> 1;int ntmp = nmid, ncnt = 0;while (ntmp) {ncnt + = NTMP/5; Ntmp/= 5; }if (ncnt = = N) {ans = Nmid; r = nMid-1;} else if (ncnt < N) {L = Nmid + 1;} else {r = nMid-1;}} if (ans = =-1) {cout << "No solution" << Endl;} else {cout << ans << Endl;}} return 0;}

  

This topic began to WA several times, and later found that the upper bound of the two points is too small to set.

SGU[154] Factorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.