[USACO06FEB]数字三角形
题目描述
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行,为字典序最小的那个答案。
当无解的时候,请什么也不输出。(好奇葩啊)
输入输出样例
4 16
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]数字三角形的更多相关文章
- P1118 [USACO06FEB]数字三角形`Backward Digit Su`… 回溯法
有这么一个游戏: 写出一个11至NN的排列a_iai,然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少11,直到只剩下一个数字位置.下面是一 ...
- 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 \ ...
- 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 ...
- 洛谷—— P1118 [USACO06FEB]数字三角形Backward Digit Su…
https://www.luogu.org/problem/show?pid=1118#sub 题目描述 FJ and his cows enjoy playing a mental game. Th ...
- luoguP1118 [USACO06FEB]数字三角形`Backward Digit Su`… 题解
一上午都在做有关搜索的题目,,, 看到这题之后就直接开始爆搜 结果只有70分, 其余的点硬生生的就是那么WA了. 我的天哪~ 70分代码: #include<iostream> #incl ...
- P1118 [USACO06FEB]数字三角形`Backward Digit Su`… (dfs)
https://www.luogu.org/problemnew/show/P1118 看的出来是个dfs 本来打算直接从下到上一顿搜索 但是不会 看了题解才知道系数是个杨辉三角....... 这样就 ...
- 洛谷P1118 [USACO06FEB]数字三角形`Backward Digit Su`…
#include<iostream> using namespace std ; ; int y[N][N]; int n; int a[N]; bool st[N]; int sum; ...
- Luogu P1118 [USACO06FEB]数字三角形 Backward Digit Sums | 搜索、数学
题目链接 思路:设一开始的n个数为a1.a2.a3...an,一步一步合并就可以用a1..an表示出最后剩下来的数,不难发现其中a1..an的系数恰好就是第n层杨辉三角中的数.所以我们可以先处理出第n ...
- G:数字三角形
总时间限制: 1000ms 内存限制: 65536kB描述73 88 1 02 7 4 44 5 2 6 5 (图1) 图1给出了一个数字三角形.从三角形的顶部 ...
随机推荐
- centos docker 安装mysql 8.0
centos 版本 CentOS Linux release 7.5.1804 (Core) 内核版本: 3.10.0-862.el7.x86_64 下载最新版mysql docker pull m ...
- knockout jquery警告删除
//触发删除的动作 $("a.delete").live('click', function () { var ...
- 源码阅读之LinkedList(JDK8)
inkedList概述 LinkedList是List和Deque接口的双向链表的实现.实现了所有可选列表操作,并允许包括null值. LinkedList既然是通过双向链表去实现的,那么它可以被当作 ...
- c++编程中处理char和wchar_t的好工具
/* ttype.h sdragonx 2015-02-18 18:32:43 这个几个模版函数是为了处理ansi或unicode,使字符串值或者字符串函数能够在模版中使用 2018/7/26 23: ...
- 02—IOC实现项目中的解耦
- Linux用户、用户组权限管理详解
Linux用户管理三个重要文件详解: Linux登陆需要用户名.密码./etc/passwd 文件保存用户名.登录Linux时,Linux 先查找 /etc/passwd 文件中是否有这个用户名,没有 ...
- NHibernate学习笔记(3)-实体反射到数据库
一.开发环境 NHiberate版本:4.0.4 开发工具:VS2013 数据库:SQLServer2012 二.开发流程 1.编写领域类与映射文件 namespace Domain { public ...
- struts2之actionSupport学习
actionSupport在手工完成字段验证,显示错误消息,国际化等情况下推荐使用.
- java设计模式之代理模式模式总结
定义:代理模式这种设计模式是一种使用代理对象来执行目标对象的方法并在代理对象中增强目标对象方法的一种设计模式. 解读定义: 1.代理对象和目标对象有共同的接口: 2.使用代理对象执行目标对象中的方法: ...
- HDU_1203_01背包
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...