Backward Digit Sums(暴力)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5664 | Accepted: 3280 |
Description
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
Output
Sample Input
4 16
Sample Output
3 1 2 4
Hint
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.
4 3 6
7 9
16
#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(暴力)的更多相关文章
- BZOJ1653: [Usaco2006 Feb]Backward Digit Sums
1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 207 Solved: ...
- Backward Digit Sums(POJ 3187)
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5495 Accepted: 31 ...
- 1653: [Usaco2006 Feb]Backward Digit Sums
1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 285 Solved: ...
- POJ3187 Backward Digit Sums 【暴搜】
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4487 Accepted: 25 ...
- POJ 3187 Backward Digit Sums 枚举水~
POJ 3187 Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3 1 2 4 他可以相邻 ...
- 【POJ - 3187】Backward Digit Sums(搜索)
-->Backward Digit Sums 直接写中文了 Descriptions: FJ 和 他的奶牛们在玩一个心理游戏.他们以某种方式写下1至N的数字(1<=N<=10). 然 ...
- P1118 [USACO06FEB]Backward Digit Sums G/S
P1118 [USACO06FEB]Backward Digit Sums G/S 题解: (1)暴力法.对1-N这N个数做从小到大的全排列,对每个全排列进行三角形的计算,判断是否等于N. 对每个 ...
- POJ-3187 Backward Digit Sums (暴力枚举)
http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...
- 【BZOJ】1653: [Usaco2006 Feb]Backward Digit Sums(暴力)
http://www.lydsy.com/JudgeOnline/problem.php?id=1653 看了题解才会的..T_T 我们直接枚举每一种情况(这里用next_permutation,全排 ...
随机推荐
- Check .NET Version with Inno Setup
原文 http://www.kynosarges.org/DotNetVersion.html Inno Setup by Jordan Russell is a great installation ...
- linux上应用随机启动
这是个go项目,其他的可以参考. 首先要有个脚本比如demo #!/bin/bash # # etcd This shell script takes care of starting and sto ...
- Erlang语言介绍
Erlang (/ˈɜrlæŋ/ er-lang) is a general-purpose concurrent, garbage-collected programming language an ...
- 让app在ios6上具有ios7的扁平效果
使用cocoapods在工程中加入UI7Kit,关于UI7Kit请自行google. 加入到工程 如果没安装cocoapods,则安装.(http://www.cocoapods.org) 安装方法: ...
- Android解决异常apk on device '0292bea1': Unable to open sync connection!
方式一:使用手机管家(如腾讯手机管家,只要拖动发射火箭就行了)清理一下正在运行的后台程序. 方式二:把USB数据线拔了重新链接. 方法三:找到USB调试,关掉USB调试,然后重新开启.在设置 --&g ...
- 移动web开发前准备知识了解(html5、jquery)笔记
1.经常使用 插件工具 chrome插件: Mobile & Tablet Emulator(用于常见移动端适配):(重点) Mobile Emulator is an useful o ...
- The 5th tip of DB Query Analyzer
The 5th tip of DB Query Analyzer Ma Genfeng (Guangdong UnitollServices incorporated, G ...
- UIScrollView 与 UIPageView 的联合使用
@interface ViewController : UIViewController<UIScrollViewDelegate> { UIScrollView * scrollV ...
- javascript常用的107个语句
1.document.write(“”); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是: document->html->(head,body) 4.一个浏览器窗口中的 ...
- MySQL初始化故障-----mysql_config_editor中的坑
今天准备新启一个MySQL实例,结果竟然无法初始化,内容如下: -------------------------------------------------------------------- ...