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. 【刷题】BZOJ 4289 PA2012 Tax

    Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边 ...

  2. 浅谈平衡树splay

    首先splay和treap不一样,treap通过随机数来调整树的形态.但splay不一样,再每插入或操作一次后,你都会把他旋转到根,再旋转过程中树的形态会不断改变,这样来达到均摊效果 常数据大. 来看 ...

  3. 【BZOJ1205】[HNOI2005]星际贸易(动态规划)

    [BZOJ1205][HNOI2005]星际贸易(动态规划) 题面 BZOJ 洛谷 题解 第一问就是一个裸\(dp\),因为什么都不用考虑... 所以设\(f[i][j]\)表示当前停靠在第\(i\) ...

  4. java旋转图片

    /** * 旋转角度 * @param src 源图片 * @param angel 角度 * @return 目标图片 */ public static BufferedImage rotate(I ...

  5. 【codevs2205】等差数列

    题目大意:给定一个长度为 N 的序列,求这个序列中等差数列的个数. 题解:根据题意应该是一道序列计数 dp.设 \(dp[i][j]\) 表示以第 i 项结尾,公差为 j 的等差数列的个数,则状态转移 ...

  6. token的理解

    今天学习了token,它的英文意思是令牌的意思.在我理解即像通行证一样,在用户登录成功系统后,会为这个用户颁发一个token,这样它去其他系统都免登录,因为有了这个令牌. token的生成我们可以用U ...

  7. jdk1.8.0_45源码解读——Map接口和AbstractMap抽象类的实现

    jdk1.8.0_45源码解读——Map接口和AbstractMap抽象类的实现 一. Map架构 如上图:(01) Map 是映射接口,Map中存储的内容是键值对(key-value).(02) A ...

  8. Zookeeper笔记之命令行操作

    $ZOOKEEPER_HOME/bin下的zkCli.sh进入命令行界面,使用help可查看支持的所有命令: 一.节点相关操作 create [-s] [-e] path data acl creat ...

  9. vue中,写在methods里的B方法去调A方法的数据,访问不到?

    今天在写项目的时候,发现了一个京城性忽略的问题,在vue的methods的方法里面定义了两个方法,如下: getTaskList() { api.growthDetails.taskList({ ap ...

  10. requests下载文件并重新上传

    import re import requests from io import BytesIO from django.core.files.uploadedfile import InMemory ...