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 ...
随机推荐
- python如何读写EXCEL文档 (有助于自动化的参数化,用的方法是XLRD,XLWT)
读EXCEL: import xlrd 例1, data=xlrd.open("E:\egg.xls") # 打开xls文件 table=data.sheets()[0] ...
- visual studio code -- python
录: 前提: 已安装python 在vsc中安装pthon模块 快速入门 打开控制台(ctrl+shift+P):Python: Select Interpreter,选择python解释器 或者在软 ...
- HTML5: HTML5 Web SQL 数据库
ylbtech-HTML5: HTML5 Web SQL 数据库 1.返回顶部 1. HTML5 Web SQL 数据库 Web SQL 数据库 API 并不是 HTML5 规范的一部分,但是它是一个 ...
- 《Java技术》 第二次作业
java第二次作业 (一)学习总结 1.学习使用Eclipse关联jdk源代码,查看String类的equals()方法,截图,并学习其实现方法.举例说明equals方法和==的区别. 在Eclips ...
- Openstack组件部署 — Keystone Install & Create service entity and API endpoints
目录 目录 前文列表 Install and configure Prerequisites 先决条件 Create the database for identity service 生成一个随机数 ...
- 08 java代码块的概述和分类
08.01_面向对象(代码块的概述和分类) A:代码块概述 在Java中,使用{}括起来的代码被称为代码块. B:代码块分类 根据其位置和声明的不同,可以分为局部代码块,构造代码块,静态代码块,同步代 ...
- pytest_参数化之3*3
import pytesttest_user_data1=[{'user':'linda','password':'888888'}, {'user':'servenruby','password': ...
- LLppdd's class meeting!
LLppdd's class meeting! Time Limit: 1 s Memory Limit: 256 MB 题目背景 LLppdd 有一个可爱团结的班级,他们会定期举行班会活动...比如 ...
- Redis 布隆过滤器
1.布隆过滤器 内容参考:https://www.jianshu.com/p/2104d11ee0a2 1.数据结构 布隆过滤器是一个BIT数组,本质上是一个数据,所以可以根据下标快速找数据 2.哈希 ...
- # Python第十节 传参
Python第十节 传参 一. 变量和变量名 首先说明变量名和变量的一点差异 例如: var = [1, 2, 3] `var = "Google" 调用变量var的时候, 既可以 ...