题目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. js中escape对应的C#解码函数 UrlDecode

    js中escape对应的C#解码函数 System.Web.HttpUtility.UrlDecode(s),使用过程中有以下几点需要注意   js中escape对应的C#解码函数 System.We ...

  2. spring和mybatis的整合开发(基于MapperFactoryBean的整合开发(方便简单不复杂))

    MapperFactoryBean是mybati-spring团队提供的一个用于根据mapper接口生成mapper对象的类. 在spring配置文件中可以配置以下参数: 1.mapperInterf ...

  3. MySQL学习8 - 数据的增删改

    一.介绍 二.插入数据 INSERT 三.更新数据UPDATE 四.删除数据DELETE 一.介绍 在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 使用INSERT实现 ...

  4. css实现移动端水平滚动导航

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. L1-Day4

    L1-Day4 1.这消息使她非常悲伤. [我的翻译]The message makes she very sad. [标准答案]The news made her very sad. [对比分析]( ...

  6. Beta冲刺(1/7)

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:beta冲刺(1/7) 团队部分 后敬甲(组长) 过去两天完成了哪些任务 团队完成测试答辩 整理博客 复习接口 接下来的 ...

  7. day3-->深浅拷贝

    import copy #浅拷贝 #copy.copy() #深拷贝 #copy.deepcopy() #赋值 #a = '123' #b = a a1 = 123123 a2 = 123123 #查 ...

  8. AI数据分析(一)

    安装Spyder+PyQt5 在python36目录下,使用cmd打开,切换到Scripts文件下 pip install spyder pip install PyQt5 python中的库 Num ...

  9. 继承了AppCompatActivity的全屏设置

    v7下全屏设置:getSupportActionBar().hide();getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ...

  10. c&c++中的宏

    1 c&c++中的宏 do {...} while (0); offsetof & container_of 2 引用 [1] do {...} while (0) 在宏定义中的作用 ...