题意:给出一个正整数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. No style sheet with given id found错误

    在chrome中打开html页面,报错No style sheet with given id found,解决方如下

  2. pdi vcard-2.1

    vCard The Electronic Business Card Version 2.1 A versit Consortium Specification September 18, 1996 ...

  3. pip安装tensorflow出错怎么办

    随着人工智能的开发越来越多人参与,现在下载tensorflow 1.2版本也经常出错了,如下: 这时怎么办呢? 其实比较简单,可以通过pypi的网站来下载: https://pypi.python.o ...

  4. [置顶] 记最近一次Nodejs全栈开发经历

    背景: 前段时间大部门下新成立了一个推广百度OCR.文字识别.图像识别等科技能力在金融领域应用的子部门.因为部门刚成立,基础设施和人力都是欠缺的.当时分到我们部门的任务是抽调一个人做新部门主站前端开发 ...

  5. 用户空间与内核驱动的交互过程 — ioctl

    在Linux内核模块的开发过程中,经常涉及到运行在用户空间上的应用程序与内核模块进行交互,ioctl系统调用是常用的一种方式.本文并不涉及vlan的具体原理,仅通过vconfig与vlan内核模块进行 ...

  6. vim简单的文本编辑命令

    <blockquote>:e! enter</blockquote>消除所有这次编辑的,回到原来文件的样子.不过此文件还是打开状态.<blockquote>:q! ...

  7. get、post接口测试-java

    public class HttpClient { //get请求方法 public String sendGet(String url, String data) { Date date = new ...

  8. BZOJ3444 最后的晚餐【细节题+组合数学】*

    BZOJ3444 最后的晚餐 Description [问题背景] 高三的学长们就要离开学校,各奔东西了.某班n人在举行最后的离别晚餐时,饭店老板觉得十分纠结.因为有m名学生偷偷找他,要求和自己暗恋的 ...

  9. win7如何安装maven、安装protoc

    问题导读1.protoc安装需要安装哪些软件?2.如何验证maven是否安装成功?3.如何验证protoc是否安装成功 ? 一.安装mvaven包 1.首先我们下载maven包 apache-mave ...

  10. 【oracle】Oracle中as关键字

    在Oracle中as关键字不能用于指定表的别名 在Oracle中指定表的别名时只需在原有表名和表的别名之间用空格分隔即可 但as关键字可以用于指定列的别名 但在存储过程中如果列的别名与原有列名相同,在 ...