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. Pull解析-解析xml文件

    首先需要导入jar包:kxml2-2.2.2.jar 例程: main: /** * pull解析 * * @author my * */ public class DemoParserStudent ...

  2. Objective-C 高级编程:iOS与OS X多线程和内存管理

    <Objective-C 高级编程:iOS与OS X多线程和内存管理> 基本信息 原书名: Pro Multithreading and Memory Management for iOS ...

  3. jquery插件开发规范

    一.请给你的代码加上注释 这个世界不存在百分百的完美的jquery插件,注释不止是给别人看.更重要的是给自己看. 你应该把注释当做你代码的一部分,养成随手加注释的习惯,尤其是编写javascript的 ...

  4. linux yum install resource - epel

    首先现在如下rpm包,然后安装对应的rpm包centos5 32位epel源下载地址: www.lishiming.net/data/attachment/forum/epel-release-5-4 ...

  5. 统计机器学习(statistical machine learning)

    组要组成部分:监督学习(supervised learning),非监督学习(unsupervised learning),半监督学习(semi-supervised learning),强化学习(r ...

  6. Python之类型转换

    函数 描述 int(x [,base]) 将x转换为一个整数 long(x [,base] ) 将x转换为一个长整数 float(x) 将x转换到一个浮点数 complex(real [,imag]) ...

  7. html+css-水平居中-不定款块状元素方法(二)

    来源:http://www.imooc.com/code/6364 除了上一节讲到的插入table标签,可以使不定宽块状元素水平居中之外,本节介绍第2种实现这种效果的方法,改变元素的display类型 ...

  8. AjaxPro.2使用小结

    这是我最近没事的时候研究的东东,使用AjaxPro.2.dll,从前台调用后台Ajax方法,希望对各位亲有帮助哦.. 1.首先将AjaxPro.2.dll从网上下载下来,打开VS项目,点击项目,右键- ...

  9. AsyncTask实现的原理和适用的优缺点

    AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口实现UI进度更新),最后反馈执行的结果给UI ...

  10. wamp图标黄色的另一种可能原因

    学习php用的是wamp即windows+apache+mysql+php,但是当启动的时候,图标为黄色(正常应为绿色),开始以为是端口的问题,但是将端口改掉依旧如此,后来发现是机器装的sqlserv ...