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.

Sample Input

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.

HINT

 

Source

Silver

题解:这个嘛,本来还想什么高端洋气的算法的,可是再想想果断决定——弃疗——10!不过才3628800而已嘛,(具体说算法嘛,很显然对于原数列,每个数依次的最终累计次数即是杨辉三角形第N行的对应数字,别的没了),可是再一想,有点不对——你不是每次还要判断此组解是否合法么?这样子复杂度可还要再×10哦(36288000,3kW多了,这下子可危险啊,虽然事实上只要有解的话,由于杨辉三角形的对称性,那么最多理论上一半的时间即可找到解)。。。可是结果是——228kb 60ms Accept我也是醉了。。。

 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
readln(n,m);
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. jq操作radio,设置选中、获取选中值

    <label><inputtype="radio"name="sex"value="1">男</label&g ...

  2. Ninja介绍

    什么是Ninja 在Unix/Linux下通常使用Makefile来控制代码的编译,但是Makefile对于比较大的项目有时候会比较慢,看看上面那副漫画,代码在编译都变成了程序员放松的借口了.所以这个 ...

  3. 迈向新纪元编辑器Atom

    第一次听说Atom是半年前天猫的学姐说她在用这款这款编辑器,期间我从dw到st再到webstorm都分别做了项目~,但是作为一名拥抱开源的FE,怎么能拒绝github社区维护的项目呢?接着就让我们感受 ...

  4. 分享一些免费的MD5解密网站

    最近下载了几个mdb文件,里面几万条md5和几千条sha512(居然还有站长用512直接放在库中的,尼玛多占空间啊),我用C#写了个工具暴 力了一小部分,大概不到3%吧, 花了我两天,电脑卡得要死,效 ...

  5. 理解Window和WindowManager

    Window表示一个窗口的概念,Window是一个抽象类,它的具体实现是PhoneWindow.创建一个Window,需要通过WindowManager即可完成,WindowManager是外界访问W ...

  6. WebForm 三级联动

    三级联动 数据库根据父级代号条件写查询 返回list<>集合 方法一: 创建三个下拉列表: ※AutoPostBack:否发生自动回传到服务器的操作.如果把该属性设置为 TRUE,则启用自 ...

  7. 详谈Struts2

    介绍struts2: struts2是一个基于mvc设计模式的web层框架. 详谈struts2的执行流程: struts2的执行流程:用户发送请求---->首先经过Struts2的核心过滤器- ...

  8. java_监控工具jvisualvm

    转:使用 VisualVM 进行性能分析及调优 启动:jvisualvm 首先到JDK安装目录/bin目录下,双击jvisualvm.exe文件启动 需要注意的是:当OS所在分区是FAT格式时,Vis ...

  9. Syslog4j如何实现接收日志

    1.Syslog4j是一个实现Syslog(RFC3164)协议的Java开源类库包括客户端与服务器端.通过 UDP/IP, TCP/IP, TCP/IPover SSL/TLS, Unix Sysl ...

  10. OSS.Common获取枚举字典列表标准库支持

    上篇(.Net Standard扩展支持实例分享)介绍了OSS.Common的标准库支持扩展,也列举了可能遇到问题的解决方案.由于时间有限,同时.net standard暂时还没有提供对Descrip ...