题意:给出一个正整数N,以及k,p,要求把N分解成k个因式,即N=n1^p + n2^p + ... + nk^p。要求n1,n2,...按降序排列,若有多个解,则选择n1+n2+...+nk最大的,若还有多个解,则选择数字序列较大的。若不存在解,则输出Impossible.

思路:这是深度优先搜索的经典例题。详见:

代码:

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
using namespace std;

;
int factor[maxn];
;
int n,k,p;

void init()
{
    ;
    while(temp<=n) {
        factor[len++]=temp;
        temp=pow(len,p);
    }
}

;
vector<int> ans,tmp;
bool flag=false;
void dfs(int idx,int currK,int currN,int currFacSum)
{
    if(currK==k && currN==n){
        flag=true;
        if(currFacSum>maxFacSum){
            maxFacSum=currFacSum;
            ans=tmp;
        }
        return;
    }
    if(idx==0 || currK>k || currN>n) return;//第一个条件不能漏!
    tmp.push_back(idx);
    dfs(idx,currK+,currN+factor[idx],currFacSum+idx);
    tmp.pop_back();
    dfs(idx-,currK,currN,currFacSum);
}

int main()
{
    scanf("%d%d%d",&n,&k,&p);
    init();//预处理
    dfs(len-,,,);
    if(flag){
        printf(],p);
        ;i<ans.size();i++)
            printf(" + %d^%d",ans[i],p);
    }else{
        printf("Impossible\n");
    }
    ;
}

1103 Integer Factorization的更多相关文章

  1. PAT 1103 Integer Factorization[难]

    1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...

  2. PAT甲级1103. Integer Factorization

    PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...

  3. PAT甲级——1103 Integer Factorization (DFS)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...

  4. 1103 Integer Factorization (30)

    1103 Integer Factorization (30 分)   The K−P factorization of a positive integer N is to write N as t ...

  5. 1103. Integer Factorization (30)

    The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

  6. 【PAT】1103 Integer Factorization(30 分)

    The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

  7. 1103 Integer Factorization (30)(30 分)

    The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

  8. PAT 1103 Integer Factorization

    The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

  9. PAT (Advanced Level) 1103. Integer Factorization (30)

    暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  10. PAT甲级1103 Integer Factorization【dfs】【剪枝】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 题意: 给定一个数n,要求从1~n中找 ...

随机推荐

  1. 20165202 2017-2018-2 《Java程序设计》第8周学习总结

    20165202 2017-2018-2 <Java程序设计>第8周学习总结 教材学习内容总结 Ch12 进程与线程 线程是比进程更小的单位,一个进程在其执行过程中,可以产生多个线程 Ja ...

  2. Python面向对象 --- 新旧式类、私有方法、类属性和类方法、静态方法

    一.Python面向对象中的新旧式类 1)新式类(推荐使用):在定义类时,类后边括号里要继承基类(object).在python3.x中若没有指定父类,会默认使用的是object作为基类:在pytho ...

  3. make笔记

    Makefile基本格式如下: target ... : prerequisites ... command ... ... 其中, target - 目标文件, 可以是 Object File, 也 ...

  4. EventUtil对象

    var EventUtil = { addHandler : function(element,type,handler){ if(element.addEventListener){ element ...

  5. MFC CListControl 点击列头排序的实现

    SetItemData可以为每一行绑定一个DWORD类型的变量.用GetItemData可以获得这个变量.举个例子,假设CListCtrl中你需要显示某个数据表中的记录,该表有个流水号主键ID,一般这 ...

  6. 使用ElasticSearch完成百万级数据查询附近的人功能

    上一篇文章介绍了ElasticSearch使用Repository和ElasticSearchTemplate完成构建复杂查询条件,简单介绍了ElasticSearch使用地理位置的功能. 这一篇我们 ...

  7. FFmpeg再学习 -- 硬件加速编解码

    为了搞硬件加速编解码,用了一周时间来看 CUDA,接下来开始加以总结. 一.什么是 CUDA (1)首先需要了解一下,什么是 CUDA. 参看:百度百科 -- CUDA 参看:CUDA基础介绍 参看: ...

  8. [Linux] 终端设置只显示当前目录及终端美化

    1.只显示当前目录 vim ~/.bashrc 找到位置: if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($d ...

  9. Qt jsoncpp 对象拷贝、删除、函数调用 demo

    /*************************************************************************************************** ...

  10. 用两个stack实现一个队列

    class Queue { stack<int> input, output; public: void push(int x) { input.push(x); } void pop(v ...