Description

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.

Sample Input

2
1 2
3
1 3 5

Sample Output

4
8

分析:

题意:给出一个数n,n的规模不超过20(问题规模比较小),接下来一行给出N个数字,然后我们可以在任何一个数字前面放置+或-号。然后计算出一个值。

问由这组数经过不同的加减组合能得到多少种不同的答案。

由于问题的规模比较小,直接暴力就可以过,深搜到最后一个数后,肯它得出的答案有没有出现过(用一个map容器来保存)。

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std; long long a[25];
map<long long,int>m;
long long ans;
int n;
void dfs(int sum,int i)
{
if(i == n+1)
{
if(m[sum]==0) ///没有出现过
{
m[sum] = 1;
ans++;
}
return;
}
dfs(sum+a[i],i+1);
dfs(sum-a[i],i+1);
}
int main()
{
while(~scanf("%d",&n))
{
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
m.clear();
ans = 0;
dfs(0,0);
printf("%d\n",ans);
}
return 0;
}

最新文章

  1. Sicily 1031: Campus (最短路)
  2. 在 2016 年学 JavaScript 是一种什么样的体验?
  3. Spring+struts2的基础上继续加hibernate3的jar包
  4. 基于吉日嘎拉的通用权限管理Webform版老界面bug修复
  5. Servlet教程
  6. fflua更新-增加对引用的支持
  7. Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
  8. [代码片段]OSTU算法
  9. LINQ TO DATATABLE/DATASET基本操作之-简单查询
  10. kibana 修改Ico图标
  11. React测试Mixin
  12. (转)《深入理解java虚拟机》学习笔记6——类加载机制
  13. Qt Add ons Modules(听说QtSystem有接口可以获取 imei号)
  14. Jexus 5.8.3正式发布:Asp.Net Core在Linux上最友好服务器平台
  15. Springboot2新特性概述
  16. HNU 2015暑期新队员训练赛2 H Blanket
  17. shell 编程中的 知识点 - 突然一下子就明白很多东西了
  18. Qt sprintf_s函数格式化字符串出错
  19. 在swift中使用线程休眠
  20. EBS Archiving and Purging: You Know you need to

热门文章

  1. golang log
  2. adb常用命令(手机测试)
  3. virtualBox 安装 CentOs 6.8 以及网络配置
  4. Linux开发C语言规范
  5. 基于Vue、web3的以太坊项目开发及交易内幕初探 错误解决总结
  6. UVA 11880 Ball in a Rectangle(数学+平面几何)
  7. [转]学习win10的bash使用ssh连接远程服务器
  8. mysql初始(6)
  9. Android调用Java WebSevice篇之二
  10. Apache服务器的Options 的 Indexes FollowSymLinks详解