http://poj.org/problem?id=3187

给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的。

暴力枚举题,枚举生成的每一个全排列,符合即退出。

dfs版:

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
//#include <map>
#include <queue> #define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);
#define maxn 1000000000
#define N 1010
using namespace std; int n,sum,flag;
int pos[],num[],cnt[],vis[]; bool solve()
{
for(int i=;i<=n;i++) num[i]=pos[i];
for(int i=n;i>;i--)
{
for(int j=;j<i;j++)
num[j]=num[j]+num[j+];
}
if(num[]==sum) return true;
return false;
}
void dfs(int k)
{
if(flag) return;
if(k==n+)
{
if(solve())
{
for(int i=;i<=n;i++)
if(i==n) printf("%d\n",pos[i]);
else printf("%d ",pos[i]);
flag=;
return;
}
}
for(int i=;i<=n;i++)
{
if(!vis[i])
{
pos[k]=i;
vis[i]=;
dfs(k+);
vis[i]=;
}
}
return;
}
int main()
{
//freopen("a.txt","r",stdin);
// freopen("b.txt","w",stdout);
scanf("%d%d",&n,&sum);
memset(vis,,sizeof(vis));
flag=;
dfs();
return ;
}

stl版:

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,sum,pos[]; bool check()
{
int dp[];
for(int i=;i<=n;i++)
dp[i]=pos[i];
for(int i=n;i>;i--)
{
for(int j=;j<i;j++)
{
dp[j]=dp[j]+dp[j+];
}
}
if(dp[]==sum) return true;
return false;
} void pri()
{
for(int i=;i<=n;i++)
if(i==n) printf("%d\n",pos[i]);
else printf("%d ",pos[i]);
}
void dfs()
{
for(int i=;i<=n;i++)
pos[i]=i;
do {
if(check()) {pri();break;}
}while(next_permutation(pos+,pos+n+));
}
int main()
{
scanf("%d%d",&n,&sum);
dfs();
return ;
}

POJ-3187 Backward Digit Sums (暴力枚举)的更多相关文章

  1. POJ 3187 Backward Digit Sums 枚举水~

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

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

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

  3. POJ 3187 Backward Digit Sums

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

  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 (递推,bruteforce)

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

  6. 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 < ...

  7. Backward Digit Sums(暴力)

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5664   Accepted: 32 ...

  8. Backward Digit Sums(POJ 3187)

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

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

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

  10. BZOJ1653: [Usaco2006 Feb]Backward Digit Sums

    1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 207  Solved:  ...

随机推荐

  1. 【递推】BZOJ 4300:绝世好题

    4300: 绝世好题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 564  Solved: 289[Submit][Status][Discuss] ...

  2. Leetcode#137 Single Number II

    原题地址 遍历所有数字,统计每一位出现的次数,模3即为只出现一次的数字在那一位的情况. 代码: int singleNumber(int A[], int n) { ] = {}; ; i < ...

  3. Core Data 版本数据迁移

    Core Data版本迁移基础 通常,在使用Core Data的iOS App上,不同版本上的数据模型变更引发的数据迁移都是由Core Data来负责完成的.这种数据迁移模式称为Lightweight ...

  4. Science:给青年科研工作者的忠告

  5. aspose.cell 自定义模板 SUM无效

    数字类型的单元格, 显示   解决方案: 绑定的DataTable的列为字符串类型. 应该将其设置成数字类型的列

  6. Lua require搜索路径指定方法

    在自己的lua文件中,如果使用到了自己写的C库或者第三方库,想让lua编译到自己指定的目录下寻找*.lua或*.so文件的时候,可以再自己的Lua代码中添加如下代码,可以指定require搜索的路径. ...

  7. js中的null VS undefined

    var a;------>undefined. JS变量的默认值.注意点在于判断变量的值为null.这是错误的.比如 if( a === null ) { // TODO; }; 实际上是und ...

  8. 基于jQuery很牛X的批量上传插件

    上传功能应该是每个网站必备的工具之一,因此出现了出现了很多各式各样的上传插件! 本文基于个人经验和使用从插件的:交互体验,易用性,文档,美观度出发,为大家推荐三款很NX的批量上传插件! 下面三款插件的 ...

  9. redis 数据库维护之 key 大小获取

    获得 redis key 大小 redis 用过一段时间后,发现一个KEY每天需更新值,但总是更新不全,故此为了定位问题,整理此脚本,辅助监控一下 写了两个脚本 注意:需要提前从 https://gi ...

  10. Winform设置相关

    >>  Winform查找根目录 1) AppDomain.CurrentDomain.BaseDirectory 地址为: d:\MyProject\Bin\  Application. ...