poj--3187--Backward Digit Sums(dfs)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5667 | Accepted: 3281 |
Description
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
Output
Sample Input
- 4 16
Sample Output
- 3 1 2 4
Hint
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- using namespace std;
- int vis[20],s[30][30],n,ans;
- bool f;
- void dfs(int x)
- {
- if(x==n+1)
- {
- for(int i=2;i<=n;i++)
- {
- for(int j=1;j<=n-i+1;j++)
- {
- s[i][j]=s[i-1][j]+s[i-1][j+1];
- }
- }
- if(s[n][1]==ans&&!f)
- {
- f=true;
- for(int i=1;i<n;i++)
- printf("%d ",s[1][i]);
- printf("%d\n",s[1][n]);
- }
- }
- if(f) return ;
- for(int i=1;i<=n;i++)
- {
- if(!vis[i])
- {
- vis[i]=1;
- s[1][x]=i;
- dfs(x+1);
- vis[i]=0;
- }
- }
- }
- int main()
- {
- while(scanf("%d%d",&n,&ans)!=EOF)
- {
- f=false;
- memset(s,0,sizeof(s));
- memset(vis,0,sizeof(vis));
- dfs(1);
- }
- return 0;
- }
poj--3187--Backward Digit Sums(dfs)的更多相关文章
- 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 < ...
- POJ 3187 Backward Digit Sums 枚举水~
POJ 3187 Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3 1 2 4 他可以相邻 ...
- 【POJ - 3187】Backward Digit Sums(搜索)
-->Backward Digit Sums 直接写中文了 Descriptions: FJ 和 他的奶牛们在玩一个心理游戏.他们以某种方式写下1至N的数字(1<=N<=10). 然 ...
- poj 3187 Backward Digit Sums(穷竭搜索dfs)
Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...
- 穷竭搜索:POJ 3187 Backward Digit Sums
题目:http://poj.org/problem?id=3187 题意: 像这样,输入N : 表示层数,输入over表示最后一层的数字,然后这是一个杨辉三角,根据这个公式,由最后一层的数,推出第一行 ...
- 【BZOJ】1653: [Usaco2006 Feb]Backward Digit Sums(暴力)
http://www.lydsy.com/JudgeOnline/problem.php?id=1653 看了题解才会的..T_T 我们直接枚举每一种情况(这里用next_permutation,全排 ...
- POJ 3187 Backward Digit Sums
暴力DFS+验证. 验证如果是暴力检验可能复杂度会太高,事实上可以o(1)进行,这个可以o(n*n)dp预处理. #include<cstdio> #include<cstring& ...
- POJ 3187 Backward Digit Sums (递推,bruteforce)
第1行j列的一个1加到最后1行满足杨辉三角,可以先推出组合数来 然后next_permutation直接暴. #include<cstdio> #include<iostream&g ...
- Backward Digit Sums(POJ 3187)
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5495 Accepted: 31 ...
随机推荐
- SQLSERVER SQL性能优化技巧
这篇文章主要介绍了SQLSERVER SQL性能优化技巧,需要的朋友可以参考下 1.选择最有效率的表名顺序(只在基于规则的优化器中有效) SQLSERVER的解析器按照从右到左的顺序处理F ...
- ASP.net参数传递总结
同一页面.aspx与.aspx.cs之间参数传递 1. .aspx.cs接收.aspx的参数:由于.aspx和.aspx.cs为继承关系,所以.aspx.cs可以直接对.aspx中的ID进行值提取,具 ...
- 使用FastReport的BarCode2D控件生成含中文的PDF417条形码
解决方法:设定CodePage为936 FastReport用户手册中关于CodePage的说明: CodePage This property is specific to the PDF417 a ...
- 【转载】HTTP 请求头与请求体
原文地址: https://segmentfault.com/a/1190000006689767 HTTP Request HTTP 的请求报文分为三个部分 请求行.请求头和请求体,格式如图:一个典 ...
- 5.C#编写Redis访问类
那么通过前面几篇博文,服务端的安装和配置应该没什么问题了,接下来的问题是如何通过代码来访问Redis. 这里我们使用的库为: StackExchange.Redis GitHub:https://gi ...
- centos安装指定mysql
mysql下载地址:http://repo.mysql.com/ nginx下载地址 我下载是这个 http://nginx.org/packages/centos/7/noarch/RPMS/ngi ...
- 【css】最近使用的两种图标字体库
## 0. 前言 比较基础的图标加载:<img src="x.png">和块元素的背景background: url(./x.png). 页面多图标时,使用雪碧图(多个 ...
- Heaters (codeforces 1066B)
贪心题 策略 在最左边设置一个array 0,每一次从右往左,如果有heater的话就寻找heater左边界是不是小于等于目前的上一个heater的右边界,如果没有一个这样的,那么就直接输出-1 代码 ...
- uva 1586 Molar mass(Uva-1586)
这题做的相当的复杂...之前做的现在应该能简单一点了写的. 我的代码: #include <bits/stdc++.h> using namespace std; main() { int ...
- Oracle存储过程及函数的练习题
--存储过程.函数练习题 --(1)创建一个存储过程,以员工号为参数,输出该员工的工资create or replace procedure p_sxt1(v_empno in emp.empno%t ...