#include <iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<stdbool.h>
//看起来这是一道深度优先遍历的题目,但是我竟然做了好久,最后还是参考书上的代码才写完成的,大概是因为最近睡得太少T_T
using namespace std;
int n,m,k;
const int maxn=410;
vector<int>temppath,path;
int ans=0;
int a[maxn];
void dfs(int x,int idx,int h1,int h2)
{
if(idx>m||h1>n)return;//限制条件
if(idx==m&&h1==n)//终点条件
{
if(h2>ans)
{
ans=h2;
path=temppath;
}return;
}
else{if(x-1>=0){
temppath.push_back(x);//选还是不选的限制条件
dfs(x,idx+1,h1+a[x],h2+x);
temppath.pop_back();
dfs(x-1,idx,h1,h2);
}}
}
int main()
{
freopen("d://jin.txt","r",stdin);
cin>>n>>m>>k;
int i;
for( i=1; i<=n; i++)
{
a[i]=pow(i,k);
if(a[i]>=n)break;
}
int b=i;
dfs(b,0,0,0);
if(path.size()==m)
{
cout<<n<<" =";
for(int i=0; i<m; i++)
{
cout<<" "<<path[i]<<"^"<<k;
if(i!=m-1)cout<<" +";
}
}
else cout<<"Impossible";
return 0;
}
1103. Integer Factorization
最新推荐文章于 2020-02-24 15:41:14 发布