当前位置:网站首页>Pat grade a 1103 integer factorizatio
Pat grade a 1103 integer factorizatio
2022-07-07 14:59:00 【Zhemu】
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
The question : to 169(N) Factorization , Decompose into 5(K) term , Each factor can be chosen many times , Finally, output the selected factor 2(p) Power ( Descending order , And ensure that the sequence sum of the sequence is the largest of all solutions )
n,p,k Input according to the topic
Ideas : Think of the complete knapsack problem , That is, an item can be selected many times , Each item can be selected or not , So there are items associated with backpack items , Count n Think of backpack capacity , Sequence and maximum associative knapsack maximum value , Therefore, we can write a full knapsack depth first search model
// Because it is the first choice , Nest once and choose once , This can reflect the complete knapsack problem ;
temp.push_back( );// Choose... First ;
DFS(index,sum,facsum);// The situation of choice ;
temp.pop_back( )// The selected situation ends ;
DFS(index-1,sum,facsum);// Don't choose this one , Go to the next judgment ;
Code :
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n,k,p;// Count , Yes n Number of items ,p Power ;
int maxfac=-1;// Used to judge whether there is an optimal solution , What is the optimal solution ;
vector<int > fac,temp,ans;// Place the sum of squares of the pretreatment , Temporary answer sequence , The final answer sequence ;
int power(int x){// Count p Power ;
int ans=1;
for(int i=0;i<p;i++){
ans*=x;// seek x Of p Power , How many times is how many times ;
}
return ans;
}
void deal( ){// Preprocess the sum of squares to fac Inside ;
int temp=0,i=0;
while(temp<=n){
fac.push_back(temp);
temp=power(++i);
}
}
// Currently processed to index term , Now selected nowk Number , At this time, the number of choices p The sum of powers , Factor and ( The factor used to judge whether the solution is the largest );
void DFS(int index,int nowk,int sum,int sumfac){
if(sum==n&&nowk==k){// First judge the critical condition ;
if(maxfac<sumfac){// At present, it is the optimal sequence ;
ans=temp;//vector Arrays can be directly replaced ;
maxfac=sumfac;// Update Max ;
}
return ;
}
if(sum>n||nowk>k)return ;// Pruning leaves ;
if(index-1>=0){//fas[0] No choice ;
temp.push_back(index);// Choose... First ;
DFS(index,nowk+1,sum+fac[index],sumfac+index);// Choose it and see if you can choose ;
temp.pop_back();// You can't choose again or don't choose if you don't meet the conditions ;
DFS(index-1,nowk,sum,sumfac);// Enter other branches to judge ;
}
}
int main( ){
scanf("%d %d %d",&n,&k,&p);
deal( );// Preprocessing , The of each item p To the power of ;
DFS(fac.size()-1,0,0,0);// Don't forget to use depth first search to find the best solution , That is, don't forget to call the function ;
if(maxfac==-1) printf("Impossible");
else
for(int i=0;i<ans.size();i++) i==0?printf("%d = %d^%d",n,ans[i],p):printf(" + %d^%d",ans[i],p);
return 0;
}
Uncommented code :
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n,k,p;
int maxfac=-1;
vector<int > fac,temp,ans;
int power(int x){
int ans=1;
for(int i=0;i<p;i++){
ans*=x;
}
return ans;
}
void deal( ){
int temp=0,i=0;
while(temp<=n){
fac.push_back(temp);
temp=power(++i);
}
}
void DFS(int index,int nowk,int sum,int sumfac){
if(sum==n&&nowk==k){
if(maxfac<sumfac){
ans=temp;
maxfac=sumfac;
}
return ;
}
if(sum>n||nowk>k)return ;
if(index-1>=0){
temp.push_back(index);
DFS(index,nowk+1,sum+fac[index],sumfac+index);
temp.pop_back();
DFS(index-1,nowk,sum,sumfac);
}
}
int main( ){
scanf("%d %d %d",&n,&k,&p);
deal( );
DFS(fac.size()-1,0,0,0);
if(maxfac==-1) printf("Impossible");
else
for(int i=0;i<ans.size();i++) i==0?printf("%d = %d^%d",n,ans[i],p):printf(" + %d^%d",ans[i],p);
return 0;
}
边栏推荐
- 比尔·盖茨晒48年前简历:“没你们的好看”
- Niuke real problem programming - day16
- Navigation — 这么好用的导航框架你确定不来看看?
- Ctfshow, information collection: web13
- Summer safety is very important! Emergency safety education enters kindergarten
- ⼀个对象从加载到JVM,再到被GC清除,都经历了什么过程?
- leetcode:648. Word replacement [dictionary tree board + find the shortest matching prefix among several prefixes]
- 防火墙基础之服务器区的防护策略
- 2022 cloud consulting technology series high availability special sharing meeting
- 【服务器数据恢复】戴尔某型号服务器raid故障的数据恢复案例
猜你喜欢
Computer win7 system desktop icon is too large, how to turn it down
C 6.0 language specification approved
Ctfshow, information collection: web9
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
Ctfshow, information collection: web5
asp.netNBA信息管理系统VS开发sqlserver数据库web结构c#编程计算机网页源码项目详细设计
Pytorch model trains practical skills and breaks through the bottleneck of speed
[understanding of opportunity -40]: direction, rules, choice, effort, fairness, cognition, ability, action, read the five layers of perception of 3GPP 6G white paper
数学建模——什么是数学建模
CTFshow,信息搜集:web14
随机推荐
Ascend 910 realizes tensorflow1.15 to realize the Minist handwritten digit recognition of lenet network
Es log error appreciation -- allow delete
Why do we use UTF-8 encoding?
Niuke real problem programming - Day12
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
In the field of software engineering, we have been doing scientific research for ten years!
Stm32cubemx, 68 sets of components, following 10 open source protocols
Andriod --- JetPack :LiveData setValue 和 postValue 的区别
8大模块、40个思维模型,打破思维桎梏,满足你工作不同阶段、场景的思维需求,赶紧收藏慢慢学
缓冲区溢出保护
一文读懂数仓中的pg_stat
asp.netNBA信息管理系统VS开发sqlserver数据库web结构c#编程计算机网页源码项目详细设计
Es log error appreciation -maximum shards open
什么是数据泄露
Niuke real problem programming - Day11
Instructions for mictr01 tester vibrating string acquisition module development kit
CTFshow,信息搜集:web8
How bad can a programmer be? Nima, they are all talents
PD virtual machine tutorial: how to set the available shortcut keys in the parallelsdesktop virtual machine?
什么是pv和uv? pv、uv