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 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 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<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 ; }
最新文章
- UVA 11461 - Square Numbers(水题)
- 根据某个文件或文件夹自制rpm包
- php 图片调整大小 封装类【转载】
- Windows下Java File对象创建文件夹时的一个";坑";
- hadoop2 环境的搭建(自动HA)
- 【转】做产品VS做项目
- win7 共享
- 自制单片机之一------AT89S51最小系统制做
- 安装pybloomfiltermmap 遇到bug
- PHP 生成毫秒时间戳
- Universal-Image-Loader完全解析--从源代码分析Universal-Image-Loader中的线程池
- Spring Boot Web 自定义注解篇(注解很简单很好用)
- SQL SERVER-创建Alwayson
- 解读《德勤2017年全球CIO报告》:顶级CIO的炼成之道
- s21day20 python笔记
- python基础面试题(一)
- 【PAT】B1073 多选题常见计分法(20 分)
- Oracle导入程序Imp的使用详解
- 剑指 offer 面试题31 连续子数组的最大和(动态规划)
- ECR是什么意思
热门文章
- TIOBE 编程语言排行榜是什么,它是如何计算编程语言排行的?
- springboot bootstrap.yml 和 application.yml
- 在IIS7以上导出所有应用程序池的方法批量域名绑定(网站绑定)
- PHP定时任务实现(计划任务 vs node.js)
- 2019牛客多校第七场C-Governing sand(线段树+枚举)
- activiti7组任务测试
- MySQL数据库学习初步
- SpringCloud-技术专区-Zuul-使用指南
- bootstrap学习(三)表单
- elasticsearch启动常见问题