Codeforces Round #750 (Div. 2) - B. Luntik and Subsequences - 题解
Posted Tisfy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #750 (Div. 2) - B. Luntik and Subsequences - 题解相关的知识,希望对你有一定的参考价值。
目录
Codeforces Round #750 (Div. 2) - B. Luntik and Subsequences
传送门
Time Limit: 1 seconds
Memory Limit: 256 megabytes
Problem Description
Luntik came out for a morning stroll and found an array a of length n n n. He calculated the sum s of the elements of the array ( s = ∑ i = 1 n a i (s=\\sum^n_{i=1}a_i (s=∑i=1nai). Luntik calls a a a subsequence of the array a nearly full if the sum of the numbers in that subsequence is equal to s − 1 s−1 s−1.
Luntik really wants to know the number of nearly full subsequences of the array a a a. But he needs to come home so he asks you to solve that problem!
A sequence x x x is a subsequence of a sequence y y y if x x x can be obtained from y y y by deletion of several (possibly, zero or all) elements.
Input
The first line contains a single integer t ( 1 ≤ t ≤ 1000 ) t (1≤t≤1000) t(1≤t≤1000) — the number of test cases. The next 2 ⋅ t 2⋅t 2⋅t lines contain descriptions of test cases. The description of each test case consists of two lines.
The first line of each test case contains a single integer n ( 1 ≤ n ≤ 60 ) n (1≤n≤60) n(1≤n≤60) — the length of the array.
The second line contains n n n integers a 1 , a 2 , … , a n ( 0 ≤ a i ≤ 1 0 9 ) a_1,a_2,…,a_n (0≤a_i≤10^9) a1,a2,…,an(0≤ai≤109) — the elements of the array a a a.
Sample Input
5
5
1 2 3 4 5
2
1000 1000
2
1 0
5
3 0 2 1 1
5
2 1 0 3 0
Sample Onput
1
0
2
4
4
Note
In the first test case, s = 1 + 2 + 3 + 4 + 5 = 15 s=1+2+3+4+5=15 s=1+2+3+4+5=15, only ( 2 , 3 , 4 , 5 2,3,4,5 2,3,4,5) is a nearly full subsequence among all subsequences, the sum in it is equal to 2 + 3 + 4 + 5 = 14 = 15 − 1 2+3+4+5=14=15−1 2+3+4+5=14=15−1.
In the second test case, there are no nearly full subsequences.
In the third test case, s = 1 + 0 = 1 s=1+0=1 s=1+0=1, the nearly full subsequences are ( 0 0 0) and () (the sum of an empty subsequence is 0 0 0).
题目大意
给你一个长度为
n
n
n的序列,问你它有多少个 和为序列的和-1
的子序列。
解题思路
知道原始序列后,我们可以立即求出原始序列的和(记为 s s s),因此我们只需要找和为 s − 1 s-1 s−1的子序列(即为原始序列去掉一个 1 1 1和若干个 0 0 0)
我们可以统计出原始序列共有多少个 1 1 1(记为 n u m 1 num1 num1)
对于每一个 1 1 1,去掉这个 1 1 1的话,剩下的元素还能去掉若干个 0 0 0
假如共有 n u m 0 num0 num0个 0 0 0,那么去掉若干个 0 0 0的方案数是 2 n u m 0 2^{num0} 2num0
也就是说,不论去掉哪一个 1 1 1,都有 2 n u m 0 2^{num0} 2num0个去掉 0 0 0的方法,使得剩下的子序列的和为 s − 1 s-1 s−1因为原始共有 n u m 1 num1 num1个 1 1 1,根据乘法原理,去掉 1 1 1个 1 1 1和若干个 0 0 0的方案数为 n u m 1 × 2 n u m 0 num1\\times2^{num0} num1×2num0
所以直接输出 n u m 1 × 2 n u m 0 num1\\times2^{num0} num1×2num0即可。
AC代码
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
ll Pow[64] = {1}; // Pow[i]是2的i次方
void init() {
for (int i = 1; i < 63; i++) {
Pow[i] = Pow[i - 1] * 2;
}
}
int a[100];
int main() {
init();
int N;
cin >> N; // 共N组样例
while(N--) {
int n;
cd(n); // scanf("%d", &n); 这组有n个元素
int num1 = 0; // 1的个数
int num0 = 0; // 0的个数
fi(i, 0, n) { // for (int i = 0; i < n; i++)
cd(a[i]); // scanf("%d", &a[i]);
if (a[i] == 1) num1++;
if (a[i] == 0) num0++;
}
ll ans = Pow[num0] * num1; // num1 * 2 ^ num0
printf("%lld\\n", ans);
}
return 0;
}
原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/120939771
以上是关于Codeforces Round #750 (Div. 2) - B. Luntik and Subsequences - 题解的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #750 (Div. 2) - B. Luntik and Subsequences - 题解
Codeforces Round #750 (Div. 2)
Codeforces Round #750 (Div. 2) E(dp)
Codeforces Round #436 E. Fire(背包dp+输出路径)