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

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

Hint

Explanation of the sample: 
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.
题解:让根据结果16找开始的序列3 1 2 4
3   1   2   4
    4  3  6
      7  9
      16
刚看到这个题就有个想法暴力所有全排列找答案,不过直接被我排除了,首先麻烦而且还可能超时,PS(n<=10超时个鬼啊);然后我就找到了一个错误的规律:
ad=((n-1)*n*(n+1)/2-sum)/(n-2);
首末两项之和确定。。。暂且不知道对错,被我调试了N长时间后,我放弃了,直接暴力就A了。。。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int MAXN=;
int ans[MAXN];
int vis[MAXN];
int N,M;
int flot;
int a[];
void dfs(int num){
if(flot)return;
if(num==N){
int sum=,temp=N;
for(int i=;i<N;i++)a[i]=ans[i];
while(temp>){
for(int i=;i<temp-;i++){
a[i]+=a[i+];
}
temp--;
}
sum=a[];
if(sum==M){
for(int i=;i<N;i++){
if(i)P_;
printf("%d",ans[i]);
}
puts("");
flot=;
}
return ;
}
for(int i=;i<N;i++){
if(vis[i+])continue;
ans[num]=i+;
vis[i+]=;
dfs(num+);
vis[i+]=;
}
}
int main(){
while(~scanf("%d%d",&N,&M)){
if(N==){
puts("");continue;
}
mem(vis,);
flot=;
dfs();
}
return ;
}

另一种写法:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int sum;
bool cal(int (*a)[], int N){ for(int i = ; i <= N; i++){
for(int j = ; j <= N - i + ; j++){
a[i][j] = a[i - ][j] + a[i - ][j + ];
}
}
if(a[N][] == sum)
return true;
else
return false; }
int main(){
int N;
int a[][]; while(~scanf("%d%d", &N, &sum)){
for(int i = ; i <= N; i++){
a[][i] = i;
} do{
if(cal(a, N)){
for(int i = ; i <= N; i++){
printf("%d", a[][i]);
if(i != N){
printf(" ");
}else{
printf("\n");
}
}
break;
}
}while(next_permutation(a[] + , a[] + N + ));
}
return ;
}
 

Backward Digit Sums(暴力)的更多相关文章

  1. BZOJ1653: [Usaco2006 Feb]Backward Digit Sums

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

  2. Backward Digit Sums(POJ 3187)

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

  3. 1653: [Usaco2006 Feb]Backward Digit Sums

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

  4. POJ3187 Backward Digit Sums 【暴搜】

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

  5. POJ 3187 Backward Digit Sums 枚举水~

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

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

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

  7. P1118 [USACO06FEB]Backward Digit Sums G/S

    P1118 [USACO06FEB]Backward Digit Sums G/S 题解:  (1)暴力法.对1-N这N个数做从小到大的全排列,对每个全排列进行三角形的计算,判断是否等于N.  对每个 ...

  8. POJ-3187 Backward Digit Sums (暴力枚举)

    http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...

  9. 【BZOJ】1653: [Usaco2006 Feb]Backward Digit Sums(暴力)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1653 看了题解才会的..T_T 我们直接枚举每一种情况(这里用next_permutation,全排 ...

随机推荐

  1. Android事件侦听器回调方法浅谈

    http://developer.51cto.com/art/201001/180846.htm Android事件侦听器作为视图View类的接口,其中包含有不少回调方法,比如:onClick():o ...

  2. 【改造Linux命令之rm - 删除文件或目录-】

    用途说明 rm命令是常用的命令,用来删除文件或目录(remove files or directories).它也是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比 ...

  3. Centos安装webbench

    webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好,安装使用也特别方便. 1.适用系统:Linux 2.编译安装: 引用 wget htt ...

  4. jQuery改造插件,添加回调函数

    <script language="javascript" type="text/javascript"> function doSomething ...

  5. 关于k-means聚类算法的matlab实现

    在数据挖掘中聚类和分类的原理被广泛的应用. 聚类即无监督的学习. 分类即有监督的学习. 通俗一点的讲就是:聚类之前是未知样本的分类.而是根据样本本身的相似性进行划分为相似的类簇.而分类 是已知样本分类 ...

  6. 基于最大最小距离的分类数目上限K确定的聚类方法

    聚类是数据挖掘很重要的组成部分.而大多数聚类算法都需要事先确定分类数目K.而本文是在实际 情况下确定分类数目K的上限.进而对数据样本进行自动分类. 首先介绍下最大最小距离算法: 设样本集为X{x(1) ...

  7. MapReduce工作机制

    MapReduce是什么? MapReduce是一种分布式计算模型,由Google提出,主要用于搜索领域,MapReduce程序本质上是并行运行的,因此可以解决海量数据的计算问题. MapReduce ...

  8. check cable connection PXE-M0F: Exiting intel PXE ROM no bootable device-- insert boot disk and pre

    今天修电脑遇到一个问题,新买的电脑的原装的是linux,然后我按常规方式进入PE后重装系统,然后开机一直显示下面的代码,进不去: check cable connection PXE-M0F: Exi ...

  9. 《JavaScript 闯关记》之初探

    当学习一门新的编程语言的时候,应该边学边做,反复演练以加深理解.因此,你需要一个 JavaScript 解释器.幸运的是,每一个 Web 浏览器都包含一个 JavaScript 解释器. 可以通过在 ...

  10. 引言:Canvas绘图API快速入门

    引言:Canvas绘图API快速入门 在接触HTML5的初学者包括我都在很多地方见到非常炫的一些页面,甚至好多学习HTML5的开发者都是冲着Web端的页游去的,那么HTML5那么绚丽的页面效果以及游戏 ...