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<stdio.h>
#include<string>
#include<iostream>
#include<string.h>
#include<sstream>
#include<vector>
#include<map>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<set>
using namespace std; int k,p;
int MAX = -;
vector<int> re;
void DFS(vector<int>& vv,int n)
{
if(vv.size() == k )
{
if(n == )
{
int sum = ;
for(int i = ;i < k;++i)
sum += vv[i];
if(sum >= MAX) // 需要等号,可使得 sequence { a1, a2, ... aK } is said to be larger than { b1, b2, ... bK } i
{
MAX = sum;
re = vv;
}
}
vv.pop_back();
return;
}
int low = vv.size() == ? : vv[vv.size() -];//剪枝 使得只有增序情况
int m = sqrt(double(n));
for(int i = low ; i <= m;++i)
{
int tmp = pow(double(i),p);
if(n >= tmp)
{
vv.push_back(i);
DFS(vv,n-tmp);
}else break;
}
if(!vv.empty())
vv.pop_back();
} int main()
{
int n;
scanf("%d%d%d",&n,&k,&p);
vector<int> vv;
DFS(vv, n);
if(re.empty())
{
printf("Impossible\n");
}
else
{
printf("%d = %d^%d",n,re[re.size()-],p);
for(int i = re.size() - ;i >= ;--i)
{
printf(" + %d^%d",re[i],p);
}
printf("\n");
}
return ;
}

1103. Integer Factorization (30)的更多相关文章

  1. 1103 Integer Factorization (30)

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

  2. 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 ...

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

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

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

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

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

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

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

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

  7. PAT 1103 Integer Factorization[难]

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

  8. PAT甲级1103. Integer Factorization

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

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

随机推荐

  1. 编译代码报出Android library projects cannot be launched错误的解决

    Android library projects cannot be launched错误的解决方法: 右键工程根目录->properties 左侧选择->android

  2. Scrapy使用心得

    今天安装了大名鼎鼎的Scrapy,不过碰到了不少问题,包括"Unable to find vcvarsall.bat" 这个错误应该很常见的,用了最简单的解决方法: 安装visua ...

  3. 两种局部刷新UITableView的方法的使用条件

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //1.取消选 ...

  4. Angularjs 使用filter格式化输出href

    工作中,由于是多级菜单,如果上级菜单为空,就会访问Angularjs 默认的state,然后再展开菜单,我找资料之后,才知道是通过filter来格式化输出数据的,格式是{{ expression | ...

  5. sql获取exec('')的返回值

    ) ) select @sql=('select @a=cNumber+1 from VoucherHistory where CardNumber='''+@CardNumber+'''') exe ...

  6. 包装BufferedReader的readLine()输出行号

    定义一个类,实现与被增强对象相同的接口,或继承这个类,视情况而定 定义一个变量,记住被增强的对象 定义一个构造函数,接收被增强的对象 覆盖要增强的方法 对于不需要增强的方法,调用被增强对象原有的方法 ...

  7. SVN Server导项目到本地库时提示"方法OPTIONS失败与无法连接到服务器"

    方法 OPTIONS 失败于 “https://xxxx/svn/xxxx”: 无法连接到服务器 (https://xxxx) 要留意  https 使用了443 端口,检查防火墙是否开放了该端口. ...

  8. HTML5_用语义化标记重新定义博客

    HTML5文档的第一行便是文档类型声明,文档类型声明的作用有两个 一:验证器依据文档类型来判断采用何种验证规则去验证代码 二:文档类型声明能够强制IE6,IE7,IE8以“标准模式”渲染页面 1: & ...

  9. [转]美国的软件公司是什么样?---- 以Fog Creek为例

    作者: 阮一峰 日期: 2009年2月15日 Joel写过一篇文章,关于软件公司如何雇到优秀的程序员. 他说,软件公司的头等大事,就是找到第一流的人才.不同于其他行业,软件是一种纯智力产品,你有什么样 ...

  10. HDU4268 Alice and Bob(贪心+multiset)

    Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this game, ...