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. c++ union

    什么是union? 翻译过来说,就是共用体,或者也叫联合体.说到了union,也就是共用体,就不得不说一下struct了,当我们有如下的struct的定义时:   1 2 3 4 5 6 struct ...

  2. Detecting diabetic retinopathy in eye images

    Detecting diabetic retinopathy in eye images The past almost four months I have been competing in a  ...

  3. 17+个ASP.NET MVC扩展点,含源码{转}

    1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig.在自定义的HttpMo ...

  4. samsung-smart app 开发

    http://www.samsungdforum.com/ http://seller.samsungapps.com/login/signIn.as?returnURL=%2fmain%2fsell ...

  5. Unity3D 运营商支付 安卓打包的陷阱 libunicomsdk.jar

    原地址:http://blog.csdn.net/alking_sun/article/details/36624491 想想做Unity3D SDK集成已经快2年了,遇到过不少很棘手的SDK,其中以 ...

  6. Java 中最常见的 5 个错误

    在编程时,开发者经常会遭遇各式各样莫名错误.近日,Sushil Das 在 Geek On Java上列举了 Java 开发中常见的 5 个错误,与君共「免」. 原文链接:Top 5 Common M ...

  7. IE6中常见兼容性问题及浏览器显示难题

    1.双倍边距Bug 问题描述:假如有一个ul,里面有若干li,当li设置为左浮动时,此时设置li的margin-left为10px,会在最左侧呈现双倍情况.即20px 正常显示: IE6显示: 修正方 ...

  8. POJ 2027

    #include<iostream> using namespace std; int main() { int time; cin>>time; int a; int b; ...

  9. C#实现文件增量备份

    最近将客户的一个ASP网站部署到了公司的机房云服务器上,该ASP网站的文件总容量已有将近4GB. 虽然现在硬盘容量很大,但每天一次完整备份的话,那占用的硬盘空间会急剧上升,考虑一个更优的备份方案就是每 ...

  10. MYSQL 遭遇 THE TOTAL NUMBER OF LOCKS EXCEEDS THE LOCK TABLE SIZE

    今天进行MySql 一个表数据的清理,经过漫长等待后出现 The total number of locks exceeds the lock table size 提示.以为是table_cache ...