Birthday Puzzle(异或+子集)

编程入门 行业动态 更新时间:2024-10-27 23:28:19

Birthday Puzzle(异或+<a href=https://www.elefans.com/category/jswz/34/1758070.html style=子集)"/>

Birthday Puzzle(异或+子集)

Birthday Puzzle

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 nn integers, 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
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:

思路:
递归找集合顺便把答案加起来

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>int n, a[25];
long long re;
void f(int cur, int x)
{int i;long long y;for(i = cur + 1; i<n; i++){y = x|a[i];re += y;f(i, x|a[i]);}
}int main()
{int i;re = 0;scanf("%d", &n);for(i=0;i<n;i++){scanf("%d", &a[i]);re += a[i];}for(i=0;i<n;i++){f(i, a[i]);}printf("%lld\n", re);return 0;
}

更多推荐

Birthday Puzzle(异或+子集)

本文发布于:2024-02-12 00:12:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1684515.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子集   Birthday   Puzzle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!