The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K-P factorization of N for any positive integers N, K and P.

Input Specification:

Each input file contains one test case which gives in a line the three positive integers N (<=400), K (<=N) and P (1<P<=7). The numbers in a line are separated by a space.

Output Specification:

For each case, if the solution exists, output in the format:

N = n1^P + ... nK^P

where ni (i=1, ... K) is the i-th factor. All the factors must be printed in non-increasing order.

Note: the solution may not be unique. For example, the 5-2 factorization of 169 has 9 solutions, such as 122 + 42 + 22 + 22 + 12, or 112 + 62 + 22 + 22 + 22, or more. You must output the one with the maximum sum of the factors. If there is a tie, the largest factor sequence must be chosen -- sequence { a1, a2, ... aK } is said to be larger than { b1, b2, ... bK } if there exists 1<=L<=K such that ai=bi for i<L and aL>bL

If there is no solution, simple output "Impossible".

Sample Input 1:

169 5 2

Sample Output 1:

169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2

Sample Input 2:

169 167 3

Sample Output 2:

Impossible

分析

这道题用深度优先搜索方法来实现。

#include<iostream> //深度优先搜索
#include<vector>
#include<math.h>
using namespace std;
int N, P, K, maxsum=-1;
vector<int> v, ans, tempans;
void init(){
int temp=0, index=1;
while(temp<=N){
v.push_back(temp);
temp=pow(index, P);
index++;
}
}
void dfs(int index, int tempsum, int tempk, int facsum){
if(tempsum==N&&tempk==K){
if(facsum>maxsum){
ans=tempans;
maxsum=facsum;
}
return ;
}
if(tempsum>N||tempk>K) return ;
for(int i=index; i>=1; i--){
tempans.push_back(i);
dfs(i, tempsum+v[i], tempk+1, facsum+i);
tempans.pop_back();
}
}
int main(){
cin>>N>>K>>P;
init();
dfs(v.size()-1, 0, 0, 0);
if(maxsum==-1){
cout<<"Impossible"<<endl;
return 0;
}
cout<<N<<" = "<<ans[0]<<"^"<<P;
for(int i=1; i<ans.size(); i++)
cout<<" + "<<ans[i]<<"^"<<P;
return 0;
}

PAT 1103 Integer Factorization的更多相关文章

  1. PAT 1103 Integer Factorization[难]

    1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...

  2. PAT甲级1103. Integer Factorization

    PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...

  3. PAT甲级——1103 Integer Factorization (DFS)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...

  4. 1103 Integer Factorization (30)

    1103 Integer Factorization (30 分)   The K−P factorization of a positive integer N is to write N as t ...

  5. 【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 ...

  6. PAT (Advanced Level) 1103. Integer Factorization (30)

    暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  7. PAT甲级1103 Integer Factorization【dfs】【剪枝】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 题意: 给定一个数n,要求从1~n中找 ...

  8. PAT甲题题解-1103. Integer Factorization (30)-(dfs)

    该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + … nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. ...

  9. 【PAT甲级】1103 Integer Factorization (30 分)

    题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...

随机推荐

  1. 并发与并行(concurrency vs parallesim)

    最近对计算机中并发(concurrency)和并行(parallesim)这两个词的区别很迷惑,将搜索到的相关内容整理如下. http://www.vaikan.com/docs/Concurrenc ...

  2. 使用EL表达式正确情况下报错:javax.servlet.jsp cannot be resolved to a type

    这个错误可能是服务器自带的servlet库未导入的原因.右键项目属性,转到Targeted Runtimes,选择一个服务器,例如Tomcat,单击应用,可能就可以解决.

  3. bzoj1997 [Hnoi2010]Planar——2-SAT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1997 神奇的经典2-SAT问题! 对于两个相交的区间,只能一里一外连边,所以可以进行2-SA ...

  4. PCB OD工具破解实例应用

    以下破解Genesis为例,对OD工具使用进行实例讲解 工具简单 介绍下下载地址: OD工具:是一个新的动态追踪工具,将IDA与SoftICE结合起来的思想,Ring 3级调试器, 是为当今最为流行的 ...

  5. [Swift通天遁地]二、表格表单-(2)创建右侧带有索引的UITableView(表单视图)

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. POJ 1584 计算几何

    思路: 求一遍凸包 用三角形面积(叉积求一下)/边长 求出来高,跟半径比一比 坑点:凸包上三点共线 //By SiriusRen #include <cmath> #include < ...

  7. mariadb的安装

    mysql (分支 mariadb)1.安装mariadb -yum -源码编译安装 -下载rpm安装 yum和源码编译安装的区别? 1.路径区别-yum安装的软件是他自定义的,源码安装的软件./co ...

  8. Select2插件ajax方式加载数据并刷新页面数据回显

    今天在优化项目当中,有个要在下拉框中搜索数据的需求:最后选择使用selec2进行开发: 官网:http://select2.github.io/ 演示: 准备工作: 文件需要引入select2.ful ...

  9. PHP MySQL 连接数据库,进行增、删、改、查、操作

    <table width="100%" border="1" cellpadding="0" cellspacing="0& ...

  10. c#——值类型与引用类型

    值类型传的是值 引用类型传的是地址