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. Luogu4783 【模板】矩阵求逆(高斯消元)

    对矩阵进行高斯消元直至消为单位矩阵,并在另一个单位矩阵上对其做同样的操作即可. 模意义下的高斯消元可以直接计算系数来避免整行的辗转相除. 还不知道有什么用. #include<iostream& ...

  2. hdwiki 附件上传大小设置

    1.进入服务器,找到 wamp 路径,搜索php.ini   ,参考   x:\wamp\usr\local\php\php.ini 文本打开,搜索  post_max_size  =  设置成要的值 ...

  3. 【bzoj3172】 Tjoi2013—单词

    http://www.lydsy.com/JudgeOnline/problem.php?id=3172 (题目链接) 题意 $n$个单词组成文本,问每个单词在文本中出现了几次. Solution 题 ...

  4. Eclipse中创建java类的时候自动设置作者信息和创建时间

    window—>preferences—>Java—>Code Stype—>Code Templates Code—>New Java files 点击Edit ${f ...

  5. 缓存面板获取之前页面选中的数据Objuid的方法

    String partUid = request.getParameter("contextInstanceUid"); contextInstanceUid是存在总线总的键名,存 ...

  6. RESTful记录-RESTful介绍

    RESTful Web服务是基于REST架构的Web服务.在REST架构一切都是一种资源. RESTful Web服务是轻量级的,高度可扩展性和可维护性,并且非常常用于创建基于API的Web应用程序. ...

  7. Solr记录-solr基础内容

    Solr架构(体系结构) 在本章中,我们将讨论Apache Solr的架构. 下图显示了Apache Solr的体系结构的框图. Solr架构 - 构件块以下是Apache Solr的主要构建块(组件 ...

  8. 使用WebHelper调用Asp.net WebAPI

    1.WebHelper using System; using System.Collections.Generic; using System.Collections.Specialized; us ...

  9. Linux下sh文件运行及桌面环境双击运行sh文件

    sh文件运行: 1.修改为可执行权限: chmod u+x hello.sh 2.运行 ./hello.sh 3.不使用可执行权限修改,用sh直接运行 sh ./hello.sh 桌面环境双击运行sh ...

  10. hadoop - hdfs 基础操作

    hdfs --help # 所有参数 hdfs dfs -help # 运行文件系统命令在Hadoop文件系统 hdfs dfs -ls /logs # 查看 hdfs dfs -ls /user/ ...