## 1653: [Usaco2006 Feb]Backward Digit Sums

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 285  Solved: 215
[Submit][Status]

## Description

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.

## Input

* Line 1: Two space-separated integers: N and the final sum.

## Output

* Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.

4 16

## Sample Output

3 1 2 4

OUTPUT DETAILS:

There are other possible sequences, such as 3 2 1 4, but 3 1 2 4
is the lexicographically smallest.

## Source

Silver

``` var
i,j,k,l,m,n:longint;
a:array[..] of longint;
b:array[..,..] of longint;
procedure swap(var x,Y:longint);
var z:longint;
begin
z:=x;x:=y;y:=z;
end;
procedure sort(l,r:longint);
var i,j,x,y:longint;
begin
i:=l;j:=r;x:=a[(l+r) div ];
repeat
while a[i]<x do inc(i);
while a[j]>x do dec(j);
if i<=j then
begin
swap(a[i],a[j]);
inc(i);dec(j);
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
begin
for i:= to n do a[i]:=i;
fillchar(b,sizeof(b),);
b[,]:=;
for i:= to n do
for j:= to i do b[i,j]:=b[i-,j-]+b[i-,j];
while a[]= do //萌萌哒生成法全排列，小学时学的现在居然还记得*_*
begin
l:=;
for i:= to n do l:=l+a[i]*b[n,i];
if l=m then
begin
for i:= to n- do
write(a[i],' ');
writeln(a[n]);
halt;
end;
j:=n;
while a[j-]>a[j] do dec(j);
k:=n;
while a[j-]>a[k] do dec(k);
swap(a[j-],a[k]);
sort(j,n);
end;
end.
```

## 1653: [Usaco2006 Feb]Backward Digit Sums的更多相关文章

1. 【BZOJ】1653: [Usaco2006 Feb]Backward Digit Sums（暴力）

http://www.lydsy.com/JudgeOnline/problem.php?id=1653 看了题解才会的..T_T 我们直接枚举每一种情况(这里用next_permutation,全排 ...

2. BZOJ 1653 [Usaco2006 Feb]Backward Digit Sums ——搜索

[题目分析] 劳逸结合好了. 杨辉三角+暴搜. [代码] #include <cstdio> #include <cstring> #include <cmath> ...

3. bzoj 1653: [Usaco2006 Feb]Backward Digit Sums【dfs】

每个ai在最后sum中的值是本身值乘上组合数,按这个dfs一下即可 #include<iostream> #include<cstdio> using namespace st ...

4. BZOJ1653: [Usaco2006 Feb]Backward Digit Sums

1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 207  Solved:  ...

5. Backward Digit Sums（POJ　3187）

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5495   Accepted: 31 ...

6. Backward Digit Sums(暴力)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5664   Accepted: 32 ...

7. POJ3187 Backward Digit Sums 【暴搜】

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4487   Accepted: 25 ...

8. POJ 3187 Backward Digit Sums 枚举水~

POJ 3187  Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3   1   2   4  他可以相邻 ...

9. 【POJ - 3187】Backward Digit Sums（搜索）

-->Backward Digit Sums 直接写中文了 Descriptions: FJ 和 他的奶牛们在玩一个心理游戏.他们以某种方式写下1至N的数字(1<=N<=10). 然 ...

## 随机推荐

1. ubuntu一些基本软件安装方法

ubuntu一些基本软件安装方法 首先说明一下 ubuntu 的软件安装大概有几种方式:1. deb 包的安装方式deb 是 debian 系 Linux 的包管理方式, ubuntu 是属于 deb ...

2. 理解RESTful架构（转）

越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(high latency).高 ...

3. linux搭建phantomjs+webdriver+testng+ant自动化工程

因为项目的原因,需要将脚本在linux环境无浏览器化去跑,那么原有的在windows系统下有浏览器化的自动化脚本场景就不适用了,这里给出linux系统下搭建phantomjs+webdriver+te ...

4. 北大poj-1021

2D-Nim Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4066   Accepted: 1851 Descriptio ...

5. Discuz!X 3.4 前台任意文件删除漏洞复现

Discuz!X 3.4 前台任意文件删除漏洞复现 参考链接: http://www.freebuf.com/vuls/149904.html http://www.freebuf.com/artic ...

6. rsync+inotifywait

0.rsync+inotify是实现文件实时同步的,加什么参数才能实现实时同步,--delete参数又是什么意思? 1.运行模式(服务器) rsync有两种常用的认证方式,一种是rsync-daemo ...

7. Vue 项目集合

饿了么安全应急响应中心 饿了么招聘 饿了么前端 · GitHub 稀土掘金 异乡好居 明星垂搜 广州建管 基于Vue.js的数据统计系统(一) 基于Vue.js的数据统计系统(二) 基于Vue.js的 ...

8. linux下如何查看当前机器提供了哪些服务

答:使用netstat工具 在命令行下输入netstat -atun即可列出当前机器提供的服务 netstat各选项解析: -a 列出所有服务 -t 列出tcp相关 -u 列出udp相关 -n 以数字 ...