PAT A1103

标签(空格分隔): PAT


解题思路: DFS

#include <cstdio>
#include <vector>
using namespace std; int n, k, p;
int maxFacSum = -1; //记录最大因子和
vector<int> ans, temp, fac; int power(int x) {
int ans = 1;
for(int i = 0; i < p; i++) {
ans *= x;
}
return ans;
} void init() {
int i = 0, temp = 0; //初始化fac,把因子的平方存入其中
while(temp <= n) {
fac.push_back(temp);
temp = power(++i);
}
} void DFS(int index, int nowK, int sum, int facSum) {
if(nowK == k && sum == n) { //dfs终止条件
if(facSum > maxFacSum) { //目前的因子和大于最大因子和
maxFacSum = facSum;
ans = temp; //更新答案vector
}
return;
}
if(nowK > k || sum > n) return; //此时不可能满足条件,直接返回
if(index - 1 >= 0) {
temp.push_back(index);
DFS(index, nowK + 1, sum + fac[index], facSum + index); //选择index
temp.pop_back();
DFS(index - 1, nowK, sum, facSum); //不选index
}
} int main() {
scanf("%d%d%d", &n, &k, &p);
init();
DFS(fac.size() - 1, 0, 0, 0);
if(maxFacSum == -1) printf("Impossible\n");
else {
printf("%d = %d^%d", n, ans[0], p);
for(int i = 1; i < ans.size(); i++) {
printf((" + %d^%d"), ans[i], p);
}
}
return 0;
}

PAT A1103的更多相关文章

  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 A1103 Integer Factorization

    线性dfs,注意每次深搜完状态的维护~ #include<bits/stdc++.h> using namespace std; ; vector<int> v,tmp,pat ...

  3. PAT A1103—DFS

     Integer Factorization The K−P factorization of a positive integer N is to write N as the sum of the ...

  4. PAT_A1103#Integer Factorization

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

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

  6. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  7. 《转载》PAT 习题

    博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...

  8. PAT Judge

    原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...

  9. PAT/字符串处理习题集(二)

    B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...

随机推荐

  1. vue全局组件-父子组件传值

    全局组件注册方式:Vue.component(组件名,{方法}) demo: 子组件:upload.vue <template> <div > <div class=&q ...

  2. java程序可以跨平台运行的原因

    java有虚拟机(JVM),JAVA程序不是直接在电脑上运行的,是在虚拟机上进行的,每个系统平台都是有自己的虚拟机(JVM),所以JAVA语言能跨平台. 1, java代码不是直接运行在CPU上,而是 ...

  3. SpringBoot之配置文件加载位置

    1.SpringBoot启动会扫描application.properties或者application.yml文件作为springboot的配置文件.默认创建项目生成application.prop ...

  4. docker容器配置nginx负载均衡 -----加权

    首先要准备三个nginx的容器: 第二个容器: 第三个容器: 进入第一个容器  要配置的容器(docker exec -it 容器名 /bin/bash) vi /usr/local/nginx/co ...

  5. 前端使用JS-SDK上传图片(文件)到七牛

    Qiniu-JavaScript-SDK (下文简称为 JS-SDK)适用于 :IE11.Edge.Chrome.Firefox.Safari 等浏览器,基于七牛云存储官方 API 构建,其中上传功能 ...

  6. ibatis项目应用

    创建数据库表: create table u_student( student_id number 16, student_name varchar2(108), student_code numbe ...

  7. AOP之配置文件实现

    看了http://www.cnblogs.com/xrq730/p/4919025.html这篇文章之后自己跟着做了一遍,有需要可以去看原文,比我写的更详细. AOP AOP(Aspect Orien ...

  8. Vagrant 入门指南

    https://blog.csdn.net/qianghaohao/article/details/80038096 https://blog.csdn.net/happyhorizion/artic ...

  9. python--多线程多进程

    一.进程 对于操作系统来说,一个任务就是一个进程(Process),比如打开一个浏览器就是启动一个浏览器进程,打开一个记事本就启动了一个记事本进程,打开两个记事本就启动了两个记事本进程.进程是很多资源 ...

  10. dp背包问题

    0-1背包 1.问题定义: 给定n种物品和背包.物品i的重量是wi,价值是vi,每种物品只有一个,背包容量为C.问:应该如何选择装入背包的物品,使得装入背包中的物品总值最大. 2.算法思路: 选择装入 ...