当前位置:网站首页>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:
ImpossibleThe 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;
}边栏推荐
- Shengteng experience officer Episode 5 notes I
- Full details of efficientnet model
- PLC: automatically correct the data set noise, wash the data set | ICLR 2021 spotlight
- leetcode:648. Word replacement [dictionary tree board + find the shortest matching prefix among several prefixes]
- 一个需求温习到的所有知识,h5的表单被键盘遮挡,事件代理,事件委托
- Notes HCIA
- CTFshow,信息搜集:web9
- Summer safety is very important! Emergency safety education enters kindergarten
- PG basics -- Logical Structure Management (locking mechanism -- table lock)
- WebRTC 音频抗弱网技术(上)
猜你喜欢

广州开发区让地理标志产品助力乡村振兴

什么是云原生?这回终于能搞明白了!

Ctfshow, information collection: web2

Deformable convolutional dense network for enhancing compressed video quality

Today's sleep quality record 78 points

在软件工程领域,搞科研的这十年!

CTFshow,信息搜集:web3

Novel Slot Detection: A Benchmark for Discovering Unknown Slot Types in the Dialogue System

Data Lake (IX): Iceberg features and data types

CTFshow,信息搜集:web9
随机推荐
Pytorch model trains practical skills and breaks through the bottleneck of speed
Es log error appreciation -trying to create too many buckets
PAG experience: complete AE dynamic deployment and launch all platforms in ten minutes!
Used by Jetson AgX Orin canfd
CTFshow,信息搜集:web13
Ctfshow, information collection: web10
Win10 or win11 taskbar, automatically hidden and transparent
Read PG in data warehouse in one article_ stat
Deformable convolutional dense network for enhancing compressed video quality
Compile advanced notes
Bits and Information & integer notes
"Baidu Cup" CTF competition 2017 February, web:include
2022年5月互联网医疗领域月度观察
⼀个对象从加载到JVM,再到被GC清除,都经历了什么过程?
Yyds dry goods inventory # solve the real problem of famous enterprises: cross line
CTFshow,信息搜集:web6
Lidar knowledge drops
Infinite innovation in cloud "vision" | the 2022 Alibaba cloud live summit was officially launched
Why do we use UTF-8 encoding?
WebRTC 音频抗弱网技术(上)