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 ...
随机推荐
- 2 TypeScript--Hello World
安装好TypeScript后,我们来完成第一个页面--Hello World 新建index.html文件: <!DOCTYPE html> <html> <head&g ...
- IDEA debug调式快捷键
F9 resume programe 恢复程序 Alt+F10 show execution point 显示执行断点 F8 Step Over 相当于eclipse的f6 跳到下一步 F7 Step ...
- Django框架之路由
1,路由系统就是url系统,整个url系统就是请求进入Django项目的入口,每一个请求的种类由url分析完毕并再去返回相应的响应,它的本质是url与要为该url调用的视图函数之间的映射关系表(当项目 ...
- Hackrank Equal DP
Christy is interning at HackerRank. One day she has to distribute some chocolates to her colleagues. ...
- HDU1166 敌兵布阵 —— 线段树单点修改
题目链接:https://vjudge.net/problem/HDU-1166 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布 ...
- webstorm使用帮助(转自http://my.oschina.net/longteng2013/blog/138010),另外有部分内容摘自其它人博客
为了更高效的开发代码,这里列出了一些webstorm的快捷键和zencoding 发表于1 年 前(2013-06-17 00:19) 阅读(2101) | 评论(2) 11人收藏此文章, 我要收 ...
- eclipse中jsp页面乱码问题
若上述位置均改为utf-8之后,页面展示扔为乱码,检查jsp页面是否有编码说明
- leetcode 783. Minimum Distance Between BST Nodes 以及同样的题目 530. Minimum Absolute Difference in BST
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- css3 vw -----解决页面滚动出现跳动的bug
100vw相对于浏览器的window.innerWidth,是浏览器的内部宽度,注意,滚动条宽度也计算在内!而100%是可用宽度,是不含滚动条的宽度. demo: h1{font-size:8vw;} ...
- adb devices 不识别显示为空(转载)
转自: http://yinger-fei.iteye.com/blog/1530118 在 android 设备的 Linux 内核中把 USB 驱动的 PID VID 修改以后,也许之前的 adb ...