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. EasyUI datagrid 删除的时候无法正确找到重复记录row index的问题

    EasyUI datagrid 删除的时候无法正确找到重复记录row index的问题: 如果无法正确找到重复记录的row index,就需要进行注意添加的时候所进行的 操作:如果添加的时候就是添加的 ...

  2. Canvas贝塞尔三级曲线

    两个控制点 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  3. CodeForces 456A

    Laptops Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  4. 常用的JQ函数

    /// <reference path="jquery-1.8.0.min.js"> /* * DIV或元素居中 * @return */ jQuery.fn.mCen ...

  5. C# 6 与 .NET Core 1.0 高级编程 - C# 6 改进

    个人原创译文,转载请注明出处.有不对的地方欢迎指出与交流. 英文原文:Professional C# 6 and .NET Core 1.0 - What's New in C# 6 C# 6 改进 ...

  6. 深圳尚学堂:JavaScript中常见的字符串操作

    快到春节放假了,春节后又是一大波的找工作热潮,在前端实招聘笔试时,必不可免额会考到关于JavaScript中字符串的处理问题.考的不是你会不会,而是你能不能在不借用XX手册或者XX指南再或者百度谷歌的 ...

  7. date()中的getYear的小问题

    function DateDemo(){ var d,s = "今天得日期是:"; d = new Date(); s += d.getMonth()++"/" ...

  8. oracle_权限

    Oracle 权限 权限允许用户访问属于其它用户的对象或执行程序,ORACLE系统提供三种权限:Object 对象级.System 系统级.Role 角色级.这些权限可以授予给用户.特殊用户publi ...

  9. Git中.gitignore文件的使用

      在我们使用git的时候,有时候就不想传一些与代码无关的文件到远程仓库中,比如说编译后的文件,.gitignore就可以帮助我们处理这些文件. 生成.gitignore文件 在git bash中使用 ...

  10. python报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 0 解决方案

    环境:mac+python 2.7 场景描述:在使用python修改excel内容修改表格内容为中文保存时报以下错误 此时已经设置了utf-8了 但保存时仍然报错错 此时将python中的中文使用un ...