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遇到 ...
随机推荐
- 网络共享服务(二)之NFS
NFS: Network File System,是一种基于TCP/IP传输的网络文件系统协议,最初由SUN公司开发.通过使用NFS协议,客户机可以像访问本地目录一样访问服务器中的共享资源.但是,NF ...
- 洛谷P1936 水晶灯火灵 P1775 古代人的难题_NOI导刊2010提高(02)【重题请做P1936】
首先我要说明,此题(古代人的难题)与水晶灯火灵是一模一样的! 古代人的难题 (File IO): input:puzzle.in output:puzzle.out 时间限制: 1000 ms 空间 ...
- css使用padding-bottom百分比进行提前占位,防止抖动
页面加载抖动问题 在web开发中,经常会遇到这样一个问题,比如一个宽度百分百,高度自适应的图片,在网速慢的情况下加载过程中会出现抖动的问题(未加载图片前容器的高度为0,图片加载完成后下面的内容会被挤下 ...
- win10专业工作站版密钥|win10专业工作站版key|win10专业工作站版激活码
一.windows10专业工作站零售版密钥 永久激活 Win 10 ProfessionalWorkstation Retail [Key]:DXG7C-N36C4-C4HTG-X4T3X-2YV77 ...
- b站德云社相声合集
每天都做德云小可爱呀 郭德纲于谦相声合集搜索: 75314217.75079477 62444678.60874866 60745041.60514509 之前在喜马拉雅上面听过,部分高清的要会员,只 ...
- Linux下用Bash语言实现简单排序的功能
题目链接: 题目描述 利用指针,编写一个函数实现三个整数按由小到大的排序. 输入 三个整数 输出 由小到大输出成一行,每个数字后面跟一个空格 样例输入 2 3 1 样例输出 1 2 3 复习下Linu ...
- 远程执行本地脚本_linux
一.远程执行脚本 1.免机器指纹确认(无需填写yes/no) ssh -o StrictHostKeyChecking=no root@192.168.108.78 2.远程执行本地脚本 ssh -o ...
- PHP获取小程序码并返回前端显示图片
小程序的二维码分为小程序码和二维码: 生成小程序二维码文档中说后端来生成. 参考 小程序开发文档资料:https://developers.weixin.qq.com/miniprogram/dev/ ...
- 不同页面获取同一cookie变量值不同的问题及解决方法
在使用cookie时发现不同页面获取到的同一个cookie变量的值不同,本篇博客介绍其中一种情况的解决方法,通过设置path的方法可使得在同一个网站下获取的cookie变量一致. 问题描述 在www. ...
- Base64编码和其在图片的传输的应用
Base64 [原文链接] 目前Base64已经成为网络上常见的传输8Bit字节代码的编码方式之一.做支付系统时,系统之间的报文交互都需要使用Base64对明文进行转码,然后再进行签名或加密,之后再进 ...