线性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. 一篇文章了解JsBridge

    链接:https://blog.csdn.net/duwen90/article/details/79389545

  2. HBO《硅谷》中的二进制码

    先反思一下……这两天感觉除了coding,没有很好地去学习专业课.心神不定 于是,就想看下硅谷,来提升一下自己的coding执行力…… 然后,我就在看剧的时候,看到了这么一张图:‘ 然后嘛…… 我就想 ...

  3. Git - 03. git 工作空间

    1. 概述 git 存放代码的地方 2. 创建 命令 # 1. 从无到有 > git init # 2. 从远程拉去现有的仓库 > git clone <url> 3. 文件生 ...

  4. 【常见浏览器的UA】

    "所谓 UA(User Agent / 用户代理),最初是指浏览器告知网站管理员,本浏览器支持的类型和框架,让网站管理员以支持的模式结构向浏览器发送页面,呈现给用户浏览.演变到今天,网站管理 ...

  5. Ubuntu执行sudo apt-get update报错E: 无法获得锁 /var/lib/apt/lists/lock - open (11: 资源暂时不可用) E: 无法对目录 /var/lib/apt/lists/ 加锁

    一.强制解锁,执行语句 sudo rm /var/lib/apt/lists/lock 二.终端输入 ps -aux | grep apt-get 查看一下apt-get的相关进程.然后sudo ki ...

  6. animate使用方法

    链接:https://www.cnblogs.com/xiaohuochai/p/7372665.html

  7. C do...while 循环

    不像 for 和 while 循环,它们是在循环头部测试循环条件.在 C 语言中,do...while 循环是在循环的尾部检查它的条件. do...while 循环与 while 循环类似,但是 do ...

  8. 三星前有note7,现有GalaxyS10,爆炸原因外力?

    编辑 | 于斌 出品 | 于见(mpyujian) 提到三星,不知道大家什么感觉,反正首先映入脑海的是在Note 7系列爆炸中,三星就让中国消费者欲哭无泪的画面.而也正是三星的态度,三星手机在这件事情 ...

  9. 【译】高级T-SQL进阶系列 (四)【上篇】:使用游标进行行级别处理

    [译注:此文为翻译,由于本人水平所限,疏漏在所难免,欢迎探讨指正] 原文链接:传送门. 正常来说,使用游标并不是处理记录集的最佳方式.然而当一个经验丰富的程序员第一次开始写TSQL时,他们经常会寻找其 ...

  10. 传奇衣服、翅膀、武器、怪物、NPC等外观代码计算方法与公式

    详细介绍humX.wil  weaponX.wil itemsX.wil dnitemsX.wil stateitemX.wil monX.wil代码计算方法   以下计算方法适用于HERO引擎和3K ...