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中找 ...
随机推荐
- 201621123005《Java程序设计》第三周作业学习总结
201621123005<Java程序设计>第三周 学习总结 标签(空格分隔): 未分类 1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化 ...
- 解决chrome报Not allowed to load local resource错误的方法
最近项目中遇到了关于图片的更改->保存->本地读取 在本地读取的环节上面出现了错误,一开始用的是直接本地路径,但是在页面上调试的出现了下面的错误,他的路径还是相对路径,下图所示: Goog ...
- 20165210 《网络对抗技术》week1 exp0 kali安装与配置
20165210 <网络对抗技术>week1 exp0 kali安装与配置 1. 安装过程: 从kali官网上下载如下图所示: 下载完成后打开VMware 点击创建新的虚拟机 弹出新虚拟机 ...
- Linux输入输出管理
一.系统输入输出的理解 运行一个程序时,需要从某个位置读取输入信息,然后CPU处理,最后将输出 显示在屏幕或文件中:其中,某个位置相当于输入设备,屏幕或文件为输出设备. 标准输入:stdin,默认 ...
- Confluence 安装
一.事前准备 1.jdk安装:5.8.10的jdk至少是7,其中7中还有很多官网是不建议的,这儿选中jdk-7u79 二.安装Confluence 双击atlassian-confluence-5.8 ...
- c# html内容处理类
using System; using System.Text; using System.Text.RegularExpressions; using System.Net; using Syste ...
- SQL 测验
1.SQL 指的是? 您的回答:Structured Query Language 2.哪个 SQL 语句用于从数据库中提取数据? 您的回答:SELECT 3.哪条 SQL 语句用于更新数据库中的数据 ...
- sql server不能删除数据库,显示错误:正在使用
解决办法: use mastergoalter database database_name set single_user with rollback immediate --将数据库回滚到原始配置 ...
- caffe学习4——net
参考文献 1 The net jointly defines a function and its gradient by composition and auto-differentiation. ...
- (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
if (this == null) Console.WriteLine("this is null"); 这句话一写,大家一定觉得荒谬,然而 if 内代码的执行却是可能的!本文讲介 ...