1103 Integer Factorization (30 分)
 

The KP 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 KP 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​​,⋯,aK​​ } is said to be larger than { b​1​​,b​2​​,⋯,bK​​ } if there exists 1≤LK 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<bits/stdc++.h>
using namespace std; const int maxn=; int factor[maxn]; int n,k,p; vector<int> ans,temp; int cnt=; bool flag=false; int maxSum = -; void init(){
int i=,temp=;
while(temp<=n){
factor[i]=(int)pow(i*1.0,p);
temp=factor[i];
cnt=i;
i++;
}
} void DFS(int index,int nowk,int sumW,int sumC){
if(nowk>k||sumC>n)
return; if(nowk==k&&sumC==n){ flag=true; if(sumW>maxSum){
maxSum=sumW;
ans=temp;
}
} if(index->=){
temp.push_back(index);
DFS(index,nowk+,sumW+index,sumC+factor[index]);
temp.pop_back();
DFS(index-,nowk,sumW,sumC); } } int main(){ ios::sync_with_stdio(false);
cin.tie(); cin>>n>>k>>p; init(); DFS(cnt,,,); if(!flag){
cout<<"Impossible\n";
return ;
} cout<<n<<" ="; for(int i=;i<ans.size();i++){
if(i>)
cout<<" +"; cout<<" "<<ans[i]<<"^"<<p;
} cout<<endl; return ; }

最新文章

  1. UVA 11461 - Square Numbers(水题)
  2. 根据某个文件或文件夹自制rpm包
  3. php 图片调整大小 封装类【转载】
  4. Windows下Java File对象创建文件夹时的一个&quot;坑&quot;
  5. hadoop2 环境的搭建(自动HA)
  6. 【转】做产品VS做项目
  7. win7 共享
  8. 自制单片机之一------AT89S51最小系统制做
  9. 安装pybloomfiltermmap 遇到bug
  10. PHP 生成毫秒时间戳
  11. Universal-Image-Loader完全解析--从源代码分析Universal-Image-Loader中的线程池
  12. Spring Boot Web 自定义注解篇(注解很简单很好用)
  13. SQL SERVER-创建Alwayson
  14. 解读《德勤2017年全球CIO报告》:顶级CIO的炼成之道
  15. s21day20 python笔记
  16. python基础面试题(一)
  17. 【PAT】B1073 多选题常见计分法(20 分)
  18. Oracle导入程序Imp的使用详解
  19. 剑指 offer 面试题31 连续子数组的最大和(动态规划)
  20. ECR是什么意思

热门文章

  1. TIOBE 编程语言排行榜是什么,它是如何计算编程语言排行的?
  2. springboot bootstrap.yml 和 application.yml
  3. 在IIS7以上导出所有应用程序池的方法批量域名绑定(网站绑定)
  4. PHP定时任务实现(计划任务 vs node.js)
  5. 2019牛客多校第七场C-Governing sand(线段树+枚举)
  6. activiti7组任务测试
  7. MySQL数据库学习初步
  8. SpringCloud-技术专区-Zuul-使用指南
  9. bootstrap学习(三)表单
  10. elasticsearch启动常见问题