dfs题型一
代码:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; const int maxn = ; //序列A中n各数选k个数使得和为x,最大平方和为maxSumSqu
int n, k, x, maxSumSqu = -, A[maxn]; vector<int> temp, ans; void dfs(int index, int nowK, int sum, int sumSqu){
if (nowK == k && sum == x){ //如果找到k个数的和为x
if (sumSqu > maxSumSqu){ //且找到的比当前更优
maxSumSqu = sumSqu; //更新最大平方和
ans = temp; //更新最优方案
} return;
} //已经处理完n个数,或者超过k个数,或者和超过x, 返回
if (index == n || nowK > k || sum > x)
return; //选index号数
temp.push_back(A[index]);
dfs(index + , nowK + , sum + A[index], sumSqu + A[index] * A[index]);
temp.pop_back(); //不选index号数
dfs(index + , nowK, sum, sumSqu);
} int main()
{
freopen("in.txt", "r", stdin);
scanf("%d %d %d", &n, &k, &x);
for (int i = ; i < n; i++){
scanf("%d", &A[i]);
printf("%d ", A[i]);
} dfs(, , , ); printf("%d\n", maxSumSqu);
fclose(stdin);
return ;
}
如果每个元素可以重复被选,那么上面的程序只需改动一点点:将29行改为:
dfs(index, nowK + 1, sum + A[index], sumSqu + A[index] * A[index]);
其实这题用K层迭代也能解决。
运用这个思路求解下面这道题:
The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K−P factorization of N for any positive integers N, K and P.
Input Specification:
Each input file contains one test case which gives in a line the three positive integers N (≤400), K (≤N) and P (1<P≤7). The numbers in a line are separated by a space.
Output Specification:
For each case, if the solution exists, output in the format:
N = n[1]^P + ... n[K]^P
where n[i]
(i
= 1, ..., K
) is the i
-th factor. All the factors must be printed in non-increasing order.
Note: the solution may not be unique. For example, the 5-2 factorization of 169 has 9 solutions, such as 122+42+22+22+12, or 112+62+22+22+22, or more. You must output the one with the maximum sum of the factors. If there is a tie, the largest factor sequence must be chosen -- sequence { a1,a2,⋯,aK } is said to be larger than { b1,b2,⋯,bK } if there exists 1≤L≤K such that ai=bi for i<L and aL>bL.
If there is no solution, simple output Impossible
.
Sample Input 1:
169 5 2
Sample Output 1:
169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2
Sample Input 2:
169 167 3
Sample Output 2:
Impossibl
解答代码为:
#include <stdio.h>
#include <vector>
#include <math.h> using namespace std;
const int maxn = ;
int fac[maxn]; //存储所有可能的项
int n, k, p;
int maxFacSum = -;
vector<int> ans, temp; //算出fac数组
int Fac(){
int i = ;
for (i = ; pow(i, p) <= n; i++){
fac[i] = pow(i, p);
}
return i - ;
} //index是fac的下标,nowK是当前已经叠加的项数,sum是当前之和,facSum是底数之和
void dfs(int index, int nowK, int sum, int facSum){
if (nowK == k && sum == n){
if (facSum > maxFacSum){
maxFacSum = facSum;
ans = temp;
}
return;
}
//
if (nowK > k || sum > n) return; if (index >= ){ //选index项
temp.push_back(index);
dfs(index, nowK + , sum + fac[index], facSum + index); //不选index项
temp.pop_back();
dfs(index - , nowK, sum, facSum);
} } int main()
{
//freopen("in.txt", "r", stdin);
scanf("%d %d %d", &n, &k, &p);
int index = Fac();
dfs(index, , , ); //如果ans的元素为空,则说明不存在有效解
if (maxFacSum == -){
printf("Impossible\n");
}
else{
printf("%d = ", n);
for (int i = ; i < k; i++){
printf("%d^%d", ans[i], p);
if (i < k - ){
printf(" + ");
}
}
} //fclose(stdin); return ;
}
dfs题型一的更多相关文章
- dfs题型二(迷宫问题)
取自:<王道论坛计算机考研机试指南>6.5节 例 6.7 Temple of the bone(九度 OJ 1461)时间限制:1 秒 内存限制:32 兆 特殊判题:否题目描述:The d ...
- ACM/ICPC 之 DFS范例(ZOJ2412-ZOJ1008)
通过几道例题简单阐述一下DFS的相关题型 ZOJ2412-Farm Irrigation 直观的DFS题型,稍加变化,记录好四个方向上的通路就能够做出来 题目和接水管类似,问最少要灌溉几次,即求解最少 ...
- 深度优先搜索 DFS(Depath First Search, DFS)
深度优先搜索是一种枚举所有完整路径以遍历所有情况的搜索方法.(不撞南墙不回头) DFS一般用递归来实现,其伪代码思路过程一般如下: void DFS(必要的参数){ if (符和遍历到一条完整路 ...
- 图论--DFS总结
1.Key word:①双向DFS ②回溯 今天就看到了这么多DFS,其实DFS更倾向于枚举所有情况. 对于双向DFS,我们考虑看看最短路,起点做一下搜索,记录一下到所有点的距离,终点做一下搜索,记 ...
- dfs序
dfs序比较重要的性质:一棵子树的所有节点在dfs序里是连续一段,主要就是利用这个性质来解题 题型一:对某个点X权值加上一个数W,查询某个子树X里所有点权值和. 解:列出dfs序,实现修改一个数,查询 ...
- ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)
两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...
- 10324 Global Warming dfs + 二分
时间限制:1000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: G++;GCC Description Global warming is a big prob ...
- 1140 分珠 dfs
时间限制:500MS 内存限制:65536K提交次数:24 通过次数:18 题型: 编程题 语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量不同,珠子之间有一 ...
- 伪Acmer的推理(dfs/bfs)
时间限制:1000MS 内存限制:65535K 提交次数:12 通过次数:9 收入:32 题型: 编程题 语言: C++;C Description 现在正是期末,在复习离散数学的Acmer遇到 ...
随机推荐
- 虚拟机floppy0
网上搜索方法是:删除该虚拟机的软盘即可. 具体原因还不知道,以后再补上原因
- spring整合websocket,如何在服务端依赖注入service
1.在pom.xml文件中添加jar包: <properties> <spring.version>4.0.5.RELEASE</spring.version> & ...
- java实现判断两个二叉树是否相同
1.定义树节点类:节点值.左节点.右节点.构造器 2.先判断树是否为空的情况 3.树不为空时,判断节点所指的值是否相等,若相等,则递归判断节点的左右节点是否相同,相同则返回true /** * Def ...
- 如何在Windows上开启Ping或者禁止PING
方法1:命令行模式 进入服务器后 点击 开始——运行 输入命令: netsh firewall set icmpsetting 8 这样就可以在外部ping到服务器了 非常简单实用! 同样道理,如果想 ...
- H3C ARP配置
一.ARP简介 ARP(Address Resolution Protocol,地址解析协议)是将IP地址解析为以太网MAC地址(或称物理地址)的协议. 在网络中,当主机或其它网络设备有数据要发送给另 ...
- Attention machenism
from attention mechanism Attention is one component of a network’s architecture, and is in charge of ...
- linux - mysql 异常:/usr/bin/which: no mysql in
问题描述 运行:which mysql 报错:/usr/bin/which: no mysql in (/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local ...
- Github中进行fork后,原仓库更新了如何与原仓库同步
我们经常在Github中Fork别人优秀的项目(在自己的GitHub下面生成一个repository),如果原仓库作者或组织更新仓库,此时你Fork的项目并不会更新,如果我们想要更新操作,该如何操作? ...
- [Python]Python日期格式和字符串格式相互转换
由字符串格式转化为日期格式的函数为: datetime.datetime.strptime() 由日期格式转化为字符串格式的函数为: datetime.datetime.strftime() # en ...
- 转: Struts2中拦截器与过滤器的区别及执行顺序
当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标准的过滤器链 c) FilterDispatecher会 ...