Any number inside the computer is represented by a binary representation, which can be used to quickly determine the position of the lowest bit 1 in the binary representation of the n!.
Solution One:
Divides the binary number of a number by 2, and if the end of the binary number is 0, it can be divisible, otherwise it cannot be divisible.
Therefore, the position of the lowest bit 1 in the binary representation of n! is to find out how many factorization 2 are in n!
The following is code 1:
#include <iostream>using namespace Std;int main (void) {int n,m;m=0;cin>>n;while (n) {n>>=1;m+=n;} cout<< m+1 <<endl; M is the number of n! factor 2, so the final result is to add 1return 0;}
Solution Two:
N! contains the number of factorization 2, equal to n (the number of 1 in the binary representation of N)
(This rule is self-proof)
Code Listing 2:
#include <iostream>using namespace Std;int main (void) {int n,i,j,m; m=0; j=0x1; cin>>n; I=n; while (i) {m+= (i&j);i>>=1; } cout << n-m+1 <<endl; M is the number of n! factor 2, so the final result is to add 1 return 0;
This article is from the "hacker" blog, make sure to keep this source http://anglecode.blog.51cto.com/5628271/1631147
Programming beauty 2--n! The location of the lowest bit 1 in binary representation