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中找 ...
随机推荐
- L146 Space Station Hole Cause Will Be Determined
The head of the U.S. space agency said Tuesday he's sure that investigators will determine the cause ...
- 数据库连接错误:CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
学习Spring整合Hibernate的知识,新建一个工程,代码结构如下: 按如下步骤整合: 代码如下: hibernate.cfg.xml: <?xml version="1.0&q ...
- java事务(一)
Java中事务处理的基本方法与原理,包含以下文章: (一)Java事务处理的基本问题 (二)失败的案例 (三)丑陋的案例 (四)成功的案例(自己实现一个线程安全的TransactionManager) ...
- java.sql.SQLException: Unsupported character encoding 'utf8mb4'.
四月 12, 2017 3:47:52 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() fo ...
- [Python] 跳过前几行快速读取文件内容:islice
from itertools import islice start = 1 # 跳过第一行idx=0,从idx=1开始读取文件 with codecs.open('data.json', encod ...
- 如何将局域网中的 windows 硬盘挂载到 linux 系统中
1.共享windows上的E盘 2.linux上执行 mount //192.168.3.181/e /tmp/test -o username=dell,,password=abcdef 3.保证两 ...
- Linux服务器运行环境搭建(三)——MySQL数据库安装
官网:http://www.mysql.com/ 官网下载地址:http://dev.mysql.com/downloads/mysql/ 说明:官网下载页面的“Select Platform” 选择 ...
- UDP示例
android学习笔记18--------------UDP示例 分类: android2011-11-10 10:07 848人阅读 评论(0) 收藏 举报 androidbufferexcepti ...
- C# 后台模拟前台post发送json数据
public static string PostMoths(string url, string param) { string strURL = url; System.Net.HttpWebRe ...
- C++中atof函数的实现和atoi的实现
在C++中有两个系统函数可以实现字符串转浮点型和字符串转整形,下面实现一下这两个函数. #include <iostream> #include <string> using ...