深搜过程中有两条分支 选当前数或者不选当前数,
不过ACWing的数据比较牛逼 我这种方法是过不去的 ,随后尝试了一下打表,依然过不去
#include <bits/stdc++.h>
using namespace std;
int N,K,P,sum=0;
vector<int> temp,result;
void DFS(int num,int numSum,int numK,int numPowSum){
if (numK>=K or numPowSum>=N) {
if (numK == K and numPowSum == N and numSum > sum) {
result=temp;
sum=numSum;
}
return ;
}if (num>=1) {
temp.push_back(num);
DFS(num, numSum + num, numK + 1, numPowSum + pow(num * 1.0, P * 1.0));
temp.pop_back();
DFS(num - 1, numSum, numK, numPowSum);
}
}
int main(){
cin>>N>>K>>P;
DFS((int)pow(N*1.0,1.0/P),0,0,0);
if (result.size()==K){
printf("%d = ",N);
for (int i = 0; i < result.size(); ++i) {
printf("%s%d^%d",i==0?"":" + ",result[i],P);
}
}else
printf("Impossible");
return 0;
}