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,全排 ...
随机推荐
- Java Script 中 ==(Equal) 和 === (Identity Equal) 的区别和比较算法逻辑
判断两个变量是否相等在任何编程语言中都是非常重要的功能. JavaScript 提供了 == 和 === 两种判断两个变量是否相等的运算符,但我们开始学习的时候 JavaScript 的时候,就被一遍 ...
- 关于Thread的Runnable和Callable接口
其实非常简单:其实他们的区别就是Callable有返回值并且可以抛出异常. /** * Represents a command that can be executed. Often used to ...
- Android-PullToRefresh下拉刷新库基本用法
How:(使用) 转自:http://blog.csdn.net/hantangsongming/article/details/42490277 PullToRefresh是一套实现非常好的下拉刷新 ...
- 移动前端之viewport
在移动设备上进行网页的重构或开发,首先得搞明白的就是移动设备上的viewport了,只有明白了viewport的概念以及弄清楚了跟viewport有关的meta标签的使用,才能更好地让我们的网页适配或 ...
- Android学习总结——Activity状态保存和恢复
Android中启动一个Activity如果点击Home键该Activity是不会被销毁的,但是当进行某些操作时某些数据就会丢失,如下: Java class: package com.king.ac ...
- 简易实现 TextView单行文本水平触摸滑动效果
为了方便查看,已使用markdown编辑形成新博文. 本文Mardown地址 近期做应用的时候实用到TextView单行长文本,当文本内容过长时候又想实现触摸水平滑动效果. 网上找了非常多,都没有看到 ...
- openfire研究之部署连接管理器(connection manager)
http://blog.sina.com.cn/s/blog_7325f5150101bafh.html 一. Openfire Connection Manager 简介 Openfire Conn ...
- html5 乒乓球(碰撞检测)
演示地址 http://koking.8u.hanmandarin.com/html5/1.html 简单介绍 小球可以在方框内部自由运动 可以通过方向键控制黑色砖块上下左右移动去与小球发生碰撞 代码 ...
- 手机端页面自适应:rem布局
rem布局非常简单,首页你只需在页面引入这段原生js代码就可以了 (function (doc, win) { var docEl = doc.documentElement, resizeEvt = ...
- 使用sumlime text有感
学习JS和CSS.HTML用sublime text挺好的,这个工具不仅打开速度很快,而且也容易让页面运行(右键,在浏览器中打开),新建html页面的时候,先写个感叹号,然后按ctrl+e,会自动生成 ...