PAT_A1103#Integer Factorization
Source:
Description:
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 thei-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 ai=bifor i<L and aL>bL.
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
Keys:
Attention:
- 不同的编译环境pow函数的精度不同,PAT跑的数据是对的,但我电脑上跑出来是错的,可以自己写一个
 
Code:
/*
time: 2019-07-02 18:55:08
problem: PAT_A1103#Integer Factorization
AC: 18:08 题目大意:
将整数N分解为以P为指数的K个因式的和
输入:
正整数N<=400,因式个数K,指数1<P<=7
输出:
按底数递减,
若不唯一,打印底数和最大的一组,
若不唯一,打印字典序较大的一组 基本思路:
深度优先搜索,至第K层时若存在解,则选择最优解
*/
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
int k,n,p,optValue=;
vector<int> fac,temp,ans; void DFS(int index, int numK, int sum, int sumFac)
{
if(numK==k && sum==n && sumFac>optValue)
{
optValue = sumFac;
ans = temp;
}
if(numK>=k || sum>=n || index<=)
return;
temp.push_back(index);
DFS(index,numK+,sum+fac[index],sumFac+index);
temp.pop_back();
DFS(index-,numK,sum,sumFac);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d%d%d", &n,&k,&p);
for(int i=; pow(i,p)<=n; i++){
fac.push_back(pow(i,p));
}
DFS(fac.size()-,,,);
if(ans.size() == )
printf("Impossible");
else
{
printf("%d = %d^%d", n,ans[],p);
for(int i=; i<ans.size(); i++)
printf(" + %d^%d", ans[i],p);
} return ;
}
PAT_A1103#Integer Factorization的更多相关文章
- PAT 1103 Integer Factorization[难]
		
1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...
 - PAT甲级1103. Integer Factorization
		
PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...
 - PAT甲级——1103 Integer Factorization (DFS)
		
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...
 - 1103 Integer Factorization (30)
		
1103 Integer Factorization (30 分) The K−P factorization of a positive integer N is to write N as t ...
 - 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 ...
 - 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 ...
 - 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 ...
 - 【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 ...
 - 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 ...
 
随机推荐
- java BufferSegment
			
package org.rx.util; import java.util.function.Consumer; import static org.rx.core.Contract.require; ...
 - js判断变量未定义
			
js判断变量未定义 控制台输出未定义变量a会报错: 我们打印出a的数据类型是: 我们可以看到未定义变量的数据类型是 "undefined" 所以判断js变量是否未定义的方法就是 t ...
 - spring boot MVC
			
1 spring boot的用途 第一,spring boot可以用来开发mvc web应用. 第二,spring boot可以用来开发rest api. 第三,spring boot也可以用来开发w ...
 - 深入JAVA虚拟机笔记-垃圾收集器与内存分配策略
			
第三章:垃圾收集器与内存分配 问题:1.哪些内存需要回收 2.什么时候回收 3.怎么回收 回收方法区:
 - 一张图搞清楚Java异常机制
			
下面是Java异常类的组织结构,红色区域的异常类表示是程序需要显示捕捉或者抛出的. Throwable Throwable是Java异常的顶级类,所有的异常都继承于这个类. Error,Excepti ...
 - CF1220F
			
CF1220F 把整棵树分成1的左边和1的右边两部分 最优情况两边子树深度的差一定可以是一 如果还可以是2,也可以通过把多的那一边的点往另一边移使他变成1 如果往一个端点加点,一定不会使这一边变优,也 ...
 - 【DRP】採用dom4j完毕XML文件导入数据库
			
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/lantingxv_jing/article/details/37762523 xml文件在如 ...
 - 小部分安卓手机 reload 等方法不执行
			
自己解析 url 来赋值刷新页面 方法如下:// location.href function updateUrl(url, key) { var key = (key || 't') + ...
 - [机器学习][face recognition] 一个视频人脸识别实现
			
开发环境和用到的库: Ubuntu jupyter notebook(python3.6) OpenCV Dlib face_recognition 实现效果如下(视频截图): #coding=utf ...
 - netstat 指令
			
netstat 指令将所有的网络端口监听情况进行罗列 语法 netstat -tuln 几个常见的服务端口 例 通过grep 查看端口来获得上面的服务是否开启,并给予提示 1 #!/bin/bas ...