线性dfs,注意每次深搜完状态的维护~

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
vector<int> v,tmp,path;
int n,k,p;
void init () {
int t=,cnt=;
while (t<=n) {
v.push_back(t);
t=pow(cnt,p);
cnt++;
}
}
int maxFacSum=-;
void dfs (int nowindex,int nowsum,int nowK,int facSum) {
if (nowK>k||nowsum>n) return;
if (nowK==k) {
if (nowsum==n) {
if (facSum>maxFacSum) {
path=tmp;
maxFacSum=facSum;
}
}
//tmp.pop_back();
return;
}
while (nowindex>=) {
tmp.push_back(nowindex);
dfs (nowindex,nowsum+v[nowindex],nowK+,facSum+nowindex);
tmp.pop_back();
if (nowindex==) return;
nowindex--;
}
}
int main () {
scanf ("%d %d %d",&n,&k,&p);
init ();
dfs (v.size()-,,,);
if (maxFacSum==-) {
printf ("Impossible");
return ;
}
printf ("%d = ",n);
for (int i=;i<path.size();i++) {
if (i!=) printf (" + ");
printf ("%d^%d",path[i],p);
}
return ;
}

PAT A1103 Integer Factorization的更多相关文章

  1. PAT A1103 Integer Factorization (30 分)——dfs,递归

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

  2. PAT 1103 Integer Factorization[难]

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

  3. PAT甲级——A1103 Integer Factorization

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

  4. A1103. 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 ...

  5. 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 ...

  6. PAT_A1103#Integer Factorization

    Source: PAT A1103 Integer Factorization (30 分) Description: The K−P factorization of a positive inte ...

  7. PAT甲级1103. Integer Factorization

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

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

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

  9. PAT A1103

    PAT A1103 标签(空格分隔): PAT 解题思路: DFS #include <cstdio> #include <vector> using namespace st ...

随机推荐

  1. 3.CRUD(增删改查)

    Select 选择,查询语句 id:就是对应的namespace中的方法名: resultType:Sql语句执行的返回值: parameterType:参数类型 我们想使用查询语句首先要在UserM ...

  2. ASA许可证

    每台安装了BASE license的ASA平台都自带了一些隐藏的特性和功能.根据不同国际出口规则,有些ASA上安装的有可能是NO Payload Encryption license.这种许可证会绑定 ...

  3. 题解【洛谷P2853】[USACO06DEC]牛的野餐Cow Picnic

    题目描述 The cows are having a picnic! Each of Farmer John's \(K (1 ≤ K ≤ 100)\) cows is grazing in one ...

  4. Java中正负数的存储方式-正码 反码和补码

    Java中正负数的存储方式-正码 反码和补码 正码 我们以int 为例,一个int占用4个byte,32bits 0 存在内存上为 00000000 00000000 00000000 0000000 ...

  5. Windows系统重装记录

    材料: u盘(需4g以上) windows官方镜像 附:windows个版本比较 步骤: u盘格式化(为了装启动盘系统需要清空数),备份系统盘所需要的的数据 下载适合自己的官方镜像,可从该网站下载(官 ...

  6. 单例模式的Java泛型实现方式

    import java.util.HashMap; import java.util.Map; /** * Created by zhao.wu on 2016/11/18. */ public cl ...

  7. linux开启端口命令

    1. 开放端口命令: /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT 2.保存:/etc/rc.d/init.d/iptables save ...

  8. python中的坎坷之函数、集合

    函数 作用:实现一个功能,函数理解成一个工具,遇到问题把这个工具拿来用 优点:函数更方便,复用,可以在多个场景下用 1.带参数的函数 name='lzs-nice' nane.split('-') # ...

  9. 【PAT甲级】1075 PAT Judge (25 分)

    题意: 输入三个正整数N,K,M(N<=10000,K<=5,M<=100000),接着输入一行K个正整数表示该题满分,接着输入M行数据,每行包括学生的ID(五位整数1~N),题号和 ...

  10. Java常量/变量

    1. 常量 /* 常量:在程序运行期间,固定不变的量. 常量的分类: 1. 字符串常量:凡是用双引号引起来的部分,叫做字符串常量.例如:"abc"."Hello" ...