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. Oracle EBS-SQL (BOM-19):主BOM与替代BOM互换.sql

    替代BOM与主BOM互相转换 BOM: 1-01-27-211       子件:1-01-27-416  ID:2202 BOM替代项:替代0001   子件: 1-01-26-204   ID:2 ...

  2. 一个简单的倒计时js插件

    接收的参数end是必须传的,格式是/分隔的日期字符串,start是可选的,不传就是从现在开始倒计时,callback也是可选的,到倒计时接收时执行自定义的函数. countdown({ 'end':' ...

  3. Nand flash 的发展和eMMC

    讨论到eMMC的发展历程,必须要从介绍Flash的历史开始 Flash分为两种规格:NOR Flash和NAND Flash,两者均为非易失性闪存模块. 1988年,Intel首次发出NOR flas ...

  4. NFC通信的模式选择

    原帖请参照:http://www.nfcchina.org/forum.php?mod=viewthread&tid=68&extra=page%3D1 1.nfc 怎么选择操作模式的 ...

  5. redhat 安装virtualbox

    1.在官网下载virtual for linux 包,链接地址:https://www.virtualbox.org/wiki/Linux_Downloads.根据需要下载对应镜像. 2.由于下载的包 ...

  6. linux底半部机制在视频采集驱动中的应用

    最近在做一个arm+linux平台的视频驱动.本来这个驱动应该是做板子的第三方提供的,结果对方软件实力很差,自己做不了这个东西,外包给了一个暑期兼职的在读博士.学生嘛,只做过实验,没做过产品,给出的东 ...

  7. 浅谈JNDI的使用

    原文:http://www.weicoop.com/web/article/52.html 关于什么是JNDI的概念这里不做解释,本文作为初学者根据所了解到内容做些总结,主要内容如下: 1.JNDI使 ...

  8. Signal ()函数详细介绍 Linux函数

    http://blog.csdn.net/ta893115871/article/details/7475095 Signal ()函数详细介绍 Linux函数 signal()函数理解 在<s ...

  9. 黑马程序员 Java基础<九>---> 多线程

    ASP.Net+Android+IOS开发..Net培训.期待与您交流! 多线程 一.概述: 1.线程是什么 说到线程,我们就得先说说进程.所谓进程,就是一个正在执行(进行)中的程序,每一个进程执行都 ...

  10. 3-07. 求前缀表达式的值(25) (ZJU_PAT数学)

    题目链接:http://pat.zju.edu.cn/contests/ds/3-07 算术表达式有前缀表示法.中缀表示法和后缀表示法等形式.前缀表达式指二元运算符位于两个运算数之前,比如2+3*(7 ...