The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K-P factorization of N for any positive integers N, K and P.

Input Specification:

Each input file contains one test case which gives in a line the three positive integers N (<=400), K (<=N) and P (1<P<=7). The numbers in a line are separated by a space.

Output Specification:

For each case, if the solution exists, output in the format:

N = n1^P + ... nK^P

where ni (i=1, ... K) is the i-th factor. All the factors must be printed in non-increasing order.

Note: the solution may not be unique. For example, the 5-2 factorization of 169 has 9 solutions, such as 122 + 42 + 22 + 22 + 12, or 112 + 62+ 22 + 22 + 22, or more. You must output the one with the maximum sum of the factors. If there is a tie, the largest factor sequence must be chosen -- sequence { a1, a2, ... aK } is said to be larger than { b1, b2, ... bK } if there exists 1<=L<=K such that ai=bi for i<L and aL>bL

If there is no solution, simple output "Impossible".

Sample Input 1:

169 5 2

Sample Output 1:

169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2

Sample Input 2:

169 167 3

Sample Output 2:

Impossible
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> ans, temp, fac;
int N, K, P, maxSumfac = -;
void dfs(int index, int cnt, int sum, int sumfac){
if(sum == N && cnt == K){
if(sumfac > maxSumfac){
maxSumfac = sumfac;
ans = temp;
}
return;
}
if(index <= || sum > N || cnt > K)
return;
if(fac[index] + sum <= N){
temp.push_back(index);
dfs(index, cnt + , sum + fac[index], sumfac + index);
temp.pop_back();
}
dfs(index - , cnt, sum, sumfac);
}
int power(int n, int p){
int bas = ;
for(int i = ; i < p; i++)
bas *= n;
return bas;
}
int main(){
scanf("%d %d %d", &N, &K, &P);
int i, num;
for(i = ; ; i++){
num = power(i, P);
if(num > N)
break;
fac.push_back(num);
}
if(num > N)
dfs(i - , , , );
else dfs(i, , , );
if(ans.size() == ){
printf("Impossible");
}else{
printf("%d = %d^%d", N, ans[], P);
int len = ans.size();
for(int i = ; i < len; i++){
printf(" + %d^%d", ans[i], P);
}
}
cin >> N;
return ;
}

总结:

1、本题题意:给出N、P、K,要求选出K个数,使得他们分别的P次方再求和等于N。按照降序输出序列,且若有多个答案,选择一次方和最大的一个输出。

2、预处理,计算中肯定需要反复用到一个数的P次方,如果每次用时都计算,显然太慢且重复。可以提前预计算一个数组,i的P次方为 fac[i] 。具体范围应该计算到 i 的P次方大于N的那个i。然后dfs从i - 1开始递减搜索。 在main函数开始的地方不要忘记写预处理的语句。 同样,当前序列的和应该在选择过程中计算,而不是每次都重复计算。

3、注意不要少写递归结束的语句。当结果符合要求时需要return,但不符合要求时也需要return。

4、vector<int> ans, temp;ans、temp一个存全局最优答案,一个存当前答案,两者可以直接赋值,其内容是拷贝。

5、注意index <= 0时也不符合条件,需要加到搜索结束的条件中去。

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甲级——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 ...

  3. PAT A1103 Integer Factorization

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

  4. PAT_A1103#Integer Factorization

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

  5. PAT 1103 Integer Factorization[难]

    1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...

  6. PAT甲级1103. Integer Factorization

    PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...

  7. PAT甲级——1103 Integer Factorization (DFS)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...

  8. 1103 Integer Factorization (30)

    1103 Integer Factorization (30 分)   The K−P factorization of a positive integer N is to write N as t ...

  9. 1103. Integer Factorization (30)

    The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

随机推荐

  1. ASP.NETZERO 开发者指南-目录篇

    前面的话 此教程适用于 ASP.NET MVC 5.x & Angularjs 1.x  的ABP框架(收费需要授权) 所以有能力的朋友还是希望你们多多支持 土牛.购买链接:https://w ...

  2. ABP+AdminLTE+Bootstrap Table权限管理系统第六节--abp控制器扩展及json封装以及6种处理时间格式化的方法

    返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期 一,控制器AbpController 说完了Swagger ui 我们再来说一下abp对控制器的处理和json的封 ...

  3. Jmeter(非GUI模式)教程

    前言 使用非 GUI 模式,即命令行模式运行 JMeter 测试脚本能够大大缩减所需要的系统资源.优点如下:1.节约系统资源:无需启动界面,节约系统资源 2.便捷快速:仅需启动命令行,输入命令便可执行 ...

  4. 洛谷P1004 方格取数-四维DP

    题目描述 设有 N \times NN×N 的方格图 (N \le 9)(N≤9) ,我们将其中的某些方格中填入正整数,而其他的方格中则放入数字 00 .如下图所示(见样例): A 0 0 0 0 0 ...

  5. B. Heaters Div3

    链接 [http://codeforces.com/contest/1066/problem/B] 分析 具体看代码,贪就完事了 代码 #include<bits/stdc++.h> us ...

  6. 结对项目——Core设计与实现

    写在前面:关于结对编程 结对编程我一直认为是一种非常好的合作方式,他的形式主要是由一个人负责代码编写,另一个人则在一旁即时对写下的代码进行审查,这样可以大大减少代码实现方面的错误. 这次我的结对伙伴是 ...

  7. Week 2 代码规范

    Question 1: 这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. My opinion: 我认为恰恰相反,这个可以提高人们的开发效率. 在团队合作当中,如果 ...

  8. Daily Scrumming* 2015.12.22(Day 14)

    一.团队scrum meeting照片 二.成员工作总结 姓名 任务ID 迁入记录 江昊 任务1112 无 任务说明 今天没有写前端界面,而是完成了跨域请求的实现以及用户实名认证API 前后端大部分数 ...

  9. ubuntu解压zip文件

    step1 # 安装解压软件 sudo apt-get install unzip step # 2 解压文件 unzip xxxxx.zip

  10. 小项目分析之C++ 实现模拟银行排队

      一.问题定义与分析 问题定义 •要解决的问题——银行一天之内的: 1.总客户数 2.客户总逗留时间 3.客户平均逗留时间 问题分析 •新来的人找个短的队伍,站在队尾开始排队 •排在队头的人可以办理 ...