线性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. Adobe PS

    1. ctrl + Tab  切换视图窗口 2.shift 拖拽图片,将 2 张图片放在一起 3.切换显示方式 /全屏/带有工具栏 快捷键:F 4. 缩小/放大工具 快捷键: alt + 鼠标滑轮 5 ...

  2. 初学mysql数据库

    package com.conn; import java.sql.Connection; import java.sql.DriverManager; public class Conn { pub ...

  3. 510,position的值,relative和absolute定位原点是

    (absolute:生成绝对定位的元素) position属性用来规定元素的定位类型和方式 ①position:static 默认值,没有定位,元素出现在正常的流中: ②position:fixed  ...

  4. Python - CentOS 下 yum 安装 python3

    1. 概述 CentOS 7 自带 python2(python 以下正文简写为 py, 命令行中依然是 python) 尝试用 yum 安装 py3 2. 环境 os centos7 3. 步骤 1 ...

  5. oracle创建、删除索引等操作

    1.创建索引 create index 索引名 on 表名(列名); 2.删除索引 drop index 索引名; 3.创建组合索引 create index 索引名 on 表名(列名1,,列名2); ...

  6. C#类和对象的理解

    C#是面向对象的开发语言 类:抽象的(模板)不占用内存空间 对象:具体的(真实存在事物)占用内存空间

  7. Codeforces Round #577 (Div. 2) 题解

    比赛链接:https://codeforc.es/contest/1201 A. Important Exam 题意:有\(n\)个人,每个人给出\(m\)个答案,每个答案都有一个分值\(a_i\), ...

  8. 在ios微信客户端遇到的坑,input等错位

    1.判断移动端设备 // 处理iOS 微信客户端弹窗状态下调起输入法后关闭输入法页面元素错位解决办法 var ua = window.navigator.userAgent.toLowerCase() ...

  9. HDU-1702-ACboy needs your help again!(Stack)

    队列和栈的判空都可以用empty #include <bits/stdc++.h> using namespace std; string oper,stru; int T,M,num; ...

  10. HTML中的meta元素

    <meta>元素必须放在<head>标记内,而且必须写在HTML文件前1024B之内 <meta>元素的主要目的是提供有关这份HTML文件的相关信息.例如编码方式, ...