题目描述

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. 讯搜问题排查xunsearch

    mysql导入数据不成功,开始重建索引后提示 [XSException] ../local/xunsearch/sdk/php/lib/XS.php(1898): DB- 可打印的版本 开始重建索引 ...

  2. linux学习之路7 linux下获取帮助

    help 帮助 ls -h或者ls - -help man 最常用的帮助命令 man (+数字 )+命令 (数字代表文档帮助类型) man -k 关键字 可以用来查询包含该关键字的文档 info 与m ...

  3. ACM_Scramble Sort

    Scramble Sort Time Limit: 2000/1000ms (Java/Others) Problem Description: In this problem you will be ...

  4. sql 所有数据表中 插入字段

    declare @tablename varchar(200)declare @sql varchar(2000)declare cur_t cursor forselect name from sy ...

  5. C#常见问题总结(三)

    11.sql比access好在哪里,为什么都用sql 解决方法: 数据量大,可以在服务器端,access一般在单机的时候用 12.c#基础视频教程有吗 解决方法: 零基础学C#这本书带全套C#基础视频 ...

  6. 开发一款APP需要多少钱

    移动互联网近几年发展尤为迅速,越来越多的企业也开始将目光聚集到了移动互联网,这意味着移动互联网时代到来,而移动APP应用是竞争的一个因素.在移动互联网时代,移动APP开发已经不再是什么新鲜事了,许多的 ...

  7. Ionic2/angularJs2中的静态类 PhotoLibrary 调用不上

    photoLibrary调用报错:No provider for PhotoLibrary: 在调用相册文件时有用到photolibrary,总有些莫名的报错,3月份的时候这个坑让我不知所措,现在写下 ...

  8. mybatis 关联查询

    1.关联的两个实体类 外部类 parent public class Parent{ private String parentId; private String parentName; priva ...

  9. 【PostgreSQL-9.6.3】物化视图

    PostgreSQL 9.3 以后出现了物化视图.物化视图是由实实在在的数据组成,这是和一般视图的根本区别. 1. 物化视图创建语法如下: --创建语法 CREATE MATERIALIZED VIE ...

  10. DIV水平 垂直居中CSS

    /*实现一.原理:要让div等块级元素水平和垂直居中,必需知道该div等块级元素的宽度和高度,然后设置位置为绝对位置,距离页面窗口左边框和上边框的距离设置为50%,这个50%就是指页面窗口的宽度和高度 ...