题意:给出一个正整数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. droppable放置组件

    Droppable 放置组件 所谓放置,就将一个事物入一个事物内触发各种效果,这个组件不依赖于其他组件.1.加载方式 //class 调用 <div id="dd" clas ...

  2. JWT的详细简介

    什么是JWT Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准.该token被设计为紧凑且安全的,特别适用于分布式站点的单点登录(SSO)场 ...

  3. 编译安装pgbouncer-checking for OpenSSL... configure: error: not found

    花了一上午时间将pgbouncer的参数通读了一遍,对他有个大致的了解:1.配置分为连接池和pgbouncer两个部分[database]\[pgbouncer ].2.一条记录对应创建一个连接池,连 ...

  4. MySQL单独存放表空间Innodb_file_per_table

    在mysql中Innodb_file_per_table参数的作用是什么呢,其实许多的朋友是不知道的,今天我们来看这篇MySQL单独存放表空间Innodb_file_per_table参数详解之后你就 ...

  5. 安装visio 2010:您的计算机上的Office 2003安装已损坏,安装程序无法继续。请删除或修复office 2003产品并重新运行安装程序

    您的计算机上的Office 2003安装已损坏,安装程序无法继续.请删除或修复office 2003产品并重新运行安装程序   最近打算安装visio 2010时出现 以下错误: “您的计算机上的Of ...

  6. React-Native基础_4.View组件

    View组件 对应ios 的UIView android 中的view 使用要先导入View import { View } from 'react-native'; 使用就是View标签,可以添加S ...

  7. Cannot mix incompatible Qt library (version 0x40805) with this library (version 0x40801)

    问题描述 今天运行我的 linux 上的 go 语言 IDE liteide 突然报错,错误如下: Cannot mix incompatible Qt library (version 0x4080 ...

  8. Kafka 单节点多Kafka Broker集群

    Kafka 单节点多Kafka Broker集群 接前一篇文章,今天搭建一下单节点多Kafka Broker集群环境. 配置与启动服务 由于是在一个节点上启动多个 Kafka Broker实例,所以我 ...

  9. linux中的网络基础

    ifconfig -a 查看所有网口ifconfig eth 查看具体网口 ifup ethoifdown etho 网卡配置文件/etc/sysconfig/networkk-scripts/ifc ...

  10. ExpressCache

    ExpressCache 非联想.等品牌电脑使用的ExpressCache(带破解文件). (原始安装文件+patch+说明).注意,执行eccmd命令时,需要管理员权限.否则报错. http://a ...