代码:

 #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层迭代也能解决。

运用这个思路求解下面这道题:

1103 Integer Factorization (30 分)
 

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 12​2​​+4​2​​+2​2​​+2​2​​+1​2​​, or 11​2​​+6​2​​+2​2​​+2​2​​+2​2​​, 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 { a​1​​,a​2​​,⋯,a​K​​ } is said to be larger than { b​1​​,b​2​​,⋯,b​K​​ } if there exists 1≤L≤K such that a​i​​=b​i​​ for i<L and a​L​​>b​L​​.

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题型一的更多相关文章

  1. dfs题型二(迷宫问题)

    取自:<王道论坛计算机考研机试指南>6.5节 例 6.7 Temple of the bone(九度 OJ 1461)时间限制:1 秒 内存限制:32 兆 特殊判题:否题目描述:The d ...

  2. ACM/ICPC 之 DFS范例(ZOJ2412-ZOJ1008)

    通过几道例题简单阐述一下DFS的相关题型 ZOJ2412-Farm Irrigation 直观的DFS题型,稍加变化,记录好四个方向上的通路就能够做出来 题目和接水管类似,问最少要灌溉几次,即求解最少 ...

  3. 深度优先搜索 DFS(Depath First Search, DFS)

    深度优先搜索是一种枚举所有完整路径以遍历所有情况的搜索方法.(不撞南墙不回头) DFS一般用递归来实现,其伪代码思路过程一般如下: void DFS(必要的参数){    if (符和遍历到一条完整路 ...

  4. 图论--DFS总结

    1.Key word:①双向DFS  ②回溯 今天就看到了这么多DFS,其实DFS更倾向于枚举所有情况. 对于双向DFS,我们考虑看看最短路,起点做一下搜索,记录一下到所有点的距离,终点做一下搜索,记 ...

  5. dfs序

    dfs序比较重要的性质:一棵子树的所有节点在dfs序里是连续一段,主要就是利用这个性质来解题 题型一:对某个点X权值加上一个数W,查询某个子树X里所有点权值和. 解:列出dfs序,实现修改一个数,查询 ...

  6. ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)

    两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...

  7. 10324 Global Warming dfs + 二分

    时间限制:1000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: G++;GCC Description Global warming is a big prob ...

  8. 1140 分珠 dfs

    时间限制:500MS  内存限制:65536K提交次数:24 通过次数:18 题型: 编程题   语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量不同,珠子之间有一 ...

  9. 伪Acmer的推理(dfs/bfs)

    时间限制:1000MS  内存限制:65535K 提交次数:12 通过次数:9 收入:32 题型: 编程题   语言: C++;C Description 现在正是期末,在复习离散数学的Acmer遇到 ...

随机推荐

  1. 剑指offer-面试题28-对称的二叉树-二叉树递归

    /* 题目: 判断给定的二叉树是否对称. */ /* 思路: 1.递归法. 2.基础条件:两棵树均为空为true:一棵树为空,一棵树不为空,为false:两棵树的根节点值不同,为false. 3.其它 ...

  2. 最简单的githut操作命令

    创建SSH Key: 参考:https://blog.csdn.net/weixin_30345055/article/details/95139358 在用户目录下,看看有没有.ssh文件夹,如果有 ...

  3. Java_Day6(上)

    Java learning_Day6 本人学习视频用的是马士兵的,也在这里献上 <链接:https://pan.baidu.com/s/1qKNGJNh0GgvlJnitTJGqgA> 提 ...

  4. 问题 B: 基础排序III:归并排序

    #include <cstdio> #include <vector> #include <algorithm> using namespace std; void ...

  5. sqli-labs less-17 --> less-20

    Less-17 (报错盲注) 参考资料1:https://www.cnblogs.com/AmoBlogs/p/8673748.html

  6. 精简Command版SqlHelper

    我在写CSharp程序对数据库进行操作时发现Connection对象起到了连接数据库的做用,实际执行SQL语句使用的是Command对象的方法,所以对SqlHelper进行了重写,具体如下: 一.创建 ...

  7. vue koa2 mongodb 从零开始做个人博客(一) 登录注册功能前端部分

    0.效果演示 插入视频插不进来,就很烦.可以出门右拐去优酷看下(点我!). 1.准备工作 1.1前端框架 前端使用了基于vue.js的nuxt.js.为什么使用nuxt.js? 首先我做的是博客的项目 ...

  8. 在阿里云IIS服务器上安装证书

    在IIS服务器上安装证书 您可将下载的阿里云SSL证书安装到IIS服务器上,使您的IIS服务器支持HTTPS安全访问. 一.前提条件 申请证书时需要选择 系统自动创建CSR. 申请证书时如果选择手动创 ...

  9. pdo一次插入多条数据的2种实现方式

    pdo一次插入多条数据的2种实现方式: **** 1.一个sql插入多个值,防注入处理放在获取到值的时候使用htmlspecialchars(addslashes($params )); try{ f ...

  10. arm9特点

    ARM9主要特点 ARM 处理器凭借它的低功耗.高性能等特点,被广泛应用于个人通信等嵌入式领域,而ARM7 也曾在中低端手持设备中占据了一席之地.然而,ARM7 的处理性能逐渐无法满足人们日益增长的高 ...