Description
Input
Output
Sample Input
Sample Output
HINT
Source
思路:
方案數就是Ann(排列組合中的) ,能被2^m整除,求m,即爲求 方案數 能被2整除的個數
此題中方案數爲n的階乘,求每個數能被2整除的個數,由於奇數不能被2整除,所以算的時候可以忽略奇數
對階乘表達式一次一次求解相加 ,直接對n/2即可求出整除的個數
例如原來n的階乘爲 10 9 8 7 6 5 4 3 2 1(省略乘號)
第一次操作後 5 \ 4 \ 3 \ 2 \ 1 \ 總數加 5
第二次操作後 \ \ 2 \ \ \ 1 \ \ \ 總數加 2
第三次操作後 \ \ 1 \ \ \ \ \ \ \ 總數加 1
第四次操作後 \ \ \ \ \ \ \ \ \ \ 總數加 0
代碼:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
int ans=0;
while(n){
int num=n/2;//求n的階乘中有多少數能被2整除
// printf("=== %d\n",num);
ans+=num;
n/=2;//對所有偶數除2,表示現在的階乘表達式還有幾個偶數
}
printf("%d\n",ans);
}
return 0;
}