51Nod:1268 和为K的组合
第1行:2个数N, K, N为数组的长度, K为需要判断的和(2 <= N <= 20,1 <= K <= 10^9)
第2 - N + 1行:每行1个数,对应数组的元素A[i] (1 <= A[i] <= 10^6)
如果可以,输出:"Yes",否则输出"No"。
5 13
2
4
6
8
10
No

时间复杂度:O(2^n)
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6;
int a[maxn];
int n,k;
bool dfs(int i,int sum)
{
if(i==n) return sum==k;
if(dfs(i+1,sum)) return true;
if(dfs(i+1,sum+a[i])) return true;
return false;
}
int main()
{
cin>>n>>k;
for(int i=0;i<n;i++) cin>>a[i];
if(dfs(0,0)) cout<<"YES\n";
else cout<<"NO\n";
return 0;
}
51Nod:1268 和为K的组合的更多相关文章
- 51Nod 1268 和为K的组合
51Nod 1268 和为K的组合 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使 ...
- 51nod 1268 和为K的组合 dfs
题目: 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使他们的和为K.如果可以,输出:& ...
- 1268 和为K的组合 Meet in mid二分思路
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1268&judgeId=193772 给出n = 20个数,问其是 ...
- 51nod1268 和为K的组合(DFS)
1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 给出N个正整数组成的数组A,求能否从中选出若干个,使他们的和为K.如果可以 ...
- 经典K线组合图解 > 正文
日K线波段中上下影线的箱体操作法(完整) http://video.sina.com.cn/v/b/130809461-2486130757.html!!经典K线组合图解 > 正文 http:/ ...
- 51nod 1105(第K大数 二分套二分)
题目链接:http://www.51nod.com/onlineJudge/submitDetail.html#!judgeId=620811 参考自:https://blog.csdn.net/f_ ...
- 77. Combinations (java 求C(n,k)的组合,排除重复元素)
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 解析:同求全 ...
- 51Nod——T 1686 第K大区间
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 ...
- 51nod 1105:第K大的数
1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...
随机推荐
- jq 插入结构
一.插入 1. append $("#div").append('<a href="baidu.com">a</a>') ; // ...
- Codeforces 916B - Jamie and Binary Sequence (changed after round)
思路: 先取出二进制的每一位,判断总个数是不是小于等于k,如果大于k则不能构成. 通过观察可以发现,每一位的一个可以转换成下一位的两个,因为要使最大位尽可能小,所以如果最大位的所有的个数都可以转换成下 ...
- Vue.js 响应式原理
1. Vue2.x 基于 Object.defineProperty 方法实现响应式(Vue3 将采用 Proxy) Object.defineProperty(obj, prop, descript ...
- 【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(二)
五.Graphics layer 1.新增Graphics layer Graphics layer用于显示用户自定义绘制的点.线.面图形.使用时确保xaml文件中Graphics layer定义 ...
- WPF中为窗体设置背景图片
在WPF应用程式中,我们往往想为一个窗体设置一个中意的背景图,而不是单独的为这个Background设置成某种颜色或渐变颜色的背景. 在WPF 利用Expression Blend工具如何达到这种效果 ...
- 20170706xlVBA根据工资汇总表生成个人工资条
Sub NextSeven20170706001() Application.ScreenUpdating = False Application.DisplayAlerts = False Appl ...
- homestead 添加新站点
homestead 添加站点的时候遇到了坑,这里记录下来,也顺便给大家一个参考. 1. 首先修改homestead.yaml文件(虽然你有可能不知道这个文件在哪,但是我也不会帮你找的.) 2. 接着修 ...
- 『Scipy』常用方法记录
优化器使用教程 J = lambda wb: self.get_cost_grad(wb, X, Y_one_hot) theta = self.wb_init(X,Y_one_hot) result ...
- 安装torch-opencv
安装torch-opencv torch torch-opencv opencv-3.1.0 opencv-contrib 想在torch中使用光流法,于是就希望能够调用opencv中的光流代码,而t ...
- ubuntu安装环境软件全文档
1,安装apace2: sudo apt-get install apache2 2谷歌浏览器的安装:sudo apt-get install chromium-browser-dbg 3,国际版Q ...