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遇到 ...
随机推荐
- ASP.NET Identity系列教程-4【Identity高级技术】
https://www.cnblogs.com/r01cn/p/5194257.html 15 ASP.NET Identity高级技术 In this chapter, I finish my de ...
- DSP定点与浮点计算
在定点DSP芯片中,采用定点数进行数值运算,其操作数一般采用整型数来表示.一个整型数的最大表示范围取决于DSP芯片所给定的字长,一般为16位或24位.显然,字长越长,所能表示的数的范围越大,精度也越高 ...
- PTA 1002 A+B for Polynomials
问题描述: This time, you are supposed to find A+B where A and B are two polynomials. Input Specification ...
- MySQL数据库优化(一)
1.1.1. 慢查询日志当查询超过一定的时间没有返回结果的时候,才会记录到慢查询日志中.默认不开启.采样的时候手工开启.可以帮助我们找出执行慢的 SQL 语句查看慢 SQL 日志是否启用(on 表示启 ...
- 在 myeclipse 引入项目
1.进入 myeclipse 在界面空白处,右键,出现如下图所示 2.选择 Import...,弹出如下图所示界面 3.双击上图红框内的内容,弹出如下图所示界面,然后点击按钮“Browse...”,选 ...
- Django 上下文管理器,为父模板添加动态数据
1.摘要:模板继承可以减少页面内容的重复定义,实现页面内容的重用. 但是当父模板中有动态数据的话,这些动态数据在子模版中是不会显示的.我们可以通过自定义上下文处理器来解决 2.Django上下文处理器 ...
- html css二级导航栏
二级导航栏制作: 1.将一级导航栏去除列表样式(list-style:none),并给予浮动,使其横向排列(float:left) 2.给每个li中添加一个<a></a>标签, ...
- c#快速热身
一.选择结构: 1. if选择结构 2. if-else选择结构 3. if-else if-else if-else多重if选择结构 4. if-if-else-else 嵌套if选择结构 5. s ...
- 插入jupyter notebook代码
<iframe src="https://nbviewer.jupyter.org/gist/gaowenxin95/53408e0f1ce268430efaad2cb1f0ca4f& ...
- 反射_python
一.反射(通过字符串的形式去操作对象中的成员) 1.getattr:获取对象中的字段和方法 2.hasattr:判断对象里面是否有字段或方法 3.setattr:设置对象里面的字段或方法 4.dela ...