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中找 ...
随机推荐
- CUDA库函数module management
http://horacio9573.no-ip.org/cuda/group__CUDA__MODULE_ga52be009b0d4045811b30c965e1cb2cf.html
- macOS 下安装SDKMAN 软件开发工具包管理器
SDKMAN 软件开发工具包管理器的安装非常简单,只需要打开终端,执行: $ curl -s "https://get.sdkman.io" | bash 就OK了,输出类似如下: ...
- softmax的多分类
关于多分类 我们常见的逻辑回归.SVM等常用于解决二分类问题,对于多分类问题,比如识别手写数字,它就需要10个分类,同样也可以用逻辑回归或SVM,只是需要多个二分类来组成多分类,但这里讨论另外一种方式 ...
- python 多维list声明时的小问题
a=[[]]*3 a Out[18]: [[], [], []] a[0].append(1) a Out[20]: [[1], [1], [1]] b=[[] for _ in range(3)] ...
- Linux usleep for shell
/**************************************************************************** * Linux usleep for she ...
- pandas.read_csv()参数(转载)
文章转载地址 pandas.read_csv参数整理 读取CSV(逗号分割)文件到DataFrame 也支持文件的部分导入和选择迭代 更多帮助参见:http://pandas.pydata.org/p ...
- hdu1575 Tr A 矩阵初识
A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n <= ...
- SmartSql 动态仓储
动态代理仓储 SmartSql源码:https://github.com/Ahoo-Wang/SmartSql 简介 动态代理仓储(SmartSql.DyRepository)组件是SmartSql非 ...
- 在linux安装redis单机和集群后,如何在windows上使用redis客户端或者java代码访问错误的原因很简单,就是没有连接上redis服务,由于redis采用的安全策略,默认会只准许本地访问。需要通过简单配置,完成允许外网访问。
这几天在学习在linux上搭建服务器的工作,可谓历经艰辛.可喜最后收获也不少. 这次是在linux上搭建redis服务器后从windows上缺无法访问,连接不上. 仔细回忆以前搭建nginx和ftp的 ...
- python字符串常用
参考这一篇: http://www.runoob.com/python/python-strings.html