K - Birthday Puzzle (遍历所有子集的位运算)

一个美丽的女孩在生日时收到了朋友送的魔法盒子,要解开上面的谜题才能打开。谜题要求对数组的所有子集进行OR操作并求和。输入包含数组大小n和元素,输出是所有子集OR操作后的结果之和。第一例中,数组为[1, 2, 3],子集的OR操作结果为1, 2, 3, 3, 3, 3,答案是18。" 125687631,7479038,Kubernetes实战:部署Ceph云原生存储系统,"['云原生', 'k8s', 'Ceph', '存储']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 aa, 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),n(1≤n≤20),the size for array aa The second line contains nnintegers, ai(1≤ai≤105)ai(1≤ai≤105) the array aa elements

Output

Help the beautiful girl to find the answer for the puzzle.

Example

Input

3
1 2 3

Output

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

 

题目大意:将数组的每个子集进行OR操作然后加起来

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
const double pi = acos(-1.0);
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e4 + 5;
int a[100];
int n;
ll ans;
void dfs(int x, int y)
{
    for(int i = x + 1;i<=n;i++)
    {
        ll z = a[i] | y;
        ans += z;
        dfs(i, z);
    }
}
int main()
{
    ans = 0;
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i];
    }
    for(int i=1;i<=n;i++)
    {
        ans += a[i];
        dfs(i, a[i]);
    }
    cout << ans << endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值