http://www.lydsy.com/JudgeOnline/problem.php?id=1653

看了题解才会的。。T_T

我们直接枚举每一种情况(这里用next_permutation,全排列)

然后判断是否符合情况(累加判断)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } int n, m;
int a[15], f[15];
int main() {
read(n); read(m);
for1(i, 1, n) a[i]=i;
do {
memcpy(f, a, sizeof(a));
for1(i, 1, n-1) for3(j, i, 1) f[j]+=f[j+1];
if(f[1]==m) {
printf("%d", a[1]);
for1(i, 2, n) printf(" %d", a[i]);
puts("");
break;
}
}while(next_permutation(a+1, a+1+n));
return 0;
}

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: 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

* 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

4 16

Sample Output

3 1 2 4

OUTPUT DETAILS:

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

HINT

Source

【BZOJ】1653: [Usaco2006 Feb]Backward Digit Sums(暴力)的更多相关文章

  1. BZOJ 1653 [Usaco2006 Feb]Backward Digit Sums ——搜索

    [题目分析] 劳逸结合好了. 杨辉三角+暴搜. [代码] #include <cstdio> #include <cstring> #include <cmath> ...

  2. bzoj 1653: [Usaco2006 Feb]Backward Digit Sums【dfs】

    每个ai在最后sum中的值是本身值乘上组合数,按这个dfs一下即可 #include<iostream> #include<cstdio> using namespace st ...

  3. 1653: [Usaco2006 Feb]Backward Digit Sums

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

  4. BZOJ1653: [Usaco2006 Feb]Backward Digit Sums

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

  5. Backward Digit Sums(暴力)

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

  6. Backward Digit Sums(POJ 3187)

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

  7. POJ3187 Backward Digit Sums 【暴搜】

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4487   Accepted: 25 ...

  8. POJ 3187 Backward Digit Sums 枚举水~

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

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

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

随机推荐

  1. Python enumerate索引迭代

    索引迭代Python中,迭代永远是取出元素本身,而非元素的索引.对于有序集合,元素确实是有索引的.有的时候,我们确实想在 for 循环中拿到索引,怎么办?方法是使用 enumerate() 函数:&g ...

  2. Openerp workflow 工作流批注模块

    转自:http://blog.csdn.net/yeahliu/article/details/17207289 2013-12-22 添加workflow_stop标识,感谢广州-jerry 201 ...

  3. 查看慢sql语句

    查看慢sql语句 st.text AS SQL_Full --父级完整语句 ,) , ((CASE statement_end_offset THEN DATALENGTH(st.text) ELSE ...

  4. profiler跟踪事件存为表之后性能分析工具

    使用profiler建立跟踪,将跟踪结果存到表中,使用下面存储过程执行 exec temp_profiler 'tra_tablename'对表数据进行处理归类,然后进行性能分析 1.先建存储过程 2 ...

  5. tomcat设置jvm参数

    http://www.quiee.com.cn/archives/592/ Tomcat默认可以使用的内存为128MB,Windows下,在文件{tomcat_home}/bin/catalina.b ...

  6. 摘:数据结构各种算法实现(C++模板)

    目  录 1.顺序表. 1 Seqlist.h 1 Test.cpp 6 2.单链表. 8 ListNode.h 8 SingleList.h 10 test.cpp 20 3.双向链表. 22 No ...

  7. 摘:C++:用ADO操作数据库的方法步骤

      ADO接口简介 ADO库包含三个基本接口:_ConnectionPtr接口._CommandPtr接口和_RecordsetPtr接口. _ConnectionPtr接口返回一个记录集或一个空指针 ...

  8. 用C语言实现循环左移和循环右移

    有天重建一段代码时,遇到了循环右移指令,不知道用C语言怎么实现,后来得到小伟指点,感谢.. me 15:56:38004BD2C9 8B55 F8 MOV EDX,DWORD PTR SS:[EBP- ...

  9. ubuntu环境下配置jdk

    方法1:修改/etc/profile 文件 /etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. 所有用户 ...

  10. UE初写小项目问题之命令行报错:Expected indentation of 4 spaces but found 6

    一.问题 刚开始用一个小项目练手,用vue-cli初始化项目,直接安装了所有的东西,如vue-router.ESLint.unit tests等等,当时只懂得vue-router是什么. 问题来了,在 ...