Topic background
The factorial writing n! of N, which represents the product of all positive integers less than or equal to N.
Title Description
The factorial will grow quickly, such as the 13! must be stored with a 32-bit integer type, to 70! That is, the use of floating-point numbers will not survive. Your task is to find the first non-0-bit factorial. As an example:
5!=1*2*3*4*5=120, so 5! The top non-0 bit is 1.
7!=1*2*3*4*5*6*7=5040, so the top non-0 bit is 5.
Input/output format
Input format:
A total of one row, a positive integer not greater than 4,220 n
Output format:
A total of one row, the output n! the last non-0 bits.
Input and Output Sample input example # #:
7
Sample # # of output:
5
Description
The title translation comes from Nocow.
Usaco Training Section 3.2
Code
1#include <iostream>2#include <algorithm>3#include <cstring>4#include <cstdio>5#include <cstdlib>6#include <cmath>7 #defineM 2621448 using namespacestd;9 Ten intN; One A intMain () { - //freopen ("01.in", "R", stdin); -scanf"%d",&N); the - Long Longans=1; - for(Long LongI=1; i<=n;i++){ -ans*=i; + while(ans%Ten==0) ans/=Ten; -ans%=100000000; + } Acout<< (ans%Ten) <<Endl; atFclose (stdin); fclose (stdout);return 0; -}
Error, the sample is also wrong, the above code can be AC, that is, the output of the last one, sample 7→5
But this subject does not change the words can be so, no data do not know
1#include <iostream>2#include <algorithm>3#include <cstring>4#include <cstdio>5#include <cstdlib>6#include <cmath>7 #defineM 2621448 using namespacestd;9 Ten intN; One A intMain () { -Freopen ("01.in","R", stdin); - while(SCANF ("%d", &n) = =1){ the Long Longans=1; - for(Long LongI=1; i<=n;i++){ -ans*=i; - while(ans>=1000000000) ans/=Ten; + //cout<<ans<<endl; - } + while(ans>Ten) ans/=Ten; Acout<<ans<<Endl; at } - - -Fclose (stdin); fclose (stdout);return 0; -}
To the back of the accuracy may have a gap to be determined
Rokua P2726 factorial factorials label:water