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. 【转】树莓派Raspberry Pi - 还原已经装过系统的TF卡

    想给树莓派换个系统的话,需要先把已经装过系统的TF卡进行还原,这里使用最简单粗暴无脑的方法: 1,下载安装Win32 Disk Imager(一般已经装过一次系统后,这个东西都有) 2,下载boots ...

  2. CF438D The Child and Sequence(线段树)

    题目链接:CF原网  洛谷 题目大意:维护一个长度为 $n$ 的正整数序列 $a$,支持单点修改,区间取模,区间求和.共 $m$ 个操作. $1\le n,m\le 10^5$.其它数均为非负整数且 ...

  3. Python 使用multiprocessingm模块创建多进程

    from multiprocessing import Process def run_proc(name): print("子进程Process %s(%s)运行..."%(na ...

  4. 前端学习 -- Css -- overflow

    子元素默认是存在于父元素的内容区中,理论上讲子元素的最大可以等于父元素内容区大小.如果子元素的大小超过了父元素的内容区,则超过的大小会在父元素以外的位置显示,超出父元素的内容,我们称为溢出的内容.父元 ...

  5. 【CC】Batman and Tree

    Portal --> CC Batman and Tree Solution 一开始看到很懵..感觉无从下手(因为自己太菜了qwq) ​ 膜拜了题解之后发现好像并没有想象的那么复杂qwq ​ 其 ...

  6. java Random.nextInt()方法

    转: java Random.nextInt()方法 lic int nextInt(int n) 该方法的作用是生成一个随机的int值,该值介于[0,n)的区间,也就是0到n之间的随机int值,包含 ...

  7. Linux系统上查找已安装软件的路径

    在Linux系统上查找已安装软件路径的命令,以查找pcre的安装路径为例: [root@localhost doc]# rpm -ql pcre /lib64/libpcre.so. /lib64/l ...

  8. linux c 编程 ------ 获取时间,计算程序执行时间

    #include <time.h> #include <stdio.h> #include <unistd.h> int main(int argc, char a ...

  9. 移动端Web页面问题解决方案

    1.安卓浏览器看背景图片,有些设备会模糊. 用同等比例的图片在PC机上很清楚,但是手机上很模糊,原因是什么呢? 经过研究,是devicePixelRatio作怪,因为手机分辨率太小,如果按照分辨率来显 ...

  10. (转)flask的context机制

    本文转自:https://blog.tonyseek.com/post/the-context-mechanism-of-flask/ 作者:无知的 TonySeek 注意:本文仅仅作为个人mark, ...