The K−P factorization of a positive integer N is to write N as the sum of the P-th power of Kpositive 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

 #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int n, k, p, maxFacSum = -;//maxFacSum用来记录最大底数之和
vector<int>fac, ans, temp;//最大底数不超过n的数,底数最优数序列,临时存放
void DFS(int index, int nowK, int sum, int facSum)
{
if (sum == n && nowK == k)//统计因素个数
{
if (facSum > maxFacSum)//更优的组合
{
ans = temp;
maxFacSum = facSum;
}
return;
}
if (sum > n || nowK > k)return;//超出限制
if (index - >= )//给出数组小角标的限制
{
temp.push_back(index);//记录数据
DFS(index, nowK + , sum + fac[index], facSum + index);//选
temp.pop_back();//弹出数据
DFS(index - , nowK, sum, facSum);//不选
}
}
int main()
{
cin >> n >> k >> p;
for (int i = ; pow(i, p) <= n; ++i)
fac.push_back(pow(i, p));//初始化底数不超过n的因素
DFS(fac.size() - , , , );//为了得到最大的因素数组,从最后一位开始向前搜索
if (maxFacSum == -)
cout << "Impossible" << endl;//没有找到满足的序列
else
{
cout << n << " = ";
for (int i = ; i < ans.size(); i++)
cout << ans[i] << "^" << p << (i == ans.size() - ? "" : " + ");
}
return ;
}

PAT甲级——A1103 Integer Factorization的更多相关文章

  1. PAT甲级1103. Integer Factorization

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

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

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

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

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

  4. PAT A1103 Integer Factorization (30 分)——dfs,递归

    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 A1103 Integer Factorization

    线性dfs,注意每次深搜完状态的维护~ #include<bits/stdc++.h> using namespace std; ; vector<int> v,tmp,pat ...

  6. A1103. 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 ...

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

  8. PAT 甲级 1113 Integer Set Partition

    https://pintia.cn/problem-sets/994805342720868352/problems/994805357258326016 Given a set of N (> ...

  9. PAT甲级——A1113 Integer Set Partition

    Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets ...

随机推荐

  1. Unity中FSM有限状态机

    什么是FSM FSM 即有限状态机,它是一个状态管理系统,表示一个对象的几种状态在指定条件下转移行为,即随着条件的不断改变内部状态不断地切换. FSM用处或者使用背景 通常使用FSM去实现一些简单的A ...

  2. Python学习笔记(四)——文件永久存储

    文件的永久存储 pickle模块的使用 pickle的实质就是将数据对象以二进制的形式存储 存储数据 pickle.dump(data,file) data表示想要存储的数据元素,file表示要将数据 ...

  3. HTML加载顺序

    一.js执行顺序 //1. 外部引入的js文件,会异步下载并且执行(<script>块中的语句),根据引入的位置会在不同时刻执行 //2.$().ready(function() {}) ...

  4. vue SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

    在使用vue_cli时出现如下错误: 原因是  node  版本太低 应该升级

  5. Neo4j 因果集群搭建及neo4j-java-driver连接

    搭建Neo4j因果集群 1.下载企业版,当前是3,5,9版本 https://neo4j.com/download-center/#enterprise 2.配置,三个核心集群为例 配置文件,conf ...

  6. Java 虚拟机 - 2.3 HotSpot虚拟机对象

    对象的创建 Step1 类加载检查 当发现一条new指令时,检查: 该指令的参数是否能在常量池中定位到一个类的符号引用: 并且检查这个符号引用代表的类是否已经被加载.解析和初始化过.如果没有,那必须先 ...

  7. yii2.0 数据库查询操作

    User::find()->all();    此方法返回所有数据:    User::findOne($id);   此方法返回 主键 id=1  的一条数据(举个例子):    User:: ...

  8. SQL Server DOC

    { https://docs.microsoft.com/zh-cn/sql/sql-server/index?view=sql-server-ver15 }

  9. 【未完成】Jmeter接口自动化测试:参数化设置

    1. 从CSV文件读取参数 创建一个CVS文件,文件第一行不写参数名,直接从参数值开始,每一列代表一个参数 在测试计划或者线程组中,添加一个配置元件-->CSV 数据文件设置 Filename: ...

  10. try-catch 捕捉不到异常

    code: int _tmain(int argc, _TCHAR* argv[]) { cout << "In main." << endl;  //定义 ...