https://www.luogu.org/problem/show?pid=1118#sub

题目描述

FJ and his cows enjoy playing a mental game. They write down the numbers from 1 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   4
4 3 6
7 9
16

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的一个排列。若答案有多种可能,则输出字典序最小的那一个。

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

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

输入输出格式

输入格式:

两个正整数n,sum。

输出格式:

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

当无解的时候,请什么也不输出。(好奇葩啊)

输入输出样例

输入样例#1:

4 16
输出样例#1:

3 1 2 4

说明

对于40%的数据,n≤7;

对于80%的数据,n≤10;

对于100%的数据,n≤12,sum≤12345。

 #include <algorithm>
#include <cstdio> using namespace std; int n,sum,ans[];
int yh[][],use[]; void DFS(int now,int tot)
{
if(now>n)
{
if(tot==sum)
{
for(int i=;i<=n;i++)
printf("%d ",ans[i]);
exit();
}
return ;
}
for(int i=;i<=n;i++)
{
if(use[i]) continue;
if(tot+i*yh[n][now]>sum) continue;
ans[now]=i;
use[i]=;
DFS(now+,tot+i*yh[n][now]);
use[i]=;
}
} int main()
{
scanf("%d%d",&n,&sum);
yh[][]=;
for(int i=;i<=n;i++)
for(int j=;j<=i;j++)
yh[i][j]=yh[i-][j-]+yh[i-][j];
DFS(,);
return ;
}

洛谷—— P1118 [USACO06FEB]数字三角形Backward Digit Su…的更多相关文章

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

    #include<iostream> using namespace std ; ; int y[N][N]; int n; int a[N]; bool st[N]; int sum; ...

  2. P1118 [USACO06FEB]数字三角形`Backward Digit Su`… 回溯法

    有这么一个游戏: 写出一个11至NN的排列a_iai​,然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少11,直到只剩下一个数字位置.下面是一 ...

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

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 to N(1 \le 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)

    https://www.luogu.org/problemnew/show/P1118 看的出来是个dfs 本来打算直接从下到上一顿搜索 但是不会 看了题解才知道系数是个杨辉三角....... 这样就 ...

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

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

  7. luoguP1118 [USACO06FEB]数字三角形`Backward Digit Su`… 题解

    一上午都在做有关搜索的题目,,, 看到这题之后就直接开始爆搜 结果只有70分, 其余的点硬生生的就是那么WA了. 我的天哪~ 70分代码: #include<iostream> #incl ...

  8. 【洛谷P1118】数字三角形

    数字三角形 题目链接 4 16 3 1 2 4 3 1 2 4 (3+1) (1+2) (2+4)(3+1+1+2) (1+2+2+4) (3+1+1+1+2+2+2+4)16=1*3+3*1+3*2 ...

  9. 洛谷 [P1118] IOI1994 数字三角形

    简单dfs 我们注意到,题目中的运算方式与杨辉三角极其相似,所以说本题实际上是一道加权的杨辉三角,搜索系数 #include <iostream> #include <cstdio& ...

随机推荐

  1. WPF和WinForm的区别, 数据驱动与事件驱动的优势对比

    Winform中针对界面的元素进行操作, 所有业务都关联在当前窗口的后台, 而在此之前, 无奈你是双击事件的添加方式.还是后台绑定事件的方式, 你都需要给每个元素一个固定规范的名称, 然后进行相关的数 ...

  2. LeetCode102 Binary Tree Level Order Traversal Java

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

  3. button按钮怎么实现超链接

    button按钮怎么实现超链接 一.总结 1.我的按钮实现超链接是通过button内嵌a标签来实现的 <button class="am-btn am-btn-default am-b ...

  4. Impala通过JDBC方式访问

    不多说,直接上干货! • 配置: – impala.driver=org.apache.hive.jdbc.HiveDriver – impala.url=jdbc:hive2://node2:210 ...

  5. Kinect 开发 —— 面部识别

    EmguCV库也能用来进行面部识别(face identify).实际的面部识别,就是将一张图像上的人物的脸部识别出来,这是个很复杂的过程,具体过程我们这里不讨论.对一幅影像进行处理来找到包含脸部的那 ...

  6. Kinect 开发 —— 姿势识别

    姿势和手势通常会混淆,但是他们是两个不同的概念.当一个人摆一个姿势时,他会保持身体的位置和样子一段时间.但是手势包含有动作,例如用户通过手势在触摸屏上,放大图片等操作. 通常,游戏者很容易模仿指定姿势 ...

  7. Oracle 练习

    --简单的select语句select deptno,dname,loc from DEPT where deptno='40';--描述表结构 部门表desc dept;--雇员表desc emp; ...

  8. 使用pandas导出PostgreSQL 模式下的所有表数据并保存

    PostgreSQL PostgreSQL 是一个非常强大的数据库,它是一个免费的对象-关系数据库服务器(数据库管理系统).PostgreSQL支持大部分 SQL 标准, 在语句上也有很大的相似的地方 ...

  9. FZU 1962 新击鼓传花游戏

    新击鼓传花游戏 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on FZU. Original ID: 19 ...

  10. C# WPF开源控件库MaterialDesign介绍

    介绍 1.由于前端时间萌发开发一个基础架构得WPF框架得想法, 然后考虑到一些界面层元素统一, 然后就无意间在GitHub上发现一个开源WPF UI, 于是下载下来了感觉不错. 官网地址:http:/ ...