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 ...
随机推荐
- interleave two text files with specified lines
a_file=$1 a_step=$2 b_file=$3 b_step=$4 a_start=1 let a_end=$a_start+$a_step b_start=1 let b_end=$b_ ...
- python学习笔记:数据类型——列表/数组(list)
Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素.通过下标访问列表中的元素(又称索引.角标),下标从0开始计数.list定义,使用中括号[]. l ...
- keepalive+Haproxy
1.keepalive Keepalived 是一款轻量级HA集群应用,它的设计初衷是为了做LVS集群的HA,即探测LVS健康情况,从而进行主备切换,不仅如此,还能够探测LVS代理的后端主机的健康状况 ...
- awk 按小时 统计接口调用次数
#统计所有接口总量awk -F ' ' '{a[$7]++} END{for(i in a){print i,a[i] | "sort -r -k 7"}}' accesslog/ ...
- shell getopts命令
由于shell命令行的灵活性,自己编写代码判断时,复杂度会比较高.使用内部命令 getopts 可以很方便地处理命令行参数.一般格式为: getopts optstring name [args] ...
- spark textFile读取多个文件
1.spark textFile读取File 1.1 简单读取文件 val spark = SparkSession.builder() .appName("demo") .mas ...
- 從nasm assembly看函數參數傳遞
在淘宝定了<<C++程序设计语言(特别版)>> 后天才能到货.从网上下了<<C++ Primer中文版>>的电子书看看.找找C++的感觉先. 先看看基本 ...
- 初探Remoting双向通信(二)
原 初探Remoting双向通信(二) 2013年06月25日 11:46:24 喜欢特别冷的冬天下着雪 阅读数 2977 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...
- 小部分安卓手机 reload 等方法不执行
自己解析 url 来赋值刷新页面 方法如下:// location.href function updateUrl(url, key) { var key = (key || 't') + ...
- sleep()方法和wait()方法的区别? sleep()方法和yield()方法的区别?
sleep()方法和wait()方法的区别? sleep方法是Thread的静态方法,wait方法是Object类的普通方法 sleep方法不释放同步锁,wait方法释放同步锁(执行notify方法唤 ...