Problem Description
Today is the Birthday of a beautiful girl, she's happy and she's telling her friends loudly to bring her birthday gifts. One of her best friends who is fond of puzzles decided to bring her a very special gift, a magic box, this box cannot be opened unless the beautiful girl solves the mysterious puzzle written on the box and writes the answer on a small piece of paper under where the puzzle is written.
"You are given an array a, the answer is obtained by doing OR operation to the numbers in each subset of array aa, then by summing all the subset ORing results". Help the beautiful girl to find the answer so she can open the magic box and continue celebrating her blessed birthday.
Input
The first line contains integer n(1≤n≤20),, the size for array a The second line contains n integers, ai(1≤ai≤105) the array a elements
Output
Help the beautiful girl to find the answer for the puzzle.
Examples
Input
3
1 2 3Output
18
Note
Note: a subset of an array aa is another array that can be obtained by removing zero or more elements from aa.
The first sample subsets:
1
2
3
1|2 = 3
1|3 = 3
2|3 = 3
1|2|3 = 3
Answer = 1+2+3+3+3+3+3 = 18
Where | is the OR operation.
For more information about the OR operation use this link: https://en.wikipedia.org/wiki/Bitwise_operation#OR
题意:给出 n 个数,求这个 n 个数相互或的和
思路:由于 n 最多到 20,因此借助 vector 进行 dfs 即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
const int MOD = 1E9+7;
const int N = 20+5;
const int dx[] = {1,-1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int n;
int a[25];
vector<int> V;
LL res ;
void cal(int pos) {
if(pos==n) {
int x=0;
for(int i=0;i<V.size();i++)
x|=V[i] ;
res+=x;
return;
}
V.push_back(a[pos]) ;
cal(pos+1);
V.pop_back() ;
cal(pos+1) ;
}
int main(){
scanf("%d",&n) ;
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
cal(0) ;
printf("%lld\n",res) ;
return 0;
}