题目描述

FJ and his cows enjoy playing a mental game. They write down the numbers from 11 to N(1 ≤ N ≤ 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4) might go like this:

    3   1   2   44   3   67   916

Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N . Unfortunately, the game is a bit above FJ's mental arithmetic capabilities.

Write a program to help FJ play the game and keep up with the cows.

有这么一个游戏:

写出一个 1 至 N的排列 a_i,然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少 1,直到只剩下一个数字位置。下面是一个例子:

3,1,2,4

4,3,6

7,9

16

最后得到 16 这样一个数字。

现在想要倒着玩这样一个游戏,如果知道 N,知道最后得到的数字的大小 sum,请你求出最初序列 a_i,为 1 至 N 的一个排列。若答案有多种可能,则输出字典序最小的那一个。

管理员注:本题描述有误,这里字典序指的是 1,2,3,4,5,6,7,8,9,10,11,12

而不是 1,10,11,12,2,3,4,5,6,7,8,9

输入输出格式

输入格式:

两个正整数 n,sum 。

输出格式:

输出包括 1 行,为字典序最小的那个答案。

当无解的时候,请什么也不输出。

输入输出样例

输入样例#1:

4 16

输出样例#1:

3 1 2 4

思路:

一开始毫无思路,看了题解知道答案的系数和与杨辉三角有关,于是先用一个数组存储答案,再用dfs搜索。

最后两个测试点超时,仔细看了看代码,发现可以用剪枝,剪枝后成功AC。

注:关于答案系数与杨辉三角

源代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 21
#define MOD 123
#define E 1e-6
using namespace std;
int n,sum;
int triangle[N][N];
int a[N],vis[N];
bool flag;
void dfs(int step,int cnt)
{if(flag)return;if(cnt>sum)//当前和大于sum,剪枝return;if(step==n+1&&cnt==sum)//达到最后一层并找到答案{flag=true;for(int i=1;i<=n;i++)cout<<a[i]<<" ";}for(int i=1;i<=n;i++)if(!vis[i]){a[step]=i;vis[i]=1;cnt+=triangle[n][step]*a[step];//加上i*系数dfs(step+1,cnt);cnt-=triangle[n][step]*a[step];//减去i*系数vis[i]=0;}
}
int main()
{cin>>n>>sum;/*构造存储答案系数的杨辉三角*/triangle[1][1]=1;for(int i=2;i<=n;i++)for(int j=1;j<=i;j++)triangle[i][j]=triangle[i-1][j-1]+triangle[i-1][j];dfs(1,0);//从1开始搜索return 0;
}

数字三角形(洛谷-P1118)相关推荐

  1. 洛谷—— P1118 [USACO06FEB]数字三角形Backward Digit Su…

    https://www.luogu.org/problem/show?pid=1118#sub 题目描述 FJ and his cows enjoy playing a mental game. Th ...

  2. 洛谷 P1118 数字三角形游戏 Label:dfs

    题目描述 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直到只剩下一个数字位置.下面是一 ...

  3. 洛谷 P1118 [USACO06FEB]数字三角形Backward Digit Su…

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

  4. 洛谷P1118 [USACO06FEB]数字三角形Backward Digit Su…

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

  5. 洛谷P1118 [USACO06FEB]数字三角形 Backward Digit Su(dfs剪枝)

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 toN(1 \le N \l ...

  6. 幸运数字(洛谷-P3292)

    题目描述 A 国共有 n 座城市,这些城市由 n-1 条道路相连,使得任意两座城市可以互达,且路径唯一.每座城市都有一个幸运数字,以纪念碑的形式矗立在这座城市的正中心,作为城市的象征. 一些旅行者希望 ...

  7. 洛谷 P5725 求三角形

    题目链接: P5725 [深基4.习8]求三角形 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)https://www.luogu.com.cn/problem/P5725题目描述: ...

  8. 信息学奥赛一本通 1089:数字反转 | 1953:【11NOIP普及组】数字反转 | OpenJudge NOI 1.5 29 | 洛谷 P1307 [NOIP2011 普及组] 数字反转

    [题目链接] ybt 1089:数字反转 ybt 1953:[11NOIP普及组]数字反转 OpenJudge NOI 1.5 29:数字反转 洛谷 P1307 [NOIP2011 普及组] 数字反转 ...

  9. 洛谷 深基 第1部分 语言入门 第4章 循环结构程序设计(2022.02.14)

    P5718 [深基4.例2]找最小值 [深基4.例2]找最小值 - 洛谷 P5718 [深基4.例2]找最小值(python3实现)-2022.02.01 P5718 [深基4.例2]找最小值(pyt ...

最新文章

  1. 第十六届全国大学生智能车竞赛华南赛区竞赛事宜的通知
  2. Spinner的使用方法
  3. 【解题报告】Leecode 438. 找到字符串中所有字母异位词——Leecode每日一题系列
  4. Java每天学习一点点 09.10.13
  5. 软考网络工程师--知识产权与标准化
  6. nfs 跟rpcbind的关系
  7. xp 64位系统下载
  8. Echarts基本图表
  9. JavaSE 计算机基础知识 Java语言概述 JDK的下载,安装 HelloWorld案例 环境变量的配置 注释 关键字 标识符
  10. 管道与系统调用pipe
  11. absolute 与overflow:hidden常见bug
  12. win10免费sdk安装详细过程
  13. IntelliJ IDEA2017.3激活
  14. SOA RPC SOAP REST
  15. 爬虫获取网站美女图片
  16. QP状态机学习①——QP状态机架构
  17. 计算机学硕考数二的一本学校,软件工程专硕和学硕,专硕是考数二,学硕考数一吗...
  18. oracle trunc年,Oracle的Trunc和round的区别
  19. 【技术推文】生态系统碳库的遥感估算:森林碳储量、草地系统、城市扩张、区域能源消耗碳排放空间格局模拟、大气温室气体浓度的卫星监测、区域碳循环的遥感模拟与预测(遥感数据同化\模型驱动使用)
  20. Python 爬取马蜂窝

热门文章

  1. 什么是云原生,跟云计算有什么关系?终于有人讲明白了
  2. 20 行 Python 代码说清量子霸权!
  3. 从单体到Flink:一文读懂数据架构的演变
  4. php5.6 xdebug 配置,php5.5+apache2.4+mysql5.6+xdebug配置
  5. 万特电能表接线仿真系统 软件_【学习软件】嘘!此3款10万级专业解锁版神器全网已绝迹!(物理实验室)...
  6. 这样实现分布式锁,才叫优雅!
  7. 天才程序员的传奇人生:项目被总统抢走,在瞄准镜下写完代码后被捕入狱
  8. 关于HashMap容量的初始化,还有这么多学问。
  9. 微信开发实现一键拨号出现屏蔽问题的解决方案
  10. java虚拟机参数详解