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. ios高级开发之多线程(一)

    1.概念: 多线程(multithreading)到底是什么呢,它是指在软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件的支持,而能够在同一时间执行多个线程,进而提升整体处理性 ...

  2. 用《捕鱼达人》去理解C#中的多线程

    线程是进程中某个单一顺序的控制流,是程序运行中的调度单位,是程序执行流的最小单位,一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成. 线程自己不拥有系统资源,只拥有一点儿在运行中必 ...

  3. 进程部分(IPC机制及生产者消费者模型)和线程部分

    进程部分 一:进程间通信IPC机制:由于进程之间的内存空间是相互隔离的,所以为了进程间的通信需要一个共享的内存空间, 但是共享带来的问题是数据在写的时候就不安全了,所以需要一种机制既有能共享的内存 空 ...

  4. [Linux]安装pyenv

    python多版本管理pyenv 网址:https://blog.csdn.net/yingfeng_yang/article/details/82527321 Ubuntu16.04安装pyenv1 ...

  5. (2)OSi模型

    osi七层模型 #(应用层,表示层,会话层) => 应用层 => 表达一个数据信息 # 传输层 port(端口) tcp/udp协议 硬件设备:四层交换机 四层路由器 # 网络层 ip协议 ...

  6. Hadoop InputFormat 输入文件分片

    1. Mapper 与 Reducer 数量 对于一个默认的MapReduce Job 来说,map任务的数量等于输入文件被划分成的分块数,这个取决于输入文件的大小以及文件块的大小(如果此文件在 HD ...

  7. Abstract与Virtual

    转自 http://www.cnblogs.com/wang7/archive/2012/04/17/2453624.html virtual和abstract都是用来修饰父类的,通过覆盖父类的定义, ...

  8. Notepad++编译和运行C语言 (GCC)

    我们在学习C语言的时候,实际上只需要编译器和编辑器就能开搞了.(初学者过早接触IDE不利于理解程序构建的过程) 在看这篇文章的时候,假设你已经知道如何把GCC配置到环境变量,并且会在命令行/终端下使用 ...

  9. Linux系统优化及基础命令

    1.Linux系统优化及基础命令 2. vim编辑器 vim 操作命令 在命令模式下操作 pageup 往上翻页(重要指数****)pagedown 往下翻页(重要指数****)H 移动到屏幕首行gg ...

  10. Linq语句jion on后指定多个条件

    代码如下: private void FrmMain_Load(object sender, EventArgs e) { var list1 = Supplier.GetSuppliers(); v ...