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. c++ 的几种强制转换的讨论

    http://www.cnblogs.com/jerry19880126/archive/2012/08/14/2638192.html static_cast  : 同 c 的强制转换类似: rei ...

  2. spring mvc 重新定向到一个新的Url

    在写项目的时候要求根据请求的参数的不同重新将请求分发,在查阅了spring mvc的一些资料无果后(想使用拦截器去做)就没办法使用重定向的方式去写了 /** * 通过访问API的方式分发请求 * * ...

  3. 01. 把存储过程结果集SELECT INTO到临时表

    在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种. 一. SELECT INTO 1. 使用select into会自动生成临时表,不需要事先创建 select * into #tem ...

  4. LearnMVC5-AddController

    原创文章,转载必需注明出处:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/learnmvc5-addcontroller/ 本人是 ...

  5. apache2.4的安装与卸载

    安装sudo apt-get install apache2,这不是源码安装的方式,产生的apache地址在/etc/apache2,配置文件是apache2.conf如果浏览器输入127.0.0.1 ...

  6. Umbraco(5)-Creating Master Template Part 1(翻译文档)

    原文地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/umbraco5-creating-master-template-par ...

  7. Ajax上传文件进度条显示

    要实现进度条的显示,就要知道两个参数,上传的大小和总文件的大小 html5提供了一个上传过程事件,在上传过程中不断触发,然后用已上传的大 小/总大小,计算上传的百分比,然后用这个百分比控制div框的显 ...

  8. What Makes a Good Programmer Good?

    I’ve worked with a lot of programmers over the years — some of them super amazing, and some distinct ...

  9. 二十二、android中application标签说明

    <application> <applicationandroid:allowClearUserData=["true" | "false"] ...

  10. iOS开发--图片处理

    纵观现实社会和移动app市场,这是一个看脸的时代,而好看且漂亮的APP界面就是移动APP的脸.漂亮的外观后面少不了UI设计人员的辛苦,如果不懂的处理,就浪费了UI设计人员的心血. 比如下面这张图片,是 ...