题目https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224

题意:

给定一个数n,要求从1~n中找出k个数,使得这些数的p次方之和等于n

思路:

因为n为400,所以dfs加剪枝【本来还在想dp来着】

他要求输出的方案是数字之和最大的,如果之和相等要输出字典序较大的。

所以还需维护一个数字之和。

一个剪枝的方法是从大到小进行dfs,后面被选的数一定比前面被选的要小,这里不限制的话显然会出现重复。

如果从大到小进行dfs的话,tmpsum=anssum的时候,后面的方案一定不如前面的方案,所以这里不取等号

 #include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int n, k, p;
vector<int>ans, tmp;
int anssum = , tmpsum = ; int ppow[]; void init()
{
for(int i = ; i <= n; i++){
ppow[i] = pow(i, p);
}
} void dfs(int i, int num, int sum)
{
if(num == ){
if(sum == && tmpsum > anssum){
anssum = tmpsum;
ans = tmp;
}
return;
}
if(sum < )return; for(int j = i; j >= ; j--){
tmp.push_back(j);
tmpsum += j;
dfs(j, num - , sum - ppow[j]);
tmp.pop_back();
tmpsum -= j;
}
} int main()
{
scanf("%d%d%d", &n, &k, &p);
init();
dfs(n, k, n);
//printf("%d\n", ans.size());
if(ans.size() != k){
printf("Impossible\n");
}
else{ printf("%d = %d^%d", n, ans[], p);
for(int i = ; i < k; i++){
printf(" + %d^%d", ans[i], p);
}
printf("\n");
}
return ;
}

PAT甲级1103 Integer Factorization【dfs】【剪枝】的更多相关文章

  1. PAT甲级——1103 Integer Factorization (DFS)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...

  2. PAT甲级1103. Integer Factorization

    PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...

  3. 【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 ...

  4. PAT甲级——A1103 Integer Factorization

    The K−P factorization of a positive integer N is to write N as the sum of the P-th power of Kpositiv ...

  5. PAT 1103 Integer Factorization[难]

    1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...

  6. 1103 Integer Factorization (30)

    1103 Integer Factorization (30 分)   The K−P factorization of a positive integer N is to write N as t ...

  7. PAT甲题题解-1103. Integer Factorization (30)-(dfs)

    该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + … nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. ...

  8. 【PAT甲级】1103 Integer Factorization (30 分)

    题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...

  9. PAT 1103 Integer Factorization

    The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

随机推荐

  1. [面试] mysql 面试题

    最近在准备面试,mysql 实在是不熟悉,就先摘录一些网上的面试题来看一下. 1. MyISAM 和 InnoDB 区别? InnoDB 支持事务处理,支持更大的并发update 和 insert 操 ...

  2. Vue项目中使用基于Vue.js的移动组件库cube-ui

    cube-ui 是滴滴公司的技术团队基于 Vue.js 实现的精致移动端组件库.很赞,基本场景是够用了,感谢开源!感谢默默奉献的你们. 刚爬完坑,就来总结啦!!希望对需要的朋友有小小的帮助. (一)创 ...

  3. LOJ #2116 Luogu P3241「HNOI2015」开店

    好久没写数据结构了 来补一发 果然写的时候思路极其混乱.... LOJ #2116 Luogu P3241 题意 $ Q$次询问,求树上点的颜色在$ [L,R]$中的所有点到询问点的距离 强制在线 询 ...

  4. codeblocks1712设置中文

    下载汉化包:百度网盘,密码:7rrz 下载后放到安装目录:D:\Program Files (x86)\CodeBlocks\share\CodeBlocks\locale\zh_CN(根据个人安装目 ...

  5. 移动端调用电话、短信、唤起QQ和使用百度地图

    H5能很方便地实现这些功能,都是一句代码搞定 调用电话 <a href="tel:12345678"> 短信 <a href='sms:12345678'> ...

  6. NB-IoT省电模式:PSM、DRX、eDRX【转】

    转自:https://blog.csdn.net/zoomdy/article/details/80447372 NB-IoT支持三种省电模式:PSM (Power Saving Mode,省电模式) ...

  7. 转:对UI自动化测试的一些感悟

    不断发掘自动化测试对各个团队的附加价值,这样才能得到来自四面八方的支持,没有将自动化加入项目过程的自动化都达不到预期的效果. UI自动化框架 把UI自动化框架设计成一个拼图性质的架构.把每个特性都设计 ...

  8. 【原创】Linux基础之curl

    http请求过程如下: # curl -v http://www.baidu.com % Total % Received % Xferd Average Speed Time Time Time C ...

  9. python正则表达式--编译正则表达式re.compile

    编译正则表达式-- re.compile 使用re的一般步骤是先将正则表达式的字符串形 式编译为pattern实例,然后使用pattern实例处理文本并获取匹配结果(一个Match实例(值为True) ...

  10. thymeleaf的onclick标签传参异常

    转自https://my.oschina.net/u/2312080/blog/2878183 异常 严重: Servlet.service() for servlet [DispatcherServ ...