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 ...
随机推荐
- git 远程库和本地库处理
创建git库的方法 第一种方法: 在码云建立一个demo的git库.git clone在本地一个文件夹.之后会出现在.git的目录下方(这是clone而非pull切记分清楚) 而不是在.git的上一层 ...
- Windows 8.1 PLSQL_32连接到RHEL6.1 Oracle10gr2_64
目录 目录 系统环境 连接Oracle Server 系统环境 操作系统 Windows 8.1 RHEL6.1 软件 Oracle10gr2 PL/SQL instantclient-basic-w ...
- 2015 ACM-ICPC 亚洲区上海站 A - An Easy Physics Problem (计算几何)
题目链接:HDU 5572 Problem Description On an infinite smooth table, there's a big round fixed cylinder an ...
- python学习笔记:函数
一.函数是什么 函数一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过程或子程序),在Pasca ...
- git 上传你代码到码云
转载自:http://blog.csdn.net/u013776188/article/details/60867437
- 用Emacs进行Java开发
用Emacs进行Java开发 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} 用 ...
- 【linux】centos6/7 + nginx 利用certbot 申请https证书
没错我又踩坑了.昨晚上搞到十二点半才成功申请.鬼知道OJ服务器是个什么渣渣. 早上才算正式弄好,中间也学了不少东西,记录一下.这次是http转https,所以默认的还是只有80端口. 请务必确保自己的 ...
- windows上测试网络数据跳转路径
今天遇到一个有关路由方面的问题,所以向一位CCIE大神问了点问题.就是关于ip ping包出了pc之后怎么走的. 然后告诉我了这么一个命令: Tracert(跟踪路由)是路由跟踪实用程序,用于确定 I ...
- JAXB读写xml
一.注解 在Java模型中的创建与 xml 文件对应的节点和属性需要用注解来表示 @XmlRootElement 作用:将一个Java类映射为一段XML的根节点参数: name 定义这个根节点 ...
- 八、结构模式之组合(Composite)模式
组合模式属于对象的结构模式,有时又叫做部分-整体模式,组合模式将对象组织到树结构中,可以用来描述整体与部分的联系.其可以使客户端将单纯元素和组合元素同等对待. 当需求中是体现部分与整体层次的结构时,以 ...