1103 Integer Factorization
题意:给出一个正整数N,以及k,p,要求把N分解成k个因式,即N=n1^p + n2^p + ... + nk^p。要求n1,n2,...按降序排列,若有多个解,则选择n1+n2+...+nk最大的,若还有多个解,则选择数字序列较大的。若不存在解,则输出Impossible.
思路:这是深度优先搜索的经典例题。详见:
代码:
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
using namespace std;
;
int factor[maxn];
;
int n,k,p;
void init()
{
;
while(temp<=n) {
factor[len++]=temp;
temp=pow(len,p);
}
}
;
vector<int> ans,tmp;
bool flag=false;
void dfs(int idx,int currK,int currN,int currFacSum)
{
if(currK==k && currN==n){
flag=true;
if(currFacSum>maxFacSum){
maxFacSum=currFacSum;
ans=tmp;
}
return;
}
if(idx==0 || currK>k || currN>n) return;//第一个条件不能漏!
tmp.push_back(idx);
dfs(idx,currK+,currN+factor[idx],currFacSum+idx);
tmp.pop_back();
dfs(idx-,currK,currN,currFacSum);
}
int main()
{
scanf("%d%d%d",&n,&k,&p);
init();//预处理
dfs(len-,,,);
if(flag){
printf(],p);
;i<ans.size();i++)
printf(" + %d^%d",ans[i],p);
}else{
printf("Impossible\n");
}
;
}
1103 Integer Factorization的更多相关文章
- PAT 1103 Integer Factorization[难]
1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...
- PAT甲级1103. Integer Factorization
PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...
- PAT甲级——1103 Integer Factorization (DFS)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...
- 1103 Integer Factorization (30)
1103 Integer Factorization (30 分) The K−P factorization of a positive integer N is to write N as t ...
- 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】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 ...
- 1103 Integer Factorization (30)(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 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 ...
- PAT (Advanced Level) 1103. Integer Factorization (30)
暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲级1103 Integer Factorization【dfs】【剪枝】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 题意: 给定一个数n,要求从1~n中找 ...
随机推荐
- @Transactional的readOnly、timeout
1.@Transactional的readOnly 在使用@Transactional注解的时候,有一个属性是readOnly,默认值是false readOnly的意思就是当前的方法是只读的,也就是 ...
- PHP工作笔记:遍历文件夹返回文件数组
直接输入文件夹的路径,调用函数即可返回文件夹里面的文件数组,不返回文件夹 <?php function scanfiles($folder){ $folder = $folder."* ...
- 【dlbook】正则化
对学习算法的修改——旨在减少泛化误差而不是训练误差 显著减少方差而不过度增加偏差. [参数范数惩罚] 通常只对权重做惩罚而不对偏置做惩罚,原因是拟合偏置比拟合权重容易很多. 不同层使用不同惩罚的代价很 ...
- Android6.0之后的权限机制对App开发的影响
随着Android系统的更新换代,每次重大更新的方面也逐步扩展,从4.*主要是增强功能,到5.*主要是美化界面,到6.*主要提高系统安全性,再到7.*和8.*主要支撑各种大屏设备,因此开发者需要对每个 ...
- Vim技能修炼教程(3) - 语法高亮进阶
语法高亮进阶 首先我们复习一下上节学到的三个命令: * syntax match用于定义正则表达式和规则的对应 * highlight default定义配色方案 * highlight link将正 ...
- objc/runtime.h 相关
Objecitve-C的重要特性是Runtime(运行时),在Interacting with the Runtime(交互运行)中,运行时函数部分,苹果给出了/usr/lib/libobjc.A.d ...
- Android常见问题——Genymotion无法启动问题
在官网下载了Genymotion和VirturalBox的合集安装之后启动模拟器的时候发现启动不了(默认下载,啥都没干),在网上找了一些方法,也没有解决,最后偶然看到一种方法才解决的,先看一下具体的问 ...
- JAXP使用Stax API时格式化输出XML 2
之前实现的一个版本:http://www.cnblogs.com/lyhtbc/p/jaxp-pretty-format-validate-validation-stax-stax2.html 这个版 ...
- lx 与cd 的计算方法
Candela to lux calculation with distance in meters The illuminance Ev in lux (lx) is equal to the lu ...
- python 除法