思路:
每个偶数的e!!的阶乘就是求对e不断/2=x,然后将所有x相加,直到e = 0.因为所要求的就是
所有e和e之前所有正偶数所包含的2的个数(
也就是从2,4,6,……,2*(n-1),2*n,中每个数字所包含的最多的2的个数,
将n/2 = x,x就是相当于将2~2*n的偶数数字都提取出一个2,然后令n = x(相当于在1~x中找偶数,就是寻找还能被2整除的数字的个数,因为1~x中奇数,偶数各半,或者相差1)
然后再寻找n中的偶数,依次递归下去寻找,直到n == 0.
)。
c++大数:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
string div(string ss){
string s1 = "",s2 = "";
int x,t1=0,t2=0,len = ss.length(),i;
for(i=0;i<len;i++){
x = (ss[i]-'0'+t2*10);
t1 = x/2;t2 = x%2;
s1 += (char)(t1+'0');
}
for(i=0;i<len&&s1[i]=='0';i++);
if(i>=len) return "0";
for(;i<len;i++) s2+=s1[i];
return s2;
}
string add(string s1,string s2){
if(s1.length()<s2.length()){
string tp = s1;
s1 = s2;
s2 = tp;
}
int i,j,l1 = s1.length(),l2 = s2.length();
for(i=l1-1,j=l2-1;i>=0;i--,j--){
int x = (s1[i]-'0')+(j>=0?(s2[j]-'0'):0);
if(x>=10){
s1[i] = (char)(x%10+'0');
if(i>0) s1[i-1]++;
else s1 = "1"+s1;
}else
s1[i] = (char)(x+'0');
}
return s1;
}
int main(void)
{
string ss,ans;
int T,i,j;
cin>>T;
while(T--){
cin>>ss;
ans = "0";
while(ss!="0"){
ss = div(ss);
ans = add(ans,ss);
//cout<<ss<<"<---->"<<ans<<endl;
}
cout<<ans<<endl;
}
return 0;
}
JAVA大数:
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
BigInteger zero = BigInteger.valueOf(0);
BigInteger two = BigInteger.valueOf(2);
int T = input.nextInt();
while(T!=0) {
T--;
BigInteger ans = BigInteger.valueOf(0);
BigInteger x = input.nextBigInteger();
while(x.compareTo(zero)!=0) {
x = x.divide(two);
ans = ans.add(x);
}
System.out.println(ans);
}
}
}