美文网首页
2020-07-27 集合求和

2020-07-27 集合求和

作者: JalorOo | 来源:发表于2020-07-27 23:05 被阅读0次

    https://www.luogu.com.cn/problem/P2415

    知识点:

    先选出指定的一个元素,加入子集;

    首先,当子集里只有一个元素时,在其他剩余的元素中不能选出任何元素加入到子集中,所以对于每个元素来说,均有C^{0}_{n-1}次被选中。

    当子集里有2个元素时,在其他剩余的元素中选出1个元素加入到子集中,所以对于每个元素来说,均有C_{n−1}^{1} 次被选中。

    当子集里有3个元素时,在其他剩余的元素中选出2个元素加入到子集中,所以对于每个元素来说,均有C_{n-1}^{2}次被选中。

    当子集里有i(i\leqn)个元素时,在其他剩余的元素中选出i-1个元素加入到子集中,所以对于每个元素来说,均有C _{n−1}^{i−1}次被选中。

    所以共有∑_{i=1}^n C_{n−1}^{i−1}次被选中,即2^{n−1}次被选中。

    #include<iostream>
    #include <cstdio>
    #include<cmath>
    using namespace std;
    int a[31],i=0,j;
    long long s=0;
    
    int read(){
        int x = 0,f = 1;
        char c = getchar();
        while (c<'0'||c>'9') {
            if (c=='-') {
                f = -1;
            }
            c = getchar();
        }
        while (c>='0'&&c<='9') {
            x = x*10+c-'0';
            c = getchar();
        }
        return x*f;
    }
    
    long long qmi(int m, int k)
    {
        int res = 1, t = m;
        while (k)
        {
            if (k&1) res = res * t;
            t = t * t;
            k >>= 1;
        }
        return res;
    }
    
    int main(){
        //cout<<s;
        while(cin>>a[i++]);//合写cin>>a[i];i++;
        for(j=0;j<i;j++){
            s+=a[j];
        }
        s *= qmi(2,i-2);//注意,i-2!
        cout<<s;
        return 0;
    }
    /*
    6
    91 67 80
    78 89 98
    78 89 91
    88 99 77
    67 89 64
    90 66 82
    ============
    6 265
    4 264
    3 258
    2 244
    1 237
    */
    

    相关文章

      网友评论

          本文标题:2020-07-27 集合求和

          本文链接:https://www.haomeiwen.com/subject/yffxrktx.html