大的index开头,
当前方案还从index开始,下一方案从index-1开始。
当前方案下一个还可以是index
#include <bits/stdc++.h>
using namespace std;
int n,k,p;
#define maxn 420
vector<int> t;
void initT(){
int i=0;
while(pow(i,p)<=n){
t.push_back(pow(i,p));
i++;
}
}
int max_fac=-1;
vector<int> temp_ans,ans;
void dfs(int root,int temp_k,int temp_sum,int temp_fac){
if(temp_sum>n) return;
if(temp_k==k){
if(temp_sum==n && temp_fac>max_fac){
ans=temp_ans; //vector好处:互相赋值
max_fac=temp_fac;
}
return;
}
//2:当前位置:index递减 or index
// 下一位置:index递减 or index
while(root>=1){
if(temp_sum+t[root]<=n){
temp_ans[temp_k]=root;
dfs(root,temp_k+1,temp_sum+t[root],temp_fac+root);
}
root--;
}
}
int main(){
cin>>n>>k>>p;
initT();
temp_ans.resize(k);
//1:index从t[t.size()-1]开始
dfs(t.size()-1,0,0,0);//n太大了,利用最大size()可以放小很多
if(ans.empty()){
cout<<"Impossible";
return 0;
}
cout<<n<<" = ";
for(int i=0;i<ans.size();i++){
if(i==0) cout<<ans[i]<<"^"<<p;
else cout<<" + "<<ans[i]<<"^"<<p;
}
return 0;
}