线性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. C语言特点有哪些?

    C语言的特点 : 1.简洁紧凑.灵活方便 C语言一共只有32个关键字,9种控制语句,程序书写自由,主要用小写字母表示.它把高级语言的基本结构和语句与低级语言的实用性结合起来. C 语言可以象汇编语言一 ...

  2. [经验] Java 使用 netty 框架, 向 Unity 客户端的 C# 实现通信[2]

    在前一篇文章中, 我们实现了从Java netty 服务端到 unity 客户端的通讯, 但是在过程中也发现有一些问题是博主苦苦无法解决的, 但是还好终于有些问题还是被我找刀方法解决了, 现在把这些解 ...

  3. AJAX技术初级探索

    一 概念 Ajax技术能够实现页面以异步方式从服务器获取信息,无需刷新页面 Ajax的核心技术是XMLHttpRequest对象(XHR) 异步实际就是请求数据的代码不会阻塞页面向下执行 二 原生Aj ...

  4. cc攻击怎么防御,如何防止cc攻击?

    当我们访问一个网站时,如果网站页面越简单,访问速度越快,页面越漂亮,加载速度就越慢,因为要加载更多东西,服务器压力也会比较大.cc攻击就是利用这种弱点,使用大量代理服务器,对网站进行攻击,消耗网站服务 ...

  5. P3378 (模板)并查集

    使用带路径压缩的并查集,不然会TLE AC代码: #include <bits/stdc++.h> #define MP make_pair #define PB push_back #d ...

  6. Hack the box邀请码和注册问题总结

    注意下,有3个坑, 1. 解码方式是随机的,记得看DATA下面提示用哪种 2. post时候可以直接用f12里的console,命令是: $.post('https://www.hackthebox. ...

  7. 使用电脑热点和Fiddler对Android app进行抓包

    如果没有路由器,怎么对app抓包?如果你的电脑可以开热点的话也可以. 打开Fiddler,菜单栏选择Tools->Options->Connections,勾选Allow remote c ...

  8. redis场景分析的很到位

    链接:http://www.zhihu.com/question/19829601/answer/88069207来源:知乎 1. MySql+Memcached架构的问题 实际MySQL是适合进行海 ...

  9. 【代码学习】PYTHON 异常处理

    一.什么是异常 在程序执行过程中可能会影响程序的正常执行,一般情况下,在python无法正常处理程序时就会发生一个异常 当python脚本发生异常时我们需要捕获处理他,否则程序会终止执行 二.异常处理 ...

  10. ES5-Object扩展方法

    1.Object新增一个方法,Object.create();,用来以一个对象为基础创建另一个对象,新建的对象的__proto__指向基础对象  var obj = {name:'maycpou',a ...