POJ 3187 Backward Digit Sums (dfs,杨辉三角形性质)
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>
using namespace std;
int a[][],n,sum,vis[],ans[];
bool f;
void setnum ()//生成杨辉三角形
{
for (int i=;i<n;++i)
{
a[i][]=;
a[i][i]=;
}
for (int i=;i<n;++i)
for (int j=;j<i;++j)
a[i][j]=a[i-][j-]+a[i-][j];
}
void printAns ()
{
for (int i=;i<n;++i)
{
printf("%d",ans[i]);
if (i!=n-)
printf(" ");
}
printf("\n");
}
void dfs (int nowsum,int step)//这样的搜索是刚好字典序的
{
if (step==n)
{
if (nowsum==sum)
{
f=true;
printAns();
}
return ;
}
if (f||nowsum>sum)
return ;
for (int i=;i<=n;++i)
{
if (vis[i])
continue;
vis[i]=;
ans[step]=i;
dfs(nowsum+i*a[n-][step],step+);
vis[i]=;//每次搜完以后要清空状态
}
}
int main()
{
scanf("%d%d",&n,&sum);
setnum();
f=false;
memset(vis,,sizeof vis);
memset(ans,,sizeof ans);
dfs(,);
return ;
}
POJ 3187 Backward Digit Sums (dfs,杨辉三角形性质)的更多相关文章
- 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(穷竭搜索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表示最后一层的数字,然后这是一个杨辉三角,根据这个公式,由最后一层的数,推出第一行 ...
- 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 ...
- 【POJ - 3187】Backward Digit Sums(搜索)
-->Backward Digit Sums 直接写中文了 Descriptions: FJ 和 他的奶牛们在玩一个心理游戏.他们以某种方式写下1至N的数字(1<=N<=10). 然 ...
- P1118 [USACO06FEB]Backward Digit Sums G/S
P1118 [USACO06FEB]Backward Digit Sums G/S 题解: (1)暴力法.对1-N这N个数做从小到大的全排列,对每个全排列进行三角形的计算,判断是否等于N. 对每个 ...
- Backward Digit Sums(暴力)
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5664 Accepted: 32 ...
随机推荐
- nil,Nil,NULL和NSNull的区别
- webbrowser控件显示word文档
参照某网站上的步骤(http://www.kuqin.com/office/20070909/968.html)首先,在Visual Studio中创建一个C#语言的Windows应用程序,然后在左侧 ...
- angualr6 引入iframe
项目开发中需要在angular项目中嵌入iframe窗口,上网搜索了相关文档,不是很多,但是总算是把功能实现了,现记录一下,便于后期查看: step1:在.html中放入需要承载内容的div,并定义好 ...
- Unity编程标准导引-3.1 Component 组件脚本及其基本生命周期
本文为博主原创文章,欢迎转载,请保留出处:http://blog.csdn.net/andrewfan 3.1组件 Component 组件是Unity中最核心的一个概念,它是一切编程的基础.没有组件 ...
- 小程序 js 判断 字符串 为空 null
判断字符串是否为空 1 2 3 4 5 var strings = ''; if (string.length == 0) { alert('不能为空'); } 判断字符串是否为“空”字符即用户输入了 ...
- 请教怎么查询ORACLE的历史操作记录!
请问如何查询ORACLE的历史操作记录!!!!!我用的是linux oracle 11g r2,想查一下前几天的数据库的历史操作记录,例如对表的insert,delete,update等等的操作记录, ...
- git add 添加错文件的撤销方法
git add 添加 多余文件 这样的错误是由于,有的时候 可能 git add . (空格+ 点) 表示当前目录所有文件,不小心就会提交其他文件 git add 如果添加了错误的文件的话 撤销操作 ...
- ceph-cluster map
知道cluster topology,是因为这5种cluster map. ====================================== 知道cluster topology,是因为这 ...
- PAT 2019-3 7-4 Structure of a Binary Tree
Description: Suppose that all the keys in a binary tree are distinct positive integers. Given the po ...
- 2018 China Collegiate Programming Contest Final (CCPC-Final 2018)(A B G I L)
A:签到题,正常模拟即可. #include<bits/stdc++.h> using namespace std; ; struct node{ int id, time; }; nod ...