题目描述

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。

一上来,直接打的大爆搜(70)。

 #include<cstdio>
int n,m;
int s[],bf[];
bool v[];
void find(int x){
if(x>n){
s[]=n;
while(s[]--){
for(int i=;i<=s[];i++) s[i]+=s[i+];
}
if(s[]==m){
for(int i=;i<=n;i++) printf("%d ",bf[i]);
n=;
}
else{
for(int i=;i<=n;i++) s[i]=bf[i];
}
return;
}
for(int i=;i<=n;i++) if(!v[i]){s[x]=bf[x]=i;v[i]=;find(x+);v[i]=;}
}
int main(){
scanf("%d%d",&n,&m);
find();
return ;
}

后来,看别人代码,找了一下每一位上对答案的影响。(80)

 #include<cstdio>
int n,m;
int s[],js[]={,};
bool v[];
void find(int x,int y){
if(x>n){
if(y==m){
for(int i=;i<=n;i++) printf("%d ",s[i]);
n=;
}
return;
}
for(int i=;i<=n;i++)
if(!v[i]){
s[x]=i;
v[i]=;
find(x+,y+js[x]*i);
v[i]=;
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
for(int j=i;j>;j--)
js[j+]+=js[j];
find(,);
return ;
}

最后又加了一个

if(y>m) return;

的剪枝。终于过了。

代码实现:

 #include<cstdio>
int n,m;
int s[],js[]={,};
bool v[];
void find(int x,int y){
if(y>m) return;
if(x>n){
if(y==m){
for(int i=;i<=n;i++) printf("%d ",s[i]);
n=;
}
return;
}
for(int i=;i<=n;i++)
if(!v[i]){
s[x]=i;
v[i]=;
find(x+,y+js[x]*i);
v[i]=;
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
for(int j=i;j>;j--)
js[j+]+=js[j];
find(,);
return ;
}

。。。我去年八月份就会的题,现在。。。

题目来源:洛谷

[USACO06FEB]数字三角形的更多相关文章

  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…

    题目描述 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…

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

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

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

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

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

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

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

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

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

  9. G:数字三角形

    总时间限制: 1000ms 内存限制: 65536kB描述73   88   1   02   7   4   44   5   2   6   5 (图1) 图1给出了一个数字三角形.从三角形的顶部 ...

随机推荐

  1. ECMA里面的操作符,

    ECMA里面的操作符,描述了一组操作于数据值的操作符,包括算数操作符.位操作符,关系操作符和相等操作符,ECMAscript操作符与之不同的是,他们能够使用于很多值,例如字符串.数字值.布尔值.甚至对 ...

  2. 【Linux】小米路由开启SSH访问权限

    一.验证小米路由ROM是否为开发版 1.  登录小米路由Web管理页面,检查ROM版本是否为开发版(若为开发版直接跳至第二步,若为稳定版继续本步骤). 2. 进入小米路由器官网(http://www1 ...

  3. EntityFramework:An error occurred while executing the command definition. See the inner exception for details.

    错误描述: 调用EF中的FirstOrDefault()时,报错误:An error occurred while executing the command definition. See the ...

  4. visual studio 2015安装

    问题:安装过程老是报:安装包丢失或者损坏,但是去虚拟光驱里面可以查找到该安装包. 解决:可能文件下载ISO过程中丢失了一些数据.使用“Hash(MD5校验工具)”检测文件的“SHA-1”值,然后与官网 ...

  5. Echarts修改legend样式

    legend: { icon: 'rect', itemWidth: 20, itemHeight: 10, itemGap: 10}

  6. Angular JS (2)

    通过Angular JS的官方教学文档,了解 routeProvider 的用法, angular.module('aaa').config(['$locationProvider','$routeP ...

  7. dede手机访问网站跳转到手机端模板

    如何手机访问的时候跳转到自己的手机端模板,这时候需要一个js跳转代码:当手机访问的时候直接跳转到手机端 那手机端前提要有手机端的模板 <script> if(navigator.platf ...

  8. MS-DOS Batch Script Template

    @echo off @setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION @rem Name: @rem Purpose: @rem @rem Autho ...

  9. parsley之验证属性设置

    parsley.js添加表单验证功能,直接在html元素中添加对应属性: Name API Description Required #2.0必填 required HTML5 data-parsle ...

  10. Masonry 原理与使用说明

    原理: 1)约束生成:MASConstraintMaker: 2)缺省补齐: - (void)setSecondViewAttribute:(id)secondViewAttribute { if ( ...