代码:

 #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. PAT (Basic Level) Practice (中文)1031 查验身份证 (15 分)

    一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8, ...

  2. Wannafly Camp 2020 Day 2D 卡拉巴什的字符串 - 后缀自动机

    动态维护任意两个后缀的lcp集合的mex,支持在串末尾追加字符. Solution 考虑在 SAM 上求两个后缀的 LCP 的过程,无非就是找它们在 fail 树上的 LCA,那么 LCP 长度就是这 ...

  3. Git和TortoiseGit

    1.简介 Git是一个开源的分布式版本控制系统,用于敏捷高效的处理任何或大或小的项目.它采用了分布式版本库的方式,不必服务器端软件支持. 2.Git和Svn的区别 1.Git 是分布式的,SVN 不是 ...

  4. STL中_Rb_tree的探索

    我们知道STL中我们常用的set与multiset和map与multimap都是基于红黑树.本文介绍了它们的在STL中的底层数据结构_Rb_tree的直接用法与部分函数.难点主要是_Rb_tree的各 ...

  5. keepalived高可用工具

    1.准备俩台虚拟机,一台主机,一台备机 我这里模拟的是 主机ip: 192.168.42.66 masternginx 备机ip: 192.168.42.77 slavenginx 虚拟ip: 192 ...

  6. 基于Web的网络商城项目设计与实现【SSM+Bootstrap+Vue】

    [Spring+SpringMVC+MyBatis+Bootstrap+Vue] 演示:线路1  线路2 1.系统功能介绍 网上商城系统 是一个功能完善的在线购物系统 - ,主要为在线销售和在线购物服 ...

  7. AcWing 1013. 机器分配

    //分组背包 for物品 for体积 for 决策 #include <iostream> using namespace std; ; int n, m; int w[N][N]; in ...

  8. mysql 中LIKE 与FIND_IN_SET 与关联表left join 速度效率比较

    有一张表Table有IDStr字段,如下只显示二个字段还有很多其他字段 方式一 字段逗号分割,直接用UserIDStr字段,里面存多个ID用逗号分割 UUID UserIDStr 1111 1,2,3 ...

  9. D0 设计模式

    单一职责 一个类只负责一个功能领域中的相应职责.,就一个类而言,应该只有一个引起它变化的原因. 单一职责原则告诉我们: 一个类不能太"累"! 在软件系统中, 一个类( 大到模块, ...

  10. layer iframe 设置关闭按钮 和刷新

    layer.open({ type: 2, title: 'XXXX网吧历史更多数据', shade:0, // closeBtn:0, resize:false, move:false, shade ...