选择i个人的方案数是C(n,i)*i,选择n-i个人的方案数是C(n,n-i)*(n-i),这两项相加就是C(n,i)*n。
所以所有项做和就是(C(n,0)+C(n,1)+....+C(n,n))*n/2=2^(n-1)*n.
计算2的n-1次方需要快速幂
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
const int mod=1000000007;
LL Pow(LL n){
if(n==0) return 1;
LL cur=2;
LL res=1;
while(n){
if(n&1){
res*=cur;
res%=mod;
}
cur*=cur;
cur%=mod;
n>>=1;
}
return res;
}
int main(){
LL n;
int kase=1;
int T;
cin>>T;
while(T--){
scanf("%lld",&n);
LL res=Pow(n-1);
res*=n;
res%=mod;
printf("Case #%d: %lld\n",kase++,res);
}
return 0;
}
本文探讨了组合数学中的一项具体求和问题,通过分析选择i个人和n-i个人的方案数来推导总的方案数量,并给出了使用快速幂计算2的n-1次方的方法。
870

被折叠的 条评论
为什么被折叠?



