P2415 集合求和

P2415 集合求和
显然,一共有2^n个子集,对于其中的一个确定的元素,它不在的集合有2^(n-1),相当于有n-1元素,那么它存在的集合有,2^n-2^(n-1)==2^(n-1),那么集合的和为sum*2^(n-1).

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 long long p(long long t,long long x)
 5 {
 6     while(x%2==0)
 7     {
 8         t*=t;
 9         x>>=1;
10     }
11     long long result=1;
12     while(x>0)
13     {
14         if(x%2==1)
15         {
16             result*=t;
17         }
18         t*=t;
19         x>>=1;
20     }
21     return result;
22 }
23 
24 int main()
25 {
26   long long n=0;long long x;
27   long long sum=0;
28   while(scanf("%lld",&x)!=EOF)
29   {
30       sum+=x;
31       n++;
32   }
33    cout<<sum*p(2,n-1);
34   return 0;
35 }

 

posted @ 2017-11-04 19:55  WeiAR  阅读(547)  评论(0编辑  收藏  举报