PAT A1103
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的更多相关文章
- 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 ...
- PAT A1103 Integer Factorization
线性dfs,注意每次深搜完状态的维护~ #include<bits/stdc++.h> using namespace std; ; vector<int> v,tmp,pat ...
- PAT A1103—DFS
Integer Factorization The K−P factorization of a positive integer N is to write N as the sum of the ...
- PAT_A1103#Integer Factorization
Source: PAT A1103 Integer Factorization (30 分) Description: The K−P factorization of a positive inte ...
- 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 ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- 《转载》PAT 习题
博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...
- PAT Judge
原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...
- PAT/字符串处理习题集(二)
B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...
随机推荐
- OpenVPN部署,实现访问云服务器的内网
本教程不描述如何FQ 一.OpenVPN服务端部署 $ yum -y install net-tools lzo lzo-devel openssl-devel pam-devel gcc gcc-c ...
- 15_Raid及mdadm命令 _LVM
磁盘管理: 机械式硬盘: U盘,光盘,软盘,硬件,磁带 ln [ -s -v ] SRC DEST 硬链接: 1.只能对文件创建,不能应用于目录 2.不能跨文件系统 ...
- C# 说说lock到底锁谁?(1)
写在前面 最近一个月一直在弄文件传输组件,其中用到多线程的技术,但有的地方确实需要只能有一个线程来操作,如何才能保证只有一个线程呢?首先想到的就是锁的概念,最近在我们项目组中听的最多的也是锁谁,如何锁 ...
- IDEA Maven项目的Mybatis逆向工程
IDEA Maven项目的Mybatis逆向工程 1.配置.pom 如果是在多模块开发下,该文件逆向工程要生成的那个模块下的pom文件. <build> <plugins> & ...
- centos7配置consul
下载wget https://releases.hashicorp.com/consul/1.4.2/consul_1.4.2_linux_amd64.zip解压unzip consul_1.4.2_ ...
- java非阻塞NIO和阻塞IO
1 非阻塞NIO和阻塞IO 1.1 定义 阻塞IO:线程被阻塞,去处理一个读取和写入,中间如果有等待时间,则线程被占用,也不能处理其他任务: 非阻塞IO(new I ...
- 小程序分享进入H5动态网页
在要分享的界面加上这段代码 onShareAppMessage: function (options) { var that = this; // var return_url = that.data ...
- expect简单自动交互-用于密码、命令输入
1. 安装expect #yum -y install expect 2. 新建.exp文件,用于ssh交换机 #vi exp.exp #!/bin/expect set f [open ipfile ...
- textarea跟随内容自动伸缩高度实现方案
监听input事件,然后将textarea的style.height设置为最低高度(19px),进而获取到元素的scrollHeight,然后将scroolHeight设置为style.height
- JavaJ2EE相关知识整理
1.Servlet的生命周期 在Web容器中,Servlet主要经历4个阶段 ①.加载Servlet:当Tomcat第一次访问Servlet的时候,Tomcat会负责创建Servle ...