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. Linux 网络设备驱动程序设计(2)

    二.回环网卡的程序设计 /*************************** *******回环网卡的驱动程序*** ***********吕晓宁*********** *********2015 ...

  2. 【CSS3】---为边框应用图片 border-image

    为边框应用图片 border-image 顾名思义就是为边框应用背景图片,它和我们常用的background属性比较相似.例如: background:url(xx.jpg) 10px 20px no ...

  3. Java开发从零开始填坑

    开始学习Java,感觉较.NET知识更零碎一些,所以开个帖子把自己踩过的坑记录下来,都是边边角角网上不容易找到的东西. 1.java命令格式:>cd %parent-of-pakadgePath ...

  4. JQuery Mobile + Cordova 实战一

    好的,今天给大家继续讲解 JQM 和 Cordova 的结合吧.Cordova 和 Phonegap 反正是一个东西,大家就当做一个是旧版(Phonegap)的一个是新版(Cordova)的就行.不同 ...

  5. 关于asp.net MVC 中的TryUpdateModel方法

    有比较才会有收货,有需求才会发现更多. 在传统的WebFormk开发工作中,我们常常会存在如下的代码块 //保存 protected void btnSubmit_Click(object sende ...

  6. android 底部tabview模板

    以下只是我个人的浅见,大神请忽略~ 底部tab的实现如下图: 现在实现这种界面是有很多方法的,但是对于android新手来说,实现这样的界面还是有难度的. 在网上找到了一个别人写好的底部tab模板,修 ...

  7. Python 网页爬虫

    解决问题:获取网页上的内容.特别是加载主框架后,再用AJAX获取数据生成内容的网页. PyQuery:可以像jQuery的py实现.你给他一个PyQuery一个HTML,他给你一个类似jQuery的操 ...

  8. webkit常见问题汇总

    前段时间有人问我一个简单的问题,html如何创建解析的? 我讲了一大堆,什么通过DocumentLoader, CachedResourceLoader, CacheResource, Resourc ...

  9. Exchange 2013 基本部署独立与非独立

    Exchange 2013 基本部署独立与非独立 转载请注明原出处 From yang 先决条件 Active Directory需要准备的,安装Microsoft .NET Framework 4. ...

  10. in/exists not in/not exists null

    in/not in exists/not exists null的理解 两个测试表 create table tmp01 as with tmp as ( select '1' as id from ...