Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 10^4^ coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (<=10^4^, the total number of coins) and M(<=10^2^, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the face values V~1~ <= V~2~ <= ... <= V~k~ such that V~1~ + V~2~ + ... + V~k~ = M. All the numbers must be separated by a space, and there must be no extra space at the end of the line. If such a solution is not unique, output the smallest sequence. If there is no solution, output "No Solution" instead.

Note: sequence {A[1], A[2], ...} is said to be "smaller" than sequence {B[1], B[2], ...} if there exists k >= 1 such that A[i]=B[i] for all i < k, and A[k] < B[k].

Sample Input 1:

8 9
5 9 8 7 2 3 4 1

Sample Output 1:

1 3 5

Sample Input 2:

4 8
7 2 4 3

Sample Output 2:

No Solution
dfs加剪枝,先整体排序,把比m大的去掉,如果加到一个数比m大了就返回,每一步同一个数只算一次,比如说到了i步,选了第j个数,继续dfs,当返回到这里时,继续尝试i+1,如果i+1和i相同直接跳,因为在后边已经测试过这种情况了。
也可以一开始就计算总和,如果总的和小于m就不用dfs了。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
int n,m;
int s[],ans[];
int flag;
void dfs(int k,int last,int sum) {
if(flag)return;
if(sum > m)return;
if(sum == m) {
flag = k;
return;
}
for(int i = last;i < n;i ++) {
if(flag)return;
if(i != last && s[i] == s[i - ])continue;
ans[k] = s[i];
dfs(k + ,i + ,sum + s[i]);
}
}
int main() {
scanf("%d%d",&n,&m);
for(int i = ;i < n;i ++) {
scanf("%d",&s[i]);
}
sort(s,s + n);
while(s[n - ] > m)n --;
dfs(,,);
if(!flag)printf("No Solution\n");
else {
printf("%d",ans[]);
for(int i = ;i < flag;i ++) {
printf(" %d",ans[i]);
}
}
}

1068 Find More Coins (30)(30 分)的更多相关文章

  1. PAT 甲级 1068 Find More Coins (30 分) (dp,01背包问题记录最佳选择方案)***

    1068 Find More Coins (30 分)   Eva loves to collect coins from all over the universe, including some ...

  2. PAT 1068 Find More Coins[dp][难]

    1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...

  3. PAT 甲级 1068 Find More Coins(0,1背包)

    1068. Find More Coins (30) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva l ...

  4. 1068 Find More Coins

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  5. etectMultiScale(gray, 1.2,3,CV_HAAR_SCALE_IMAGE,Size(30, 30))

    # 函数原型detectMultiScale(gray, 1.2,3,CV_HAAR_SCALE_IMAGE,Size(30, 30)) # gray需要识别的图片 # 1.03:表示每次图像尺寸减小 ...

  6. 1048 Find Coins (25 分)

    1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other p ...

  7. c# 时间格式处理,获取格式: 2014-04-12T12:30:30+08:00

    C#  时间格式处理,获取格式: 2014-04-12T12:30:30+08:00 一.获取格式: 2014-04-12T12:30:30+08:00 方案一:(局限性,当不是当前时间时不能使用) ...

  8. java.time.format.DateTimeParseException: Text '2019-10-11 12:30:30' could not be parsed at index 10

    java.time.format.DateTimeParseException: Text '2019-10-11 12:30:30' could not be parsed at index 10 ...

  9. 1068 Find More Coins (30分)(dp)

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

随机推荐

  1. LNMP环境搭建(二:MySQL)

    1.获取MySQL官方的rpm包,根据操作系统与需要安装的MySQL版本进行选择,官方地址:https://www.mysql.com/downloads/ # cd /usr/local/src # ...

  2. Python PhatomJS 和Selenium动态加载页面 获取图片内容

    如果您觉得感兴趣的话,可以添加我的微信公众号:一步一步学Python![](http://images2017.cnblogs.com/blog/993869/201711/993869-201711 ...

  3. POJ 1518 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】

    链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  4. java.time.format.DateTimeFormatter

    Java的日期与时间 DateTimeFormatter类是Java 8中日期时间功能里,用于解析和格式化日期时间的类,位于java.time.format包下.   1.预定义的DateTimeFo ...

  5. PAT 1062. 最简分数(20)

    一个分数一般写成两个整数相除的形式:N/M,其中M不为0.最简分数是指分子和分母没有公约数的分数表示形式. 现给定两个不相等的正分数 N1/M1 和 N2/M2,要求你按从小到大的顺序列出它们之间分母 ...

  6. linux c编程:管道

    2在前面介绍过,进程之间交换信息的唯一途径就是传送打开的文件.可以经由fork或者exec来传送.这一章将介绍新的进程共享方式 每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都 ...

  7. C调用Lua中的函数解析table

    Passing Tables to Lua Functions A use case that happens often is the passing of tables to and from L ...

  8. 坑爹的shell 空格

    shell 空格很敏感,被线上代码坑了,占个位,回头好好整理一下

  9. spring ioc和aop理解

    1.IOC 表示控制反转. 简单点说就是原来的对象是在要使用之前通过在代码里通过new Something()的方式创建出来的: IOC则是由spring容器创建同一创建,在程序要使用到该对象的时候, ...

  10. 通过套接字(socket)和UDP协议实现网络通信

    UDP---用户数据报协议,是一个简单的面向数据报的运输层协议.(无连接.封包.大小限制.速度快). 一.UDP协议的特点: 将数据及源和目的地封装成数据包中,不需要建立连接. 每个数据报的大小限制在 ...