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:

  1. 169 5 2

Sample Output 1:

  1. 169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2

Sample Input 2:

  1. 169 167 3

Sample Output 2:

  1. Impossible
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<vector>
  5. using namespace std;
  6. vector<int> ans, temp, fac;
  7. int N, K, P, maxSumfac = -;
  8. void dfs(int index, int cnt, int sum, int sumfac){
  9. if(sum == N && cnt == K){
  10. if(sumfac > maxSumfac){
  11. maxSumfac = sumfac;
  12. ans = temp;
  13. }
  14. return;
  15. }
  16. if(index <= || sum > N || cnt > K)
  17. return;
  18. if(fac[index] + sum <= N){
  19. temp.push_back(index);
  20. dfs(index, cnt + , sum + fac[index], sumfac + index);
  21. temp.pop_back();
  22. }
  23. dfs(index - , cnt, sum, sumfac);
  24. }
  25. int power(int n, int p){
  26. int bas = ;
  27. for(int i = ; i < p; i++)
  28. bas *= n;
  29. return bas;
  30. }
  31. int main(){
  32. scanf("%d %d %d", &N, &K, &P);
  33. int i, num;
  34. for(i = ; ; i++){
  35. num = power(i, P);
  36. if(num > N)
  37. break;
  38. fac.push_back(num);
  39. }
  40. if(num > N)
  41. dfs(i - , , , );
  42. else dfs(i, , , );
  43. if(ans.size() == ){
  44. printf("Impossible");
  45. }else{
  46. printf("%d = %d^%d", N, ans[], P);
  47. int len = ans.size();
  48. for(int i = ; i < len; i++){
  49. printf(" + %d^%d", ans[i], P);
  50. }
  51. }
  52. cin >> N;
  53. return ;
  54. }

总结:

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. HAProxy 日志输出及配置

    正所谓,没有软件敢说没有bug,人无完人,software is  not perfect software.是软件就可能存在bug,那么如果出现bug,我们就要分析对我们业务的影响及可能如何避免bu ...

  2. 手动编写的几个简单的puppet管理配置

    puppet在自动化配置管理方面有很强大的优势,这里就不做过多介绍了,下面记录下几个简单的puppet管理配置: 一.首先在服务端和客户端安装puppet和facter 1)服务端 安装Puppet ...

  3. Scrum Meeting 7

                第七次会议 No_00:工作情况 No_01:任务说明 待完成 已完成 No_10:燃尽图 No_11:照片记录 待更新 No_100:代码/文档签入记录 No_101:出席表 ...

  4. git体会

    刘仙臣个人github链接:http://www.github.com/liuxianchen 这次作业学会了关于git的一些基本操作,学习了到了许多东西,为以后的学习奠定了基础,激发了学习的兴趣.具 ...

  5. java中字符串的排序(1)

    按照前段时间在快速.冒泡等排序的评论中提到是否可以进行字符串的排序,由于最近有考试,时间比较紧,所以今天才实现此功能.此功能是针对一串字符川进行的实现,运行后的结果如下所示: 具体的程序相对较为简单, ...

  6. “数学口袋精灵”App的第三个Sprint计划----开发日记(第十一天12.17)

    项目进度: 基本完成一个小游戏,游戏具有:随机产生算式,判断对错功能.通过轻快的背景音乐,音效,给玩家提供一个良好的氛围.  任务分配: 冯美欣:设计"数学口袋精灵"App图标.整 ...

  7. 剑指offer:变态跳台阶

    题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法.   思路 首先想到的解决方案是根据普通跳台阶题目改编,因为可以跳任意级,所以要 ...

  8. Python 安装 OpenCV 遇到的问题

    从 python下了 opencv_python-3.3.1+contrib-cp36-cp36m-win_amd64.whl [python 3.6  os win10 64  IDE Pychar ...

  9. ppm\℃是什么意思/

    转自http://www.zybang.com/question/b158a106b4e39d8fdb2b93fd3777a00f.html 在基准电压的数据手册里,我们会找到一个描述基准性能的直流参 ...

  10. opencv学习笔记(一)

    摘要:最近要做一个和图像处理有联系的项目,从此走上了学习opencv的道路. 灰度图:2维矩阵 彩色图:3维矩阵 ps:目前大部分设备都是用无符号 8 位整数(类型为 CV_8U)表示像素亮度 Mat ...