2019年9月25日,在实验室测试,结果依然不对,排除机器问题
![QQ截图20190925163812.png QQ截图20190925163812.png](https://www.noah-dream.com/usr/uploads/2019/09/2180411418.png)
![QQ图片20190926074816.png QQ图片20190926074816.png](https://www.noah-dream.com/usr/uploads/2019/09/1580531428.png)
世间居然有这么离奇的事情,赶紧把代码丢到实验室群里,这时大佬抛出了解答!
![QQ截图20190926074850.png QQ截图20190926074850.png](https://www.noah-dream.com/usr/uploads/2019/09/2334869216.png)
这究竟是怎么一回事,等待后期补充
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>ad, ans, temp, num;
int n, k, p, maxnum = -999;
void dfs(int cnt, int sum, int sumindex, int index)
{
if (cnt >= k || sum >= n || index < 1)
{
if (sum == n&&cnt==k) {
if (sumindex > maxnum) {
ans = temp;
maxnum = sumindex;
}
}
return;
}
temp.push_back(num[index]);
dfs(cnt + 1, sum + ad[index], sumindex + num[index], index);
temp.pop_back();
dfs(cnt, sum, sumindex, index - 1);
}
int main()
{
cin >> n >> k >> p;
for (int i = 0;; i++) {
int t = (int)pow(i, p);
if (t < n) {
ad.push_back(t);
num.push_back(i);
}
else {
break;
}
}
dfs(0, 0, 0, ad.size() - 1);
for (int i = 0; i < ans.size(); i++) {
if (i == 0) {
cout << n << " =";
}
cout << " " << ans[i] << "^" << p;
if (i < ans.size() - 1) {
cout << " +";
}
}
if (ans.size() == 0) {
cout << "Impossible";
}
return 0;
}