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 (≤), K (≤) and P (1). 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 1, or 1, 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 { , } is said to be larger than { , } if there exists 1 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

C++代码如下:

 #include<iostream>
#include<cmath>
#include<vector>
using namespace std; int n, k, p;
vector<int>v;
vector<int>temp,ans;
int sum_g=;
void init() {
int t;
for (int i = ; i < ; i++) {
t = pow(i, p);
if (t <= n)
v.push_back(t);
else break;
}
}
void DFS(int index, int sum, int nowk, int sumk) {
if ( nowk == k && sum == n) {
if (sumk > sum_g) {
sum_g = sumk;
ans = temp;
}
return;
}
if ( sum>n || nowk > k)return;
if (index - >= ) {
temp.push_back(index);
DFS(index , sum + v[index], nowk + , sumk + index);
temp.pop_back();
DFS(index - , sum, nowk, sumk);
}
}
int main() {
cin >> n >> k >> p;
init();
DFS(v.size() - , , , );
if (ans.size() > ) {
cout << n << " = ";
for (int i = ; i < ans.size() - ; i++)
cout << ans[i] << "^" << p << " + ";
cout << ans[ans.size() - ] << "^" << p << endl;
}
else
cout << "Impossible" << endl;
return ;
}

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

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

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

  2. 1103 Integer Factorization (30)

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

  3. PAT 1103 Integer Factorization[难]

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

  4. 1103 Integer Factorization (30)(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 ...

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

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

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

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

    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甲级——1103 Integer Factorization (DFS)

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

  10. PAT甲级1103. Integer Factorization

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

随机推荐

  1. SpringMVC处理ajax请求的跨域问题和注意事项

    .首先要知道ajax请求的核心是JavaScrip对象和XmlHttpRequest,而浏览器请求的核心是浏览器我的个人博客(基于SSM,Redis,Tomcat集群的后台架构) github:htt ...

  2. Square Numbers UVA - 11461(水题)

    #include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...

  3. [USACO4.4]追查坏牛奶Pollutant Control

    题目链接:ヾ(≧∇≦*)ゝ Solution: 第一问很好解决,根据网络流:最大流=最小割定理,我们可以轻松求出. 至于第二问,我们不妨把每一条边乘上一个大于1000的数再加上1. 这样的话,对于最小 ...

  4. bzoj1488[HNOI2009]图的同构

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1488 1488: [HNOI2009]图的同构 Time Limit: 10 Sec  M ...

  5. 前端学习 -- Css -- 选择器的优先级

    当使用不同的选择器,选中同一个元素时并且设置相同的样式时,这时样式之间产生了冲突,最终到底采用哪个选择器定义的样式,由选择器的优先级(权重)决定优先级高的优先显示. 优先级的规则 内联样式 , 优先级 ...

  6. Luogu 2469 [SDOI2010]星际竞速 / HYSBZ 1927 [Sdoi2010]星际竞速 (网络流,最小费用流)

    Luogu 2469 [SDOI2010]星际竞速 / HYSBZ 1927 [Sdoi2010]星际竞速 (网络流,最小费用流) Description 10年一度的银河系赛车大赛又要开始了.作为全 ...

  7. Objective-C 中的协议(@protocol)和接口(@interface)的区别

    Objective-C 中的协议(@protocol),依照我的理解,就是C#, Java, Pascal等语言中的接口(Interface).协议本身不实现任何方法,只是声明方法,使用协议的类必须实 ...

  8. HDU 6156 数位dp

    Palindrome Function Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Ot ...

  9. jsp中的EL和JSTL的关系

    对于JSTL和EL之间的关系,这个问题对于初学JSP的朋友来说,估计是个问题,下面来详细介绍一下JSTL和EL表达式他们之间的关系,以及JSTL和EL一些相关概念! EL相关概念 JSTL一般要配合E ...

  10. SSO系统的实现

    当一个网站系统比较大型的时候,我们通常采用面向服务的编程,采用分布式的编程.各个子系统共同来实现一个大的系统,这时候登录注册功能的实现也面临着一些问题. 一.WHAT? SSO是什么? sso是单点登 ...