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 ...
随机推荐
- html相关操作点
$( "#members li a" ).bind( "click", function( e ) {} ); $( document ).live( &quo ...
- 【转】Java类MemoryUsage查看虚拟机的使用情况
原文地址:https://www.cnblogs.com/xubiao/p/5465473.html Java类MemoryUsage,通过MemoryUsage可以查看Java 虚拟机的内存池的内存 ...
- Win10 打开MSDTC
标签:log com http it sp src c bs io 1,Win+R 打开运行窗口,输入 dcomcnfg,打开组件服务窗口 2,在组件服务 catalo ...
- 微信小程序观察者模式 observers
const app = getApp(); const request = require('../../../utils/request.js'); Component({ options: { m ...
- XSS漏洞防护
主要是添加黑名单进行拦截 public class XSSFilter implements Filter { private final Log logger = LogFactory.getLog ...
- 推荐几个顶级的IT技术公众号,坐稳了!
提升自我的路很多,学习是其中最为捷径的一条.丰富的知识提升的不仅仅是你的阅历,更能彰显你的气质,正如古人云:"文质彬彬是君子." 今天为大家整理了10个公众号,分别为多领域,多角度 ...
- idea部署tomcat项目时,在项目里打断点不能拦截
以下内容都是基于我自己的项目(如未解决道友们的问题,请别介意) idea部署tomcat项目时,在项目里打断点不能拦截,工作中遇到的问题,记录一下(tomcat6) 当项目的以下内容配置完毕后,启动项 ...
- css 实现水波纹,波浪动画效果
<div class="wave"> 水波纹效果 <div class="wave1"></div> <div cla ...
- 2018-2-13-安装-aria2
title author date CreateTime categories 安装 aria2 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23:3 + ...
- Java连接ActiveMQ代码示例(Producer和Consumer)
import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; ...