PAT甲级1103 Integer Factorization【dfs】【剪枝】
题目: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】【剪枝】的更多相关文章
- PAT甲级——1103 Integer Factorization (DFS)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...
- PAT甲级1103. Integer Factorization
PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...
- 【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 ...
- 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 ...
- PAT 1103 Integer Factorization[难]
1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...
- 1103 Integer Factorization (30)
1103 Integer Factorization (30 分) The K−P factorization of a positive integer N is to write N as t ...
- PAT甲题题解-1103. Integer Factorization (30)-(dfs)
该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + … nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. ...
- 【PAT甲级】1103 Integer Factorization (30 分)
题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...
- 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 ...
随机推荐
- was类加载器
来自:http://liuwei1578.blog.163.com/blog/static/49580364200991572642653/ Jar包冲突问题是在大型Java软件开发中经常遇到的问题, ...
- 程序设计-理解java继承-遁地龙卷风
(0)写在前面 编程和现实世界是息息相关的,你是如何理解现实世界中的继承呢? 继承在编程中应理解为:对父类资源(本文只讨论方法)的使用,父类方法只提供类基本的功能,父类方法之间不存在调用关系. (1) ...
- java web添加mysql过程中遇到的错误及解决办法
问题一:遇到提示找不到驱动 com.mysql.jdbc.Driver 起初项目中是导入了mysql-connector-java-5.1.45-bin.jar 包的,但是一直依然报错,最后去官网 ...
- 【原创】大叔经验分享(10)Could not transfer artifact org.apache.maven:maven. from/to central. Received fatal alert: protocol_version
maven编译工程报错 [ERROR] Failed to execute goal net.alchim31.maven:scala-maven-plugin:3.2.2:add-source (s ...
- Token令牌管理权限
什么是token HTTP是一种无状态的协议,也就是HTTP没法保存客户端的信息,没办法区分每次请求的不同. Token是服务器生成的一串字符,作为客户端请求的令牌.当第一次登陆后,服务器会分发Ton ...
- Google Android SDK开发范例大全笔记 二
网络设备管理相关相关 代码地址 1 WifiManager LocationManager分别控制 wifi及GPS WifiManager 判断网络状态 ,LocationManager判断定位状态 ...
- 商品规格笛卡尔积PHP
<?php $color = array('red', 'green'); $size = array(39, 40, 41); $local = array('beijing', 'shang ...
- Flask上下文管理源码--亲自解析一下
前戏 偏函数 def index(a,b): return a+b # 原来的调用方法 # ret=index(1,2) # print(ret) # 偏函数--帮助开发者自动传递参数 import ...
- linux关于软件安装的博文
https://www.cnblogs.com/kundeg/archive/2018/03/06/7247934.html https://www.cnblogs.com/qiaozhoulin/p ...
- 剑指offer数组列表
一.数组 面试题3 : 找出数组中重复的数字 面试题3(二):不修改数组找出重复的数字 面试题4:二维数组的查找 面试题21:调整数组顺序使奇数位于偶数前面 面试题39:数组中出现次数超过一半的数字 ...