-->Backward Digit Sums

直接写中文了

Descriptions:

FJ 和 他的奶牛们在玩一个心理游戏。他们以某种方式写下1至N的数字(1<=N<=10)。 然后把相邻的数相加的到新的一行数。重复这一操作直至只剩一个数字。比如下面是N=4时的一种例子

    3   1   2   4

4 3 6

7 9

16

在FJ回来之前,奶牛们开始了一个更难的游戏:他们尝试根据最后结果找到开始的序列。这已超过了FJ的思考极限。

写一个程序来帮助FJ吧


Input

N和最后的和


Output

满足要求的一行序列。若有多种情况,输出字典序最小的一种


Sample Input

4 16

Sample Output

3 1 2 4

Hint

样例解释 
这里还有其他可能的排列,若 3 2 1 4,但 3 1 2 4 是字典序最小的
 
题目链接:
 
直接对这n个数进行全排列,然后对其求和,比对答案即可
既然是全排列 这里有一个全排列函数,可以直接调用,而且排列顺序就是字典顺序,很方便  next_permutation 参考:https://www.cnblogs.com/sky-stars/p/10948384.html 讲的很细
 
AC代码:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 1005
using namespace std;
int a[Maxn];
int n;
int ans,result;
void solve()
{
do
{
int b[Maxn];//注意一定要设置一个新数组,不然a数组会混乱
for(int i=;i<n;i++)
b[i]=a[i];
for(int i=n; i>=; i--)//求出这组数组的和
{
for(int j=; j<i; j++)
{
b[j]=b[j]+b[j+];
}
}
result=b[];//b[0]即是这组数字之和
if(result==ans)//得到正确答案,输出数组即可
{
for(int i=; i<n; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
break;
}
}
while(next_permutation(a,a+n));//全排列
}
int main()
{
cin>>n>>ans;
for(int i=; i<n; i++)//输入数组
a[i]=i+;
solve();
}

【POJ - 3187】Backward Digit Sums(搜索)的更多相关文章

  1. POJ 3187 Backward Digit Sums 枚举水~

    POJ 3187  Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3   1   2   4  他可以相邻 ...

  2. 穷竭搜索:POJ 3187 Backward Digit Sums

    题目:http://poj.org/problem?id=3187 题意: 像这样,输入N : 表示层数,输入over表示最后一层的数字,然后这是一个杨辉三角,根据这个公式,由最后一层的数,推出第一行 ...

  3. poj 3187 Backward Digit Sums(穷竭搜索dfs)

    Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...

  4. POJ 3187 Backward Digit Sums (dfs,杨辉三角形性质)

    FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N < ...

  5. POJ 3187 Backward Digit Sums

    暴力DFS+验证. 验证如果是暴力检验可能复杂度会太高,事实上可以o(1)进行,这个可以o(n*n)dp预处理. #include<cstdio> #include<cstring& ...

  6. POJ 3187 Backward Digit Sums (递推,bruteforce)

    第1行j列的一个1加到最后1行满足杨辉三角,可以先推出组合数来 然后next_permutation直接暴. #include<cstdio> #include<iostream&g ...

  7. Luogu P1118 [USACO06FEB]数字三角形 Backward Digit Sums | 搜索、数学

    题目链接 思路:设一开始的n个数为a1.a2.a3...an,一步一步合并就可以用a1..an表示出最后剩下来的数,不难发现其中a1..an的系数恰好就是第n层杨辉三角中的数.所以我们可以先处理出第n ...

  8. BZOJ 1653 [Usaco2006 Feb]Backward Digit Sums ——搜索

    [题目分析] 劳逸结合好了. 杨辉三角+暴搜. [代码] #include <cstdio> #include <cstring> #include <cmath> ...

  9. Backward Digit Sums(POJ 3187)

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5495   Accepted: 31 ...

随机推荐

  1. 【jqGrid】翻页获取选中的值

    1.实现效果: 点击确定所选按钮之后,获取翻页选中的题库表号 2.关键代码 onSelectAll: function (aRowids, status) { }, onSelectRow: func ...

  2. SparkSQL之UDF使用

    package cn.piesat.test import org.apache.spark.sql.SparkSession import scala.collection.mutable.Arra ...

  3. confirm 弹出信息

    if(confirm("真的要删除吗?")){ alert("点击了确认按钮"); } else{ alert("点击了取消按钮"); } ...

  4. codevs 2977 二叉堆练习1x

    时间限制: 10 s 空间限制: 32000 KB 题目等级 : 白银 Silver       题目描述 Description 已知一个二叉树,判断它是否为二叉堆(小根堆) 输入描述 Input ...

  5. 阿sa

    # jieba库概述 .jieba是优秀的中文分词第三方库.需要额外安装. pip install jieba # jieba库三种分词模式 精确模式:把文本精确的切分开,不存在冗余单词. 全模式:把 ...

  6. elastic search&logstash&kibana 学习历程(二)es基础知识

    简介:es的index索引,document文档对象,副本,多节点集群等基础知识 1.通俗的解释: 在Elasticsearch中,文档归属于一种类型(type),而这些类型存在于索引(index)中 ...

  7. CodeForces–830A--二分,贪心

    Office Keys time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  8. 「Luogu P5602」小E与美食

    题目链接 戳我 \(Solution\) 这道题只需要枚举吃\(k\)个美食,最后在取前\(k\)大的美味值.对于每个算出答案后取\(max\) \(Code\) #include<bits/s ...

  9. Keras学习笔记二:保存本地模型和调用本地模型

    使用深度学习模型时当然希望可以保存下训练好的模型,需要的时候直接调用,不再重新训练 一.保存模型到本地 以mnist数据集下的AutoEncoder 去噪为例.添加: file_path=" ...

  10. BZOJ3033太鼓达人

    第一问,1<<k,谁都看得出来. 毫无思路,暴搜,枚举每一个数列,Hash加map判断是否重复,拿到30,打表都打不出来. #include <iostream> #inclu ...