题目描述

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<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,num;
const int PT[][] = //先打一张杨辉三角表
{
{ } , // N = 1
{ , } , // N = 2
{ , , } , // N = 3
{ , , , } , // N = 4
{ , , , , } , // N = 5
{ , , , , , } , // N = 6
{ , , , , , , } , // N = 7
{ , , , , , , , } , // N = 8
{ , , , , , , , , } , // N = 9
{ , , , ,,, , , , } , // N = 10
{ , , ,,,,,, , , } , // N = 11
{ , , ,,,,,,, , , } }; // N = 12
int vis[];
int ans[];
int flag;
int dfs(int p,int k,int now)//第p个数,值为k,和是now
{ if(now>num)return ;
if(p==n)
{
if(now==num)
{
ans[p]=k;
return ;
}
else
return ;
}
vis[k]=;
for(int i=;i<=n;i++)
{
if(vis[i]==&&dfs(p+,i,now+PT[n-][p]*i))
{
ans[p]=k;
return ;
}
}
vis[k]=;
return ;
}
int main()
{ scanf("%d%d",&n,&num);
if(dfs(,-,))
for(int i=;i<=n;i++)
printf("%d ",ans[i]);
return ;
}

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

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

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

  2. 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 \ ...

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

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

  4. P1118 [USACO06FEB]数字三角形`Backward Digit Su`… (dfs)

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

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

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

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

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

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

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

  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. [USACO06FEB]数字三角形

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

随机推荐

  1. 点滴记录——Ubuntu 14.04中Solr与Tomcat整合安装

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/38965981 1. 安装jdk,tomcat     sudo apt-get instal ...

  2. c++代码赏析之类对象传參

    #include <iostream> using namespace std; class A { private: int x; public: A():x(0) { x = 0; c ...

  3. java移位运算符实验程序:&lt;&lt;(左移)、&gt;&gt;(带符 号右移)和&gt;&gt;&gt;(无符号右移)

    public class txs { static void leftbit(){ int i;               //整型长度为32位                           ...

  4. Android开发文档翻译之-Services

    Service是一种能长期在后台运行同一时候不须要与用户进行交互的应用组件.其它组件能够开启service,开启后service能够自行运行及时用户已经切换到其它的应用.此外,组件能够与service ...

  5. 数据库建表参考(SQL Server)

      (1).字段设置为Not Null+Default Value.原因:减少三值判断,可为Null的字段要多判断null:另外,定长字段为null也占空间,变长字段为空字符串也是不占空间,所以设置成 ...

  6. JMeter Web测试快速入门教程

    学习前的准备 学习本教程前,你的电脑上至少应该有Apache JMeter这款软件.如果你没有,点击此处下载. 当你点进去后,你会发现它是一个依赖Java的软件 因此如果你电脑上没有Java环境,你应 ...

  7. win系统下启动linux上的kafka集群及使用

    一.首先在win系统下C:\Windows\System32\drivers\etc文件夹中hosts文件加入例如以下内容: 10.61.6.167 slaves1 10.61.6.168 slave ...

  8. Okapi BM25 (BM stands for Best Matching)

    Okapi BM25 - Wikipedia https://en.wikipedia.org/wiki/Okapi_BM25 In information retrieval, Okapi BM25 ...

  9. QmlWinExtras

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/zhengtianzuo06/article/details/78404961QtWinExtras是 ...

  10. PP-判断生产订单状态(关闭)

    方法一.工单号通过 resb找到对象号 然后找到状态为I0045利用表JEST与TJ02T . 方法二.函数'STATU_CHECK' 检查工单状态为'I0045' 则为已做技术性关闭. READ T ...