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 = n~1~\^P + ... n~K~\^P

where n~i~ (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 12^2^ + 4^2^ + 2^2^ + 2^2^ + 1^2^, or 11^2^ + 6^2^ + 2^2^ + 2^2^ + 2^2^, 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 { a~1~, a~2~, ... a~K~ } is said to be larger than { b~1~, b~2~, ... b~K~ } if there exists 1<=L<=K such that a~i~=b~i~ for i<L and a~L~>b~L~

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
题意比较好懂,容易想到dfs,但是不能纯回溯,需要剪枝,或者说按照一个非递减的顺序去试每个值,而且判断结果时满足k项就更新,因为从第一层开始,往下dfs,每一层的初始试验值都大于等于上一层的正在试验值,如果遇到跟之前一次结果相同的,序列一定比之前大。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int n,k,p;
vector<int> temp,ans;
int pow_[];///把可能用到的p次方都算出来,要多算一个 这样循环结束 不至于死循环(如果算的pow_[j] 都小于n 那么最大的max(j) + 1对应pow_[j]初始为0
int pow(int t) {
int d = ;
for(int i = ;i < p;i ++) {
d *= t;
}
return d;
}
void dfs(int t,int s,int last) {///last是上一层正在尝试的 ,本次从last开始 达到非递减的目的
if(t >= k) {
if(!s) {
ans = temp;
}
return;
}
while(pow_[last] <= s) {///这里防止死循环 如果last = max(j) 保证pow_[last]能使循环结束
temp.push_back(last);
dfs(t + ,s - pow_[last],last);
temp.pop_back();
last ++;
}
}
int main() {
scanf("%d%d%d",&n,&k,&p);
int j = ,d = ;
while(d <= n) {
pow_[j ++] = d;
d = pow(j);
}
pow_[j] = d;///多算一次 不然判断会死循环
dfs(,n,);
if(ans.empty())printf("Impossible");
else {
printf("%d = %d^%d",n,ans[k - ],p);
for(int i = k - ;i >= ;i --) {
printf(" + %d^%d",ans[i],p);
}
}
}

1103 Integer Factorization (30)(30 分)的更多相关文章

  1. 【PAT】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 ...

  2. PAT 1103 Integer Factorization[难]

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

  3. 1103 Integer Factorization (30)

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

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

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

  5. PAT甲级1103. Integer Factorization

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

  6. 【PAT甲级】1103 Integer Factorization (30 分)

    题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...

  7. 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 ...

  8. 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 ...

  9. PAT (Advanced Level) 1103. Integer Factorization (30)

    暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

随机推荐

  1. vagrant系列四:vagrant搭建redis与redis的监控程序redis-stat

    上一篇php7环境的搭建 真是火爆.仅仅两天时间,就破了我之前swagger系列的一片文章,看来,大家对搭建好开发环境真是情有独钟. 为了訪问量,我今天再来一篇redis的搭建. 当然不能仅仅是red ...

  2. information entropy as a measure of the uncertainty in a message while essentially inventing the field of information theory

    https://en.wikipedia.org/wiki/Claude_Shannon In 1948, the promised memorandum appeared as "A Ma ...

  3. JVM类加载器

    系统中的类加载器 1.BootStrap ClassLoader a.启动ClassLoader b.加载rt.jar 2.Extension ClassLoader a.扩展ClassLoader ...

  4. 网络的分层协议总结(转发:https://www.cnblogs.com/Zhang-wj/p/5907534.html)

    网络的分层协议总结 OSI七层模型OSI 中的层            功能                                                        TCP/IP ...

  5. linux shell 中数组使用方法介绍

    linux shell在编程方面比windows 批处理强大太多,不管是在循环.运算.已经数据类型方面都是不能比較的. 以下是个人在使用时候,对它在数组方面一些操作进行的总结. 1.数组定义 [che ...

  6. mysql练习(待补充)

    2.查询‘生物’课程比‘物理’课程成绩高的所有学生的学号 思路: 获取所有生物课程的人(学号,成绩)-临时表 获取所有物理课程的人(学号,成绩)-临时表 根据学号连接两个临时表: 学号 生物成绩 物理 ...

  7. Python lambda 匿名函数

    lambda [arg1[, arg2, ... argN]]: expression Python使用lambda关键字创造匿名函数.所谓匿名,意即不再使用def语句这样标准的形式定义一个函数.这种 ...

  8. ssh允许root用户登陆

    新的系统无root用户密码,设置root用户密码,修改也是这么修改 sudo passwd root 连续输入两次新密码. 允许root用户登陆: /etc/ssh/sshd_config 找到 Pe ...

  9. java中如何制作可双击执行的程序--jar打包工具的使用

    假定当前工作目录在E盘: 1.带包编译:javac -d c:\ MyMenuDemo.java 2.DOS命令行切换到c盘,注意,这里一般切换到的是用户文件目录,需要手动切换到C盘根目录 >C ...

  10. [原创]java WEB学习笔记25:MVC案例完整实践(part 6)---新增操作的设计与实现

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...