1653: [Usaco2006 Feb]Backward Digit Sums
1653: [Usaco2006 Feb]Backward Digit Sums
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 285 Solved: 215
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
题解:这个嘛,本来还想什么高端洋气的算法的,可是再想想果断决定——弃疗——10!不过才3628800而已嘛,(具体说算法嘛,很显然对于原数列,每个数依次的最终累计次数即是杨辉三角形第N行的对应数字,别的没了),可是再一想,有点不对——你不是每次还要判断此组解是否合法么?这样子复杂度可还要再×10哦(36288000,3kW多了,这下子可危险啊,虽然事实上只要有解的话,由于杨辉三角形的对称性,那么最多理论上一半的时间即可找到解)。。。可是结果是——228kb 60ms Accept我也是醉了。。。
1 var
2 i,j,k,l,m,n:longint;
3 a:array[0..20] of longint;
4 b:array[0..20,0..20] of longint;
5 procedure swap(var x,Y:longint);
6 var z:longint;
7 begin
8 z:=x;x:=y;y:=z;
9 end;
10 procedure sort(l,r:longint);
11 var i,j,x,y:longint;
12 begin
13 i:=l;j:=r;x:=a[(l+r) div 2];
14 repeat
15 while a[i]<x do inc(i);
16 while a[j]>x do dec(j);
17 if i<=j then
18 begin
19 swap(a[i],a[j]);
20 inc(i);dec(j);
21 end;
22 until i>j;
23 if l<j then sort(l,j);
24 if i<r then sort(i,r);
25 end;
26 begin
27 readln(n,m);
28 for i:=0 to n do a[i]:=i;
29 fillchar(b,sizeof(b),0);
30 b[1,1]:=1;
31 for i:=2 to n do
32 for j:=1 to i do b[i,j]:=b[i-1,j-1]+b[i-1,j];
33 while a[0]=0 do //萌萌哒生成法全排列,小学时学的现在居然还记得*_*
34 begin
35 l:=0;
36 for i:=1 to n do l:=l+a[i]*b[n,i];
37 if l=m then
38 begin
39 for i:=1 to n-1 do
40 write(a[i],' ');
41 writeln(a[n]);
42 halt;
43 end;
44 j:=n;
45 while a[j-1]>a[j] do dec(j);
46 k:=n;
47 while a[j-1]>a[k] do dec(k);
48 swap(a[j-1],a[k]);
49 sort(j,n);
50 end;
51 end.
52
- 架设ftp反向代理服务器
- CentOS 7 上面安装PowerShell
- 苹果刻意降低旧 iPhone 效能的做法,或在法国面临刑事检控
- maven常用插件: 打包源码 / 跳过测试 / 单独打包依赖项
- 通过SmtpClient发送Exchange会议邮件
- Microsoft training Kits
- C#与yaml解析
- WordPress中的jQuery库不起作用的相关问题
- 人工智能时代家长不必做“虎妈”
- WCF 消息帧格式
- C#以post方式调用struts rest-plugin service的问题
- Web前端开发初级阶段需要学习的知识有哪些?
- 作为TensorFlow的底层语言,你会用C+构建深度神经网络吗?
- 企业应用中使用Silverlight 3
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- adb shell读取设置手机ocd值以及ocd介绍
- sqli-labs练习(第三、四关)
- 使用 Python 操作 word文档
- sqli-labs练习(第五、六关)
- 分享一个Qt写的SMTP邮件客户端(库)
- mysql学习笔记
- MapReduce的常见输入格式之NlineInputFormat
- MapReduce的常见输入格式之KeyValueTextInputFormat
- MapReduce之 FileInputFormat的切片策略(默认)
- MapReduce之片和块的关系
- MapReduce的常见输入格式之CombineTextInputFormat
- MapReduce之Job提交流程
- MapReduce之自定义InputFormat
- MapReduce之MapTask工作机制
- 数据链路层的三个基本问题