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 <= 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。
杨辉三角+暴力!
#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…的更多相关文章
- 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…
https://www.luogu.org/problem/show?pid=1118#sub 题目描述 FJ and his cows enjoy playing a mental game. Th ...
- 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; ...
- luoguP1118 [USACO06FEB]数字三角形`Backward Digit Su`… 题解
一上午都在做有关搜索的题目,,, 看到这题之后就直接开始爆搜 结果只有70分, 其余的点硬生生的就是那么WA了. 我的天哪~ 70分代码: #include<iostream> #incl ...
- Luogu P1118 [USACO06FEB]数字三角形 Backward Digit Sums | 搜索、数学
题目链接 思路:设一开始的n个数为a1.a2.a3...an,一步一步合并就可以用a1..an表示出最后剩下来的数,不难发现其中a1..an的系数恰好就是第n层杨辉三角中的数.所以我们可以先处理出第n ...
- 【洛谷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 ...
- [USACO06FEB]数字三角形
题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N ...
随机推荐
- 3 TypeScript 语法特性
一.类型注解(Type annotations) TypeScript 通过类型注解提供静态类型以在编译时启动类型检查,简单来说,就是指定数据类型,它会在代码运行的时候,对传入的数据进行数据类型匹配检 ...
- 3.NetDh框架之缓存操作类和二次开发模式简单设计(附源码和示例代码)
前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...
- @Html.ValidationMessageFor客户端验证
<%=Html.LabelFor(model => model.sUser) %><%=Html.TextBoxFor(model => model.sUser) %&g ...
- 2016/3/1 淘宝 腾讯 网易 css初始化代码 以及最基础的初始化
淘宝官网(http://www.taobao.com/)样式初始化 body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, o ...
- 3.myeclipse 8.5 m1 注册码
为了能在eclipse 中方便的使用uml,尝试了多次安装各种uml插件未果,myeclipse 自带uml插件,但是要注册啊,要破解啊!!! user:baiduzhidaopassword:oLR ...
- linux怎么区别文本文件和二进制文件
linux的文本文件与二进制文件的区分与windows的区分是相同的!说到底计算机存储的文件都是以二进制形式存储的,但是区别是,习惯上认为: (1).文本文件 文本文件是包含用户可读信息的文件.这些文 ...
- jquery中的工具函数 Utilities
noConflict(deep) 释放$和Jquery的控制权 isFunction(obj) isArray(obj) isWindow(obj) isNumeric(obj) type(obj) ...
- USACO26 moofest 奶牛集会(归并排序)
听说这题也是bzoj的3378&&poj1990,然而没有权限号交不了..poj懒得登. 题意:有n个奶牛,他们相互发出max(a[i].v,a[j].v)*abs(a[i].p-a[ ...
- ML.NET
ML.NET http://www.cnblogs.com/BeanHsiang/category/1218714.html 随笔分类 - 使用ML.NET实现NBA得分预测 摘要: 本文将介绍一种特 ...
- appium学习【三】:截图时,图片命令中包含当前的函数名,以区分错误是在哪个函数报的
import sys funcName = sys._getframe().f_back.f_code.co_name #获取调用函数名 print sys._getframe().f_code.co ...