洛谷 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 44 3 67 916
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的一个排列。若答案有多种可能,则输出字典序最小的那一个。
管理员注:本题描述有误,这里字典序指的是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。
(标题有点长啊)
分析:最后的结果其实是一个杨辉三角。(拿样例来说,第4层的杨辉三角是1 3 3 1; 1∗*3+3∗*1+3∗*2+1∗*4=16),我们可以枚举全排列,然后带入杨辉三角系数中计算,这还是会超时。我们每一步都要判断一下,当当前值已经大于要求的值,就可以剪枝掉。
代码:
varb:array [1..12] of boolean;a:Array [1..12] of longint;f:array [0..12,0..12] of longint;n,m,i,j:longint;
procedure dfs(x:longint);var i,t,j:longint;
begin
if x=n+1 thenbegint:=0;for i:=1 to n dot:=t+f[n,i]*a[i];if t=m thenbeginfor i:=1 to n-1 dowrite(a[i],' ');write(a[n]);halt;endelse exit;end;for i:=1 to n doif b[i]=false thenbeginb[i]:=true;a[x]:=i;t:=0;for j:=1 to x dot:=t+f[n,j]*a[j];if t<=m then dfs(x+1);b[i]:=false;end;
end;beginreadln(n,m);f[1,1]:=1;for i:=2 to n dofor j:=1 to i dof[i,j]:=f[i-1,j]+f[i-1,j-1];dfs(1);
end.
洛谷 P1118 [USACO06FEB]数字三角形Backward Digit Su…相关推荐
- 洛谷—— 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…
题目描述 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(dfs剪枝)
题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 toN(1 \le N \l ...
- P1118 [USACO06FEB]数字三角形Backward Digit Su…
P1118 [USACO06FEB]数字三角形Backward Digit Su- 题目描述 FJ and his cows enjoy playing a mental game. They wri ...
- P1118 [USACO06FEB]数字三角形`Backward Digit Su`…
题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 toN(1 \le N \l ...
- P1118 [USACO06FEB]数字三角形`Backward Digit Su`… 回溯法
有这么一个游戏: 写出一个11至NN的排列a_iai,然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少11,直到只剩下一个数字位置.下面是一 ...
- P1118 [USACO06FEB]数字三角形`Backward Digit Su`…(C++_DFS)
题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 toN(1 \le N \l ...
- 杨辉三角 [USACO06FEB]数字三角形Backward Digit Su…
杨辉三角 简单的说一下就是两个未知数和的幂次方运算后的系数问题,比如(x+y)的平方=x的平方+2xy+y的平方,这样系数就是1,2,1这就是杨辉三角的其中一行,立方,四次方,运算的结果看看各项的系数 ...
- [USACO06FEB]数字三角形Backward Digit Su…
题目描述 3 1 2 44 3 67 916 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少 ...
最新文章
- PyTorch框架:(6)图像识别实战常用模块解读
- 动态多尺度图表达3D人体骨架运动,实现精准预测效果超SOTA
- 可控制转速CSS3旋转风车特效
- 【转载】Linux修改文件权限
- SQL SERVER触发器使用实例
- 转:从零开始开发一款Android App (from 简书)
- android报错及解决1--Bitmap加载时,报bitmap size exceeds VM budget
- IIFP之牛刀小试(初识篇)
- Java经纬度坐标转换到平面坐标
- 自学python要多久-自学Python需要多长时间?
- java 重写泛型方法_java – 泛型方法重写8
- 【IDE】IAR for ARM官网下载链接(超级全,什么版本都有——找不到来打我)
- hdu2028java-Lowest Common Multiple Plus
- SIM卡在手机中的主要作用
- 浙江大学机器学习(胡浩基)课程学习笔记一:概述与支持向量机
- TensorFlow Estimator 官方文档之----内置Estimator
- 基于BINN算法的CCPP全路径覆盖算法
- 今年流行的3步走,学习前端方法路径总结
- 海康威视工业相机SDK二次开发(VS+Opencv+QT+海康SDK+C++)(一)
- windows装android软件,电脑上运行安卓软件WindowsAndroid如何成功安装
热门文章
- 给桌面右键菜单添加新选项
- 《李兴华Java架构师 》完整版
- unity制作一款塔防游戏
- 【创新应用】5AloT(5G+AI+物联网)深度研究报告:下一轮科技红利
- 基于STM32的安卓蓝牙多功能遥控车
- window.showmodaldialog 兼容谷歌方案
- 情人节程序员用HTML网页表白【春娇-志明结婚邀请函】 HTML5七夕情人节表白网页源码 HTML+CSS+JavaScript
- 摄像机传输到计算机,如何将摄像机文件导入计算机 - Windows 10
- 实验9 人口预测与数据拟合(最小二乘法)
- 串口升级华为S2300系列交换机