题意:

给三个正整数N、K、P,将N表示成K个正整数(可以相同,递减排列)的P次方和,如果有多种方案,选择底数n1+…+nk最大的方案,如果还有多种方案,选择底数序列的字典序最大的方案

思路:

这题看时间和n的大小就知道估计是用dfs做,不过我回溯烂的很,没写出来,下面是参考别人代码写的。

注意codeblock中pow返回时会损失精度,pow(5,2)如果用int显示为24,解决方法是+0.5或者用double表示,vs和vc中没有这个问题。

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int maxn = 505;
int n, k, p;
vector<int> tempAns,v,ans;
int maxFacSum = -1;void init() {for (int i = 0; i <= pow(n, 1.0 / p); i++) {v.push_back(pow(i, p));}
}void dfs(int index, int tempSum, int tempK, int facSum) {if (tempSum == n&&tempK == k) {if (facSum > maxFacSum) {ans = tempAns;maxFacSum = facSum;}return;}if (tempSum > n || tempK > k) return;if (index >= 1) {tempAns.push_back(index);dfs(index, tempSum + v[index], tempK + 1, facSum + index);tempAns.pop_back();dfs(index - 1, tempSum, tempK, facSum);}
}int main() {scanf("%d%d%d", &n, &k, &p);init();dfs(v.size()-1, 0,0,0);if (maxFacSum == -1) {printf("Impossible\n");return 0;}printf("%d = ",n);for (int i = 0; i < ans.size(); i++) {if (i != 0)printf(" + ");printf("%d^%d", ans[i], p);}return 0;
}

转载于:https://www.cnblogs.com/seasonal/p/10343608.html

PAT1103 Integer Factorization (30)(DFS:回溯)相关推荐

  1. PAT甲级1103 Integer Factorization (30 分):[C++题解]背包问题,DP解法

    文章目录 题目分析 题目链接 题目分析 分析 把N(样例中N=169)看成背包的体积:把k(样例中k=5)看成背包能承的重量.把这道题转化为二维完全背包问题.由于数据范围给出的次幂P∈[2,7],那么 ...

  2. PAT甲级 -- 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 positi ...

  3. 1103. Integer Factorization (30)

    时间限制 1200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The K-P factorization of a posi ...

  4. 1103 Integer Factorization (30 分)【难度: 中 / 爆搜】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 爆搜的做法,动态规划也可以做,有时间也一个动 ...

  5. 1103 Integer Factorization (30分)

    第一个深度优先搜索的题目. 很显然这个就是分情况讨论,每次选和不选某值,最终将满足结果的序列筛选出来,我自己写的一个dfs遍历,我比较喜欢对每一个元素进行遍历,用循环控制,这样的好处是避免递归层数太多 ...

  6. PAT1087 All Roads Lead to Rome (30)(最短路径+dfs+回溯)

    题意: 有N个城市,M条无向边,从某个给定的起始城市出发,前往名为ROM的城市.每个城市(除了起始城市)都有一个点权(称为幸福值),和边权(每条边所需的花费).求从起点到ROM所需要的最少花费,并输出 ...

  7. 【DFS+回溯+字符串】【洛谷P1019】【单词接龙】

    传送门 /*[DFS+回溯+字符串][洛谷P1019][单词接龙]https://www.luogu.com.cn/problem/P1019题意:给你很多个单词 一个开头首字母 每个单词至多用2次 ...

  8. DFS+回溯算法专题

    基础知识 回溯法是一种选优搜索法(试探法),被称为通用的解题方法,这种方法适用于解一些组合数相当大的问题.通过剪枝(约束+限界)可以大幅减少解决问题的计算量(搜索量). 深度优先搜索(Depth-Fi ...

  9. Leetcode一起攻克搜索(BFS,DFS,回溯,并查集)

    文章目录 BFS简介 DFS简介 回溯简介 并查集简介 DFS题目 690. 员工的重要性 1.dfs解法: 2.bfs算法 547.朋友圈 dfs解法 200.岛屿数量 dfs解法 417.太平洋大 ...

最新文章

  1. socket的拉屎模型
  2. Flex和Bison简介和Windows下使用入门
  3. poj 1077 eight
  4. [codevs 1914] 运输问题
  5. 【Redis系列】深入浅出Redis主从复制之哨兵模式【实践】
  6. C++ 常见崩溃问题分析
  7. Sail(CodeForces - 298B )
  8. python list sort by,python中List的sort方法指南
  9. 百度启用Baidu.co.jp域名,有利于其在日本推广
  10. Spark源码系列(五)分布式缓存
  11. 学写压缩壳心得系列之一 熟悉概念,未雨绸缪
  12. dialogfield
  13. LINUX安装7Zip
  14. ActivityGroup 例子
  15. 微信java版s40_塞班微信S40版下载
  16. python与爬虫-02复杂的HTML解析
  17. 【那些年做过的设计 · 纪念贴】UI设计中移动端和PC端之间具体有什么区别
  18. 电商网站适合用什么服务器?
  19. 阿里云上一键安装lnmp或lamp
  20. 严重 [RMI TCP Connection(3)-127.0.0.1]

热门文章

  1. HttpRuntime.Cache的使用经验
  2. python之多并发socket
  3. 选择题:JAVA的类和对象
  4. Elasticsearch安装及自动同步mysql数据库数据
  5. FPGA机器学习之学习的方向
  6. 让我的网站变成响应式的3个简单步骤
  7. 函数hook注意事项
  8. SQL 审核系统体验
  9. B+树索引和哈希索引
  10. golang结构体tag的使用