以二进制为本的骚气操作
n个数一共2的n次方种组合方式,以循环次数的二进制来控制选与不选,某位为1则选择某位的数字,最后看sum是否为0,是则ans+1;
# include <iostream>
# include <algorithm>
# include <cstdio>
# include <cstdlib>
# include <cmath>
using namespace std;
int main(){
int n, ans = 0;
scanf("%d", &n);
int a[100];
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
}
int k = pow(2, n);
for(int i = 1; i <= k; i++){
int temp = i, sum = 0, lab = 1;
while(temp > 0){
if(temp & 1){
sum += a[lab];
}
lab ++;
temp >>= 1;
}
if(sum == 0) ans++;
}
printf("%d
", ans);
return 0;
}