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<iostream> //深度优先搜索
#include<vector>
#include<math.h>
using namespace std;
int N, P, K, maxsum=-1;
vector<int> v, ans, tempans;
void init(){
int temp=0, index=1;
while(temp<=N){
v.push_back(temp);
temp=pow(index, P);
index++;
}
}
void dfs(int index, int tempsum, int tempk, int facsum){
if(tempsum==N&&tempk==K){
if(facsum>maxsum){
ans=tempans;
maxsum=facsum;
}
return ;
}
if(tempsum>N||tempk>K) return ;
for(int i=index; i>=1; i--){
tempans.push_back(i);
dfs(i, tempsum+v[i], tempk+1, facsum+i);
tempans.pop_back();
}
}
int main(){
cin>>N>>K>>P;
init();
dfs(v.size()-1, 0, 0, 0);
if(maxsum==-1){
cout<<"Impossible"<<endl;
return 0;
}
cout<<N<<" = "<<ans[0]<<"^"<<P;
for(int i=1; i<ans.size(); i++)
cout<<" + "<<ans[i]<<"^"<<P;
return 0;
}

PAT 1103 Integer Factorization的更多相关文章

  1. PAT 1103 Integer Factorization[难]

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

  2. PAT甲级1103. Integer Factorization

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

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

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

  4. 1103 Integer Factorization (30)

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

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

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

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

  7. PAT甲级1103 Integer Factorization【dfs】【剪枝】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 题意: 给定一个数n,要求从1~n中找 ...

  8. PAT甲题题解-1103. Integer Factorization (30)-(dfs)

    该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + … nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. ...

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

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

随机推荐

  1. mysql20170407代码实现

    今天用了一会儿sqlyog,感觉还是会让人懒惰,所以选择了ms-dos环境,感觉不错,原生态,敲着很爽; | test01 | | tree | | world | +---------------- ...

  2. oc67--NSString1

    // // main.m // NSString基本概念 // Foundation框架,苹果有80多个框架,Foundation有125个头文件. #import <Foundation/Fo ...

  3. SignalR -- server push 利器

    实际上关于SignalR的介绍网上有很多,这里不做过多赘述,我们来看下官方网站的描述. [摘录自http://signalr.net/] What is ASP.NET SignalR ASP.NET ...

  4. astgo常见问题(FAQ)知识库

    Q:为什么我在astgo 的一些列表页面看不到右侧顶端的高级功能菜单?R:因为你没有先选择代理商,这些操作都是针对于某个代理商才可以操作的! Q:为什么我无法给astgo 的代理商充值?R:因为你登录 ...

  5. Weblogic 启动慢解决方法

      添加 启动参数  :      -Xms256m -Xmx512m -XX:MaxPermSize=256m 原因 : 实际是JVM在Linux下的bug:他想调用一个随机函数,但是取不到. 暂时 ...

  6. In 7-bit

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3713 题意:给定一个字符串,首先输出这个字符串的长度(以两位的十六进制的形 ...

  7. kindeditor上传文件的使用

    在线富文本编辑器kindeditor配置(.Net Framework 3.5)   下载地址:http://kindeditor.net/down.php 解压放在项目要目录下, 在Bin目录下添加 ...

  8. codevs3162抄书问题(划分型dp)

    3162 抄书问题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description 现在要把M本有顺序的书分给K个人复制(抄写),每 ...

  9. C 语言程序员必读的 5 本书

    你正通过看书来学习C语言吗?书籍是知识的丰富来源.你可以从书中学到各种知识.书籍可以毫无歧视地向读者传达作者的本意.C语言是由 Dennis Ritchie在1969年到1973年在贝尔实验室研发的. ...

  10. 月薪5K和月薪50K的程序员,差距都在哪里?

    毕业两年买房买车,BAT里拼杀年薪百万.这些大神级的传说想必大家都有耳闻. 而渴望成为人生赢家的程序员们也怀揣着这样梦想,纷纷踏入互联网的大门.   假以时日,这些人的差距愈发明显.最直观的就是薪资水 ...