Backward Digit Sums
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5667   Accepted: 3281

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:

  1. 3 1 2 4
  2.  
  3. 4 3 6
  4.  
  5. 7 9
  6.  
  7. 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

  1. 4 16

Sample Output

  1. 3 1 2 4

Hint

Explanation of the sample:

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

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. int vis[20],s[30][30],n,ans;
  7. bool f;
  8. void dfs(int x)
  9. {
  10. if(x==n+1)
  11. {
  12. for(int i=2;i<=n;i++)
  13. {
  14. for(int j=1;j<=n-i+1;j++)
  15. {
  16. s[i][j]=s[i-1][j]+s[i-1][j+1];
  17. }
  18. }
  19. if(s[n][1]==ans&&!f)
  20. {
  21. f=true;
  22. for(int i=1;i<n;i++)
  23. printf("%d ",s[1][i]);
  24. printf("%d\n",s[1][n]);
  25. }
  26. }
  27. if(f) return ;
  28. for(int i=1;i<=n;i++)
  29. {
  30. if(!vis[i])
  31. {
  32. vis[i]=1;
  33. s[1][x]=i;
  34. dfs(x+1);
  35. vis[i]=0;
  36. }
  37. }
  38. }
  39. int main()
  40. {
  41. while(scanf("%d%d",&n,&ans)!=EOF)
  42. {
  43. f=false;
  44. memset(s,0,sizeof(s));
  45. memset(vis,0,sizeof(vis));
  46. dfs(1);
  47. }
  48. return 0;
  49. }

poj--3187--Backward Digit Sums(dfs)的更多相关文章

  1. POJ 3187 Backward Digit Sums (dfs,杨辉三角形性质)

    FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N < ...

  2. POJ 3187 Backward Digit Sums 枚举水~

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

  3. 【POJ - 3187】Backward Digit Sums(搜索)

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

  4. poj 3187 Backward Digit Sums(穷竭搜索dfs)

    Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...

  5. 穷竭搜索:POJ 3187 Backward Digit Sums

    题目:http://poj.org/problem?id=3187 题意: 像这样,输入N : 表示层数,输入over表示最后一层的数字,然后这是一个杨辉三角,根据这个公式,由最后一层的数,推出第一行 ...

  6. 【BZOJ】1653: [Usaco2006 Feb]Backward Digit Sums(暴力)

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

  7. POJ 3187 Backward Digit Sums

    暴力DFS+验证. 验证如果是暴力检验可能复杂度会太高,事实上可以o(1)进行,这个可以o(n*n)dp预处理. #include<cstdio> #include<cstring& ...

  8. POJ 3187 Backward Digit Sums (递推,bruteforce)

    第1行j列的一个1加到最后1行满足杨辉三角,可以先推出组合数来 然后next_permutation直接暴. #include<cstdio> #include<iostream&g ...

  9. Backward Digit Sums(POJ 3187)

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

随机推荐

  1. SQLSERVER SQL性能优化技巧

    这篇文章主要介绍了SQLSERVER SQL性能优化技巧,需要的朋友可以参考下 1.选择最有效率的表名顺序(只在基于规则的优化器中有效)       SQLSERVER的解析器按照从右到左的顺序处理F ...

  2. ASP.net参数传递总结

    同一页面.aspx与.aspx.cs之间参数传递 1. .aspx.cs接收.aspx的参数:由于.aspx和.aspx.cs为继承关系,所以.aspx.cs可以直接对.aspx中的ID进行值提取,具 ...

  3. 使用FastReport的BarCode2D控件生成含中文的PDF417条形码

    解决方法:设定CodePage为936 FastReport用户手册中关于CodePage的说明: CodePage This property is specific to the PDF417 a ...

  4. 【转载】HTTP 请求头与请求体

    原文地址: https://segmentfault.com/a/1190000006689767 HTTP Request HTTP 的请求报文分为三个部分 请求行.请求头和请求体,格式如图:一个典 ...

  5. 5.C#编写Redis访问类

    那么通过前面几篇博文,服务端的安装和配置应该没什么问题了,接下来的问题是如何通过代码来访问Redis. 这里我们使用的库为: StackExchange.Redis GitHub:https://gi ...

  6. centos安装指定mysql

    mysql下载地址:http://repo.mysql.com/ nginx下载地址 我下载是这个 http://nginx.org/packages/centos/7/noarch/RPMS/ngi ...

  7. 【css】最近使用的两种图标字体库

    ## 0. 前言 比较基础的图标加载:<img src="x.png">和块元素的背景background: url(./x.png). 页面多图标时,使用雪碧图(多个 ...

  8. Heaters (codeforces 1066B)

    贪心题 策略 在最左边设置一个array 0,每一次从右往左,如果有heater的话就寻找heater左边界是不是小于等于目前的上一个heater的右边界,如果没有一个这样的,那么就直接输出-1 代码 ...

  9. uva 1586 Molar mass(Uva-1586)

    这题做的相当的复杂...之前做的现在应该能简单一点了写的. 我的代码: #include <bits/stdc++.h> using namespace std; main() { int ...

  10. Oracle存储过程及函数的练习题

    --存储过程.函数练习题 --(1)创建一个存储过程,以员工号为参数,输出该员工的工资create or replace procedure p_sxt1(v_empno in emp.empno%t ...