A1103 Integer Factorization (30 分)

在这里插入图片描述
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int n,k,p;//n = A1^p+...+Ak^p

vector<int> V_factor;
int MaxsumFac=-1;
vector<int> temp; //存储暂时的答案
vector<int> ans;  //存储最终的答案

void getFactor()
{
    int temp=0;
    int i=0;
    while(temp<=n)
    {
        V_factor.push_back(temp);
        temp = pow(++i,p);
    }
}

// void PrintTemp()
// {
//     for(vector<int>::iterator it = temp.begin();it!=temp.end();it++)
//     {
//         cout << *it << " ";
//     }
//     cout << "\n";
// }
void DFS(int index,int nowK,int sumSqu,int sumFac)
{
    //PrintTemp();
    //1.已经找到答案了
    if(nowK == k && sumSqu == n)
    {
        if(sumFac > MaxsumFac)
        {
            MaxsumFac = sumFac;
            ans = temp;
        }
        return;
    }
    //2.不会有答案的情况
    if(sumSqu > n || nowK > k)
        return;
    //3.继续往下遍历
    //假如选择index号
    if(index > 0)
    {
        temp.push_back(index);
        DFS(index,nowK+1,sumSqu+V_factor[index],sumFac+index);
        //如果不选index号
        temp.pop_back();
        DFS(index-1,nowK,sumSqu,sumFac);
    }
}

void printRes()
{
    printf("%d = %d^%d",n,ans[0],p);
    for(int i=1;i<k;i++)
    {
        printf(" + %d^%d",ans[i],p);
    }       
}
int main()
{
    scanf("%d%d%d",&n,&k,&p);
    //找到质因数
    getFactor();
    DFS(V_factor.size()-1,0,0,0);
    if(MaxsumFac == -1)
        printf("Impossible\n");
    else
        printRes();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值