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 = n[1]^P + ... n[K]^P

where n[i] (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 12​2​​+4​2​​+2​2​​+2​2​​+1​2​​, or 11​2​​+6​2​​+2​2​​+2​2​​+2​2​​, 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 { a​1​​,a​2​​,⋯,a​K​​ } is said to be larger than { b​1​​,b​2​​,⋯,b​K​​ } if there exists 1≤L≤K such that a​i​​=b​i​​ for i<L and a​L​​>b​L​​.

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 <algorithm>
#include <set>
#include <string.h>
#include <vector>
#include <math.h>
#include <queue>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
const int maxn = ;
int n,p,k,maxk=-;
int vis[maxn]={};
vector<int> res,tmp;
void dfs(int index,int ksum,int cntk,int nsum){
//if(index<1 || nsum>n || cntk>k) return;
if(nsum==n && cntk == k){
if(ksum>maxk){
res=tmp;
maxk=ksum;
}
return;
}
tmp.push_back(index);
if(nsum+vis[index]<=n && cntk+<=k)dfs(index,ksum+index,cntk+,nsum+vis[index]);
tmp.pop_back();
if(index->)dfs(index-,ksum,cntk,nsum);
}
int main(){
scanf("%d %d %d",&n,&k,&p);
int i;
for(i=;i<=n;i++){
int res = pow(i,p);
if(res>n)break;
vis[i]=res;
}
i--;
dfs(i,,,);
if(maxk==-)printf("Impossible");
else{
printf("%d = ",n);
sort(res.begin(),res.end(),cmp);
for(int j=;j<res.size();j++){
printf("%d^%d",res[j],p);
if(j<res.size()-)printf(" + ");
}
}
}

注意点:看到题目想到了要从大到小一个个遍历然后去比较条件,想用while和for写出来,发现真的写不来,有好多情况,看了大佬的思路,原来这就是递归,很明显的有个递归边界,递归式也很方便,果然对递归的理解还是不够深,知道思路,却没想到用递归这个武器。

最新文章

  1. C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 远程同步服务器大量基础数据到客户端
  2. mongodb 维护
  3. Piggy-Bank
  4. css3 钟表
  5. 关于HTML中浮动与清除的思考
  6. sql 选取每个分组中的第一条数据
  7. Ajax的“dataType”乱用的陷阱
  8. 李洪强iOS开发之使用CycleScrollView实现轮播图
  9. 用Canvas,画中国国旗(Canvas基本知识点)
  10. Python学习--字典
  11. python之tuple
  12. [BZOJ1601] [Usaco2008 Oct] 灌水 (kruskal)
  13. Extjs6.2 项目学习系列(一)
  14. java基础1之基本数据类型
  15. linux-网站收藏
  16. css 可继承属性 display:inline-block 历史
  17. socket 发送图片
  18. function方法控制是否隐藏部分内容
  19. 有关JOIN ON的心得体会
  20. 【liunx】nslookup命令

热门文章

  1. elasticsearch6.7 05. Document APIs(6)UPDATE API
  2. 【nginx】详细配置说明
  3. js .map方法
  4. Python3.4:splinter or traceback
  5. javascript: checked 不可全选
  6. [工作总结]jQuery在工作开发中常用代码片段集锦(1-10)
  7. 微软 WPC 2014 合作伙伴keynote
  8. 测试思想-集成测试&#160;关于接口测试&#160;Part&#160;2
  9. weblogic系列漏洞整理 -- 1. weblogic安装
  10. 08-OpenLDAP主机控制策略