Luntik and Subsequences 思维

在这里插入图片描述
题意 :

  • 给一序列,设元素和为sum,求有几个子序列满足子序列的元素和为sum - 1

思路 :

  • 1个元素1可以使得子序列元素和为原序列元素和-1,所以方案数与1的个数有关,且线性相关,元素0可有可无,所以方案数还要乘上 2 c n t 0 2^{cnt_0} 2cnt0
  • 所以答案是 c n t 1 ∗ 2 c n t 0 cnt_1 * 2^{cnt_0} cnt12cnt0,记得开long long
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

ll qmi(ll a, ll b)
{
    
    
    ll res = 1;
    while (b)
    {
    
    
        if (b & 1) res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

int main()
{
    
    
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    int _;
    cin >> _;

    while (_ -- )
    {
    
    
        int n;
        cin >> n;
        ll cnt0 = 0, cnt1 = 0;
        for (int i = 1; i <= n; i ++ )
        {
    
    
            int x;
            cin >> x;
            if (x == 0) cnt0 ++ ;
            if (x == 1) cnt1 ++ ;
        }
        cout << cnt1 * qmi(2ll, cnt0) << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_51448653/article/details/121234365