1103. Integer Factorization

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值