PAT 1103 Integer Factorization
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 = n1^P + ... nK^P
where ni (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<iostream> //深度优先搜索
#include<vector>
#include<math.h>
using namespace std;
int N, P, K, maxsum=-1;
vector<int> v, ans, tempans;
void init(){
int temp=0, index=1;
while(temp<=N){
v.push_back(temp);
temp=pow(index, P);
index++;
}
}
void dfs(int index, int tempsum, int tempk, int facsum){
if(tempsum==N&&tempk==K){
if(facsum>maxsum){
ans=tempans;
maxsum=facsum;
}
return ;
}
if(tempsum>N||tempk>K) return ;
for(int i=index; i>=1; i--){
tempans.push_back(i);
dfs(i, tempsum+v[i], tempk+1, facsum+i);
tempans.pop_back();
}
}
int main(){
cin>>N>>K>>P;
init();
dfs(v.size()-1, 0, 0, 0);
if(maxsum==-1){
cout<<"Impossible"<<endl;
return 0;
}
cout<<N<<" = "<<ans[0]<<"^"<<P;
for(int i=1; i<ans.size(); i++)
cout<<" + "<<ans[i]<<"^"<<P;
return 0;
}
PAT 1103 Integer Factorization的更多相关文章
- 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 (DFS)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...
- 1103 Integer Factorization (30)
1103 Integer Factorization (30 分) The K−P factorization of a positive integer N is to write N as t ...
- 【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 ...
- PAT (Advanced Level) 1103. Integer Factorization (30)
暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲级1103 Integer Factorization【dfs】【剪枝】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 题意: 给定一个数n,要求从1~n中找 ...
- PAT甲题题解-1103. Integer Factorization (30)-(dfs)
该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + … nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. ...
- 【PAT甲级】1103 Integer Factorization (30 分)
题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...
随机推荐
- 最直观的poi的使用帮助(告诉你怎么使用poi的官网),操作word,excel,ppt
最直观的poi的使用帮助(告诉你怎么使用poi的官网),poi操作word,excel,ppt 写在最前面 其实poi的官网上面有poi的各种类和接口的使用说明,还有非常详细的样例,所以照着这些样例来 ...
- 使用WCF进行跨平台开发之一(WCF的实现、控制台托管与.net平台的调用)
WCF是Windows Communication Foundation的缩写,是微软发展的一组数据通信的应用程序开发接口,它是.NET框架的一部分,是WinFx的三个重要开发类库之一,其它两个是WP ...
- bzoj 3993 星际战争
题目大意: X军团和Y军团正在激烈地作战 在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团的阵地,其中第i个巨型机器人的装甲值为Ai 当一个巨型机器人的装甲值减少到0或者以下时,这个巨型机 ...
- P2120 [ZJOI2007]仓库建设 斜率优化dp
好题,这题是我理解的第一道斜率优化dp,自然要写一发题解.首先我们要写出普通的表达式,然后先用前缀和优化.然后呢?我们观察发现,x[i]是递增,而我们发现的斜率也是需要是递增的,然后就维护一个单调递增 ...
- bzoj3771
http://www.lydsy.com/JudgeOnline/problem.php?id=3771 生成函数... 其实就是多项式乘法...lrj书上有一个通俗的解释... 然后就是这个样子,我 ...
- Spark 机器学习------逻辑回归
package Spark_MLlib import javassist.bytecode.SignatureAttribute.ArrayType import org.apache.spark.s ...
- codevs3327选择数字(单调队列优化)
3327 选择数字 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 给定一行n个非负整数a[1]..a[n].现 ...
- wxwidgets安装环境配置
一:安装VS2012 wxWidgets-2.9.5( 2.95版本为最稳定版本) 二:打开wxWidgets-2.9.5的安装目录,找到build-msw-wx_vc10.sln打开(等待) 三:打 ...
- android ListView,GridView 设置某一项显示位置
在项目中有时会用到,当使用 listview 想让它显示某一项,当它又不在前面的位置,可以 使用 //让某一项显示出来(否则可能不在当前) listview.setSelection(positio ...
- day03_12/13/2016_bean的管理之作用域与初始化时间
在Spring中,Bean有几种作用域: 1.singleton作用域 当一个bean的作用域设置为singleton,那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean ...