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. 剑指offer 11:二进制中 1 的个数

    题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 解题代码 法一: public class Solution { public int NumberOf1(int n) { ...

  2. 《Java8实战》读书笔记

    个人感悟: 1.lambda表达式,补充了JAVA在面向对象之外,面向过程的一面.在写面向过程代码的时候更方面了,甚至可以利用代码来做类似数学公式的运算(P64) 2.流,对集合的操作,就像用SQL对 ...

  3. setTimeout setInterval 计时器

    setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. 返回值:返回一个 ID(数字),可以将这个ID传递给 clearTimeout() 来取消执行. 案例: 点击按钮开始,停止时 ...

  4. jmockit 模拟同一个函数多次调用每次返回不同结果

    new Expectations(){{ calendarService.getGeneralCalendar((Date)any); returns(null, new AbrahamResult& ...

  5. spring boot2.0(一 ) 基础环境搭建

    1.基础配置 开发环境:window jdk版本:1.8(spring boot2.0最低要求1.8) 开发工具:eclipse 构建方式:maven3 2.POM配置文件 <project x ...

  6. C语言实验一(3)

    #include<stdio.h> #include<math.h> int main() { float x,y; scanf("%f,%f",& ...

  7. jdbc、jpa、spring data jpa、hibernate、mybatis之间的关系及区别

    基础概念 jdbc(Java DataBase Connectivity)是java连接数据库操作的原生接口.JDBC对Java程序员而言是API,对实现与数据库连接的服务提供商而言是接口模型.作为A ...

  8. 利用scrapy-client 发布爬虫到远程服务端

    远程服务端Scrapyd先要开启 远程服务器必须装有scapyd,并开启. 这里远程服务开启的端口和ip: 192.166.12.80:6800 客户端配置和上传 先修爬虫项目文件scrapy.cfg ...

  9. 使用js如何设置、获取盒模型的宽和高

    第一种: dom.style.width/height 这种方法只能获取使用内联样式的元素的宽和高. 第二种: dom.currentStyle.width/height 这种方法获取的是浏览器渲染以 ...

  10. restore not found的错误(问题2)

    最近在写gan,那么就牵扯到在一个session中加载两个图,restore的时候会有问题.如这篇文章写的(http://blog.csdn.net/u014659656/article/detail ...