Lightoj 1138-trailing Zeroes (III) factorial end 0 Number & two points

Source: Internet
Author: User

Title Link: http://lightoj.com/volume_showproblem.php?problem=1138

Test instructions: give you a number n, then find a minimum number x, so that the end of x! has n 0; if there is no output impossible

Can be used to find the result, the focus is to find a number of the factorial of the end contains 0 of the number, must be related to the number of factors 5 and 2, the factor of 2 is significantly more than 5, so we only need a number of factorial factor in a total of how many 5 can be;

ll Find (ll x) {    0;      while (x)    {        + = x/5;         5 ;    }     return ans;}

For example 125! 125/5 = 25; So the item with 5 in 125! is 25*5+24*5+23*5+...+3*5+2*5+1*5 = 25!*5

So 125! At the end of the 25+5+1 = 31 0;

#include <stdio.h>#include<string.h>#include<algorithm>#include<math.h>typedefLong LongLL;#defineN 1000001using namespacestd;Const DoubleEPS = 1e-6; ll Find (ll x) {ll ans=0;  while(x) {ans+ = x/5; X/=5; }    returnans;}intMain () {intT, T =1, N; scanf ("%d", &T);  while(T--) {scanf ("%d", &N); LL ans=0, L =2, R =1000000000;  while(L <=R) {LL Mid= (l+r)/2; LL ret=Find (Mid); if(ret = =N) Ans=Mid; if(Ret >=N) R= Mid-1; ElseL= Mid +1; }        if(ans = =0) printf ("Case %d:impossible\n", t++); Elseprintf ("Case %d:%lld\n", t++, ans); }    return 0;}
View Code

Lightoj 1138-trailing Zeroes (III) factorial end 0 Number & two points

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.