线性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. Django视图层、模板层

    过滤器方法 upper 大写方式输出 add 给value加上一个数值 capfirst 第一个字母大写 center 输出指定长度的字符串,把变量居中 cut 删除指定字符串 date 格式化日期 ...

  2. 08day 操作命令以及目录结构

    yum /var/log目录(日志文件)两个重要目录:message--记录系统或服务程序运行状态信息 secure--记录用户登录信息 tail -f 查看日志方法    head 查问文件头部

  3. vue 循环多个标签,点击标签变色,再点击取消,可以同时点击多个

    效果: <div class="relFacilityTitcon"> <i v-for="(item,index) in facilityList&q ...

  4. C9300升级-USB

    1.show ver查看设备的版本 2.一些版本信息的参考 3.准备USB查看其具备的镜像命令:dir usbflash0: 4.复制镜像到设备命令:copy usbflash0:cat9k_iosx ...

  5. 【笔记】Linux进程间同步和进程绑定至特定cpu

    #define _GNU_SOURCE #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> ...

  6. 每天进步一点点------Sobel算子(2)

    转载  http://blog.csdn.net/tianhai110 索贝尔算子(Sobel operator)主要用作边缘检测,在技术上,它是一离散性差分算子,用来运算图像亮度函数的灰度之近似值. ...

  7. 使用Docker搭建Spark集群(用于实现网站流量实时分析模块)

    上一篇使用Docker搭建了Hadoop的完全分布式:使用Docker搭建Hadoop集群(伪分布式与完全分布式),本次记录搭建spark集群,使用两者同时来实现之前一直未完成的项目:网站日志流量分析 ...

  8. winform BackgroundWorker 的用法 - 异步执行

    1.设置 backgroundWorker1.WorkerReportsProgress = true; backgroundWorker1.WorkerSupportsCancellation = ...

  9. animation模块的使用

    1.动画的实现-初探 import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncA ...

  10. 1、json背景

    教程链接 链接:http://pan.baidu.com/s/1mil4M1M 密码:vwsn JSON (JavaScript对象表示法)是一种轻量级的基于文本的开放标准,被设计用于可读的数据交换, ...