标签:深度优先 resize stream printf 递归 总结 深度优先遍历 ace index
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int n, k, p, maxFacSum = -1;
vector<int> v, ans, tempAns;
void init(){
int temp = 0, index = 1;
while(temp <= n){
v.push_back(temp);
temp = pow(index, p);
index++;
}
}
void dfs(int index, int tempSum, int tempK, int facSum){
if(tempK == k){
if(tempSum == n && facSum > maxFacSum){
ans = tempAns;
maxFacSum = facSum;
}
return;
}
while(index >= 1){
if(tempSum + v[index] <= n){
tempAns[tempK] = index;
dfs(index, tempSum + v[index], tempK + 1, facSum + index);
}
if(index == 1) return;
index--;
}
}
int main(){
scanf("%d%d%d", &n, &k, &p);
init();
tempAns.resize(k);
dfs(v.size() - 1, 0, 0, 0);
if(maxFacSum == -1){
printf("Impossible");
return 0;
}
printf("%d = ", n);
for(int i = 0; i < ans.size(); i++){
if(i != 0) printf(" + ");
printf("%d^%d", ans[i], p);
}
return 0;
}
A1103 Integer Factorization (30分)(DFS)
标签:深度优先 resize stream printf 递归 总结 深度优先遍历 ace index
原文地址:https://www.cnblogs.com/tsruixi/p/13226914.html