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 ; }
1103 Integer Factorization (30)的更多相关文章
- 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 ...
- 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 ...
- 【PAT甲级】1103 Integer Factorization (30 分)
题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...
- PAT (Advanced Level) 1103. Integer Factorization (30)
暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲题题解-1103. Integer Factorization (30)-(dfs)
该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + … nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. ...
- PAT甲级——1103 Integer Factorization (DFS)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...
- PAT 1103 Integer Factorization[难]
1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...
- PAT甲级1103. Integer Factorization
PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...
- 【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 ...
随机推荐
- HTML-参考手册: 颜色混搭
ylbtech-HTML-参考手册: 颜色混搭 1.返回顶部 1. HTML 颜色混搭 混搭两种颜色,并查看效果: 选择颜色: #FF0000 #0000FF 顶部颜色: ...
- [c#源码分享]TCP通信中的大文件传送
NetworkComms网络通信框架序言 源码 (为节省空间,不包含通信框架源码,通信框架源码请另行下载) 文件传送在TCP通信中是经常用到的,本文针对文件传送进行探讨 经过测试,可以发送比较大的 ...
- SecureCRT key登录linux ssh设置
一.首先用secureCrt创建密钥 1.使用SecureCRT创建私钥和公钥. SecureCRT quick Connect-> Authentiation -> Public Key ...
- nginx-413
修改nginx配置文件:client_max_body_size 300M 修改php.ini upload_max_filesize | post_max_size
- centos7 编译打包bcache-tools
centos7 build bcache-tools 获取源码 centos 本身不提供bcache-tools的rpm,所以需要自己build. 从fedora下载源码,也可以从github社区下载 ...
- Java.util包教程
java.util.ArrayDeque 类提供了可调整大小的阵列,并实现了Deque接口.以下是关于阵列双端队列的要点: 数组双端队列没有容量限制,使他们增长为必要支持使用. 它们不是线程安全的;如 ...
- AOP之PostSharp2-OnMethodBoundaryAspect
在上一篇中我们了解了简单的OnExceptionAspectAOP面向方向切入,在第一节中我们将继续我们的PostSharp AOP系列的OnMethodBoundaryAspect方法行为的切入,这 ...
- 使用source insert 查看Linux内核源码
先配置下source insert软件,添加工程文件时可以支持各种类型的文件 “ Options ” --> “ Preferences ” ---> “ Languages ” ---& ...
- STL_set
#include <iostream> #include <set> #include <string> #include <cstdio> using ...
- spring中配置Properties对象的方法
工作中有必要将Properties对象进行注解进入:比如 class Person{ @Autowired private Properties properties; } 如果有这种需求的话,那么需 ...