题目描述:Special array

 
输入n和m(20>=m>=n>0)求出所有满足以下方程的正整数数列 i1,i2,...,in,使i1+i2+...+in=m,且i1>=i2...>=in。例如:当n=4, m=8时,将得到如下5 个数列: 5 1 1 1 4 2 1 1 3 3 1 1 3 2 2 1 2 2 2 2

输入

输入只有一行,包含每个数列的元素个数n和数列元素的和m。

输出

按照字典逆序输出所有的数列,每个数列输出一行,每个数列元素用一个空格分开。

样例输入

4 8

样例输出

5 1 1 1
4 2 1 1
3 3 1 1
3 2 2 1
2 2 2 2 解题思路:很简单的一道DFS题目,减枝策略可以除了常规的策略之外,再加上当前搜索数目最大不超过剩余数-剩余位置个数。
 // specical array.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include <iostream>
#include <cstring>
using namespace std;
const int MAX = ;
int n, m, arr[MAX]; //最后一个数,已经填数的个数,数组总和
void DFS(int cur, int cnt, int sum)
{
if (cnt == n)
{
if (sum == m)
{
for (int i = ; i < n; i++)
{
if (i != n - ) cout << arr[i] << " ";
else cout << arr[i] << endl; }
}
return; }
for (int i = cur; i >= ; i--) // 从大到小开始搜索
{
if (i + sum + n - cnt - <= m) //n-cnt-1 是剩余大小
{
arr[cnt] = i;
DFS(i, cnt + , sum + i);
} }
} int main()
{
while (cin >> n >> m)
{
memset(arr, , sizeof(arr)); for (int i = m - n + ; i >= ; i--)
{
arr[] = i;
DFS(i, , i);
}
} return ;
}

ACM-Special Array的更多相关文章

  1. ACM会议列表与介绍(2014/05/06)

    Conferences ACM SEACM Southeast Regional Conference ACM Southeast Regional Conference the oldest, co ...

  2. 观V8源码中的array.js,解析 Array.prototype.slice为什么能将类数组对象转为真正的数组?

    在官方的解释中,如[mdn] The slice() method returns a shallow copy of a portion of an array into a new array o ...

  3. CF959D Mahmoud and Ehab and another array construction task 数学

    Mahmoud has an array a consisting of n integers. He asked Ehab to find another array b of the same l ...

  4. codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)

    题目传送门 题目大意:先提供一个数组,让你造一个数组,这个数组的要求是 1 各元素之间都互质  2  字典序大于等于原数组  3 每一个元素都大于2 思路: 1.两个数互质的意思就是没有公因子.所以每 ...

  5. Codeforces 959 D Mahmoud and Ehab and another array construction task

    Discription Mahmoud has an array a consisting of n integers. He asked Ehab to find another arrayb of ...

  6. 信息检索盛会 微软“领衔主演”——记ACM SIGIR 2013信息检索国际会议

    微软"领衔主演"--记ACM SIGIR 2013信息检索国际会议" title="信息检索盛会 微软"领衔主演"--记ACM SIGIR  ...

  7. SCI&EI 英文PAPER投稿经验【转】

    英文投稿的一点经验[转载] From: http://chl033.woku.com/article/2893317.html 1. 首先一定要注意杂志的发表范围, 超出范围的千万别投,要不就是浪费时 ...

  8. sdn测量论文简介

    Prelude: Ensuring Inter-Domain Loop-Freedom in SDN-Enabled Networks 来源:APNet: The Asia-Pacific Works ...

  9. CIKM Competition数据挖掘竞赛夺冠算法陈运文

    CIKM Competition数据挖掘竞赛夺冠算法陈运文 背景 CIKM Cup(或者称为CIKM Competition)是ACM CIKM举办的国际数据挖掘竞赛的名称.CIKM全称是Intern ...

随机推荐

  1. Django:验证码相关问题

    http://blog.csdn.net/csapr1987/article/details/7728315 https://zhidao.baidu.com/question/13837387222 ...

  2. CSS3实现放大缩小动画

    HTML <div> <div style="height: 35px;width:300px;background:orangered;border-radius: 4p ...

  3. 通过CrawlSpider对招聘网站进行整站爬取(拉勾网实战)

    爬虫首先要明确自己要爬取的网站以及内容 进入拉勾网的网站然后看看想要爬取什么内容职位,薪资,城市,经验要求学历要求,全职或者兼职职位诱惑,职位描述提取公司的名称 以及 在拉勾网的url等等 然后在na ...

  4. luogu P2766 最长不下降子序列问题

    第一问可以直接DP来做,联想上一题,线性规划都可以化为网络流?我们可以借助第一问的DP数组,来建立第二问第三问的网络流图,考虑每一种可能,都是dp数组中满足num[i]>=num[j]& ...

  5. Postgresql数据库数据简单的导入导出

    Postgresql数据库数据简单的导入导出 博客分类: DataBase postgres  命令操作: 数据的导出:pg_dump -U postgres(用户名)  (-t 表名)  数据库名( ...

  6. 树莓派开启VNC远程桌面

    分类: Raspberry Pi Linux2013-03-12 10:18 4288人阅读 评论(1) 收藏 举报   目录(?)[+]   1.安装VNC sudo apt-get install ...

  7. windowsService 程序

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. GNS3 icmp之严格路由

    路由配置: icmp记录路由抓取出接口的IP地址,最多可以抓取9个.ip协议头中的options为40个字节 R1 : conf t int f0/0 no shutdown ip add 192.1 ...

  9. Easy_Re

    这题比较简单,一波常规的操作之后直接上ida(小白的常规操作在以前的博客里都有所以这里不在赘述了),ida打开之后查看一下, 这里应该就是一个入口点了,接着搜索flag字符串, 上面的黄色的部分转换成 ...

  10. mongodb- 备份和导入备份

    一.使用 mongodump 命令备份数据 mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表 -o 文件存放路径 参数说明: -h 指明数据库宿主机 ...