Given n numbers, your task is to insert '+' or '-' in front of each number to construct expressions. Note that the position of numbers can be also changed.
You can calculate a result for each expression. Please count the number of distinct results and output it.
Input
There are several cases. For each test case, the first line contains an integer n (1 ≤ n ≤ 20), and the second line contains n integers a1,a2, ... ,an(-1,000,000,000 ≤ ai ≤ 1,000,000,000).
Output
For each test case, output one line with the number of distinct results.
#include<iostream>
#include<algorithm>
#include<set>
#include<stdio.h>
#include<string.h>
using namespace std;
long long int a[25];
set <long long int> S;
long long int ans;
int n;
void dfs(int sum,int i)
{
if(i == n+1)
{
S.insert(sum);
return;
}
dfs(sum+a[i],i+1);//加法
dfs(sum-a[i],i+1);//减法
}
int main()
{
while(~scanf("%d",&n))
{
S.clear();//清除S集合
for(int i = 1; i <= n; i++)
scanf("%lld",&a[i]);
ans = 0;
dfs(0,0);
printf("%d\n",S.size());
}
return 0;
}