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 ...
随机推荐
- PHP出现Warning: A non-numeric value encountered问题的原因及解决方法
本文介绍php出现Warning: A non-numeric value encountered问题,用实例分析出现这种错误的原因,并提供避免及解决问题的方法. <?php error_rep ...
- Android碎纸机效果
1.总体思想 活用padding和margin 2.实现过程 public class PopupShredderView extends FrameLayout{ public PopupShred ...
- 网易新闻client(高仿)
近期整理了下自己曾经做过的项目,决定分享出来.本篇所展示的是仿网易新闻client,服务端是在新浪SAE部署着的.所以大家下载后,可直接在手机上看到效果.接下来看效果图: watermark/2/te ...
- 【iOS系列】-xib封装使用
[iOS系列]-xib封装使用 Xib文件可以用来描述某一块局部的UI界面 Xib文件的加载 修改xib文件的大小size(Freeform) 第一: NSArray *objs = [[NSBund ...
- Windows平台cocos2d-x 3.0 android开发环境
cocos2d-x升级到3.0后变化不小,除了API的变化(主要是函数和类名称变化,以及使用了C++11的不少特性.function/bind, lamda, std::thread-),创建和编译p ...
- NDK编程中如何在C文件中打印调试信息
1,在Android.mk文件中加上 LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog LOCAL_PATH := $(call my-dir)include ...
- Linux __setup解析【转】
本文转载自:http://blog.csdn.net/fdaopeng/article/details/7895037 __setup这条宏在Linux Kernel中使用最多的地方就是定义处理Ker ...
- UVA11722概率问题之线性规划
链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...
- hdu 5534
题目描述:n个节点度数之和为n-2,每个节点预分配了1个度,任意分配度数是否有可能形成树? 从1到n节点,考虑树的形状,考虑分配给当前节点i的度数,并且注意到当前节点的度数不会影响其他节点(之前或者之 ...
- bzoj4276
线段树优化建图+费用流 朴素的做法是每个强盗直接对每个区间的每个点连边,然后跑最大权匹配,这样有5000*5000条边,肯定过不去,那么我们用线段树优化一下,因为线段树能把一个O(n)的区间划分为O( ...