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 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 (<=400), K (<=N) and P (1<P<=7). The numbers in a line are separated by a space.
Output Specification:
For each case, if the solution exists, output in the format:
N = n1^P + ... nK^P
where ni (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 122 + 42 + 22 + 22 + 12, or 112 + 62+ 22 + 22 + 22, 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 { a1, a2, ... aK } is said to be larger than { b1, b2, ... bK } if there exists 1<=L<=K such that ai=bi for 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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> ans, temp, fac;
int N, K, P, maxSumfac = -;
void dfs(int index, int cnt, int sum, int sumfac){
if(sum == N && cnt == K){
if(sumfac > maxSumfac){
maxSumfac = sumfac;
ans = temp;
}
return;
}
if(index <= || sum > N || cnt > K)
return;
if(fac[index] + sum <= N){
temp.push_back(index);
dfs(index, cnt + , sum + fac[index], sumfac + index);
temp.pop_back();
}
dfs(index - , cnt, sum, sumfac);
}
int power(int n, int p){
int bas = ;
for(int i = ; i < p; i++)
bas *= n;
return bas;
}
int main(){
scanf("%d %d %d", &N, &K, &P);
int i, num;
for(i = ; ; i++){
num = power(i, P);
if(num > N)
break;
fac.push_back(num);
}
if(num > N)
dfs(i - , , , );
else dfs(i, , , );
if(ans.size() == ){
printf("Impossible");
}else{
printf("%d = %d^%d", N, ans[], P);
int len = ans.size();
for(int i = ; i < len; i++){
printf(" + %d^%d", ans[i], P);
}
}
cin >> N;
return ;
}
总结:
1、本题题意:给出N、P、K,要求选出K个数,使得他们分别的P次方再求和等于N。按照降序输出序列,且若有多个答案,选择一次方和最大的一个输出。
2、预处理,计算中肯定需要反复用到一个数的P次方,如果每次用时都计算,显然太慢且重复。可以提前预计算一个数组,i的P次方为 fac[i] 。具体范围应该计算到 i 的P次方大于N的那个i。然后dfs从i - 1开始递减搜索。 在main函数开始的地方不要忘记写预处理的语句。 同样,当前序列的和应该在选择过程中计算,而不是每次都重复计算。
3、注意不要少写递归结束的语句。当结果符合要求时需要return,但不符合要求时也需要return。
4、vector<int> ans, temp;ans、temp一个存全局最优答案,一个存当前答案,两者可以直接赋值,其内容是拷贝。
5、注意index <= 0时也不符合条件,需要加到搜索结束的条件中去。
A1103. Integer Factorization的更多相关文章
- 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 ...
- PAT甲级——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 Kpositiv ...
- PAT A1103 Integer Factorization
线性dfs,注意每次深搜完状态的维护~ #include<bits/stdc++.h> using namespace std; ; vector<int> v,tmp,pat ...
- PAT_A1103#Integer Factorization
Source: PAT A1103 Integer Factorization (30 分) Description: The K−P factorization of a positive inte ...
- 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 ...
随机推荐
- MPI-Hydra Process Managerment Framework
1. 概述2. 执行过程和控制流 官方文档地址:https://wiki.mpich.org/mpich/index.php/Hydra_Process_Management_Framework 1. ...
- FreeCAD源码初步了解
FreeCAD简介 FreeCAD是基于OpenCASCADE的开源CAD/CAE软件,完全开源(GPL的LGPL许可证),官方源码地址,详情可参考维基百科,百度百科等等. 如果要编译FreeCAD, ...
- 后台跑包方法 断开ssh程序也能继续执行的方法screen命令
aircrack-ng -w 字典路径 握手包路径 screen -S 001创建会话 screen -ls 列出窗口列表 screen -r 5位数字 进入会话指令 如果会话恢复不了,则是有可能 ...
- Linux内核分析——第三章 进程管理
第三章 进程管理 3.1 进程 1.进程就是处于执行期的程序:进程就是正在执行的程序代码的实时结果:进程是处于执行期的程序以及相关的资源的总称:进程包括代码段和其他资源. 线程:是在进程中活动的对象. ...
- Doors Breaking and Repairing CodeForces - 1102C (思维)
You are policeman and you are playing a game with Slavik. The game is turn-based and each turn consi ...
- 『编程题全队』Beta 阶段用户使用调查报告
目录 一.项目概述 1.1项目名称 1.2项目简介 1.3项目预期达到目标 1.4项目测试方法 二.项目测试过程 2.1测试对象 2.2测试时长 2.3用户测试反馈 一.项目概述 1.1项目名称 本次 ...
- Maven相关问题解决.docx
1. 问题 2. 原因 出现.lastUpdated结尾的文件的原因:由于网络原因没有将Maven的依赖下载完整,导致. 解决方案: 1.删除所有以.lastUpdate结尾的文件 a)1.切换到ma ...
- 我的集合学习笔记--LinkedList
一,Node节点: /** * 存储元素基本单位 */ public class Node { Object data; Node pre; Node next; public Node(Node p ...
- FICO基础知识(二)
FI中的maser data: COA (Chart Of Account) 科目表 Account 科目 Vendor master dada 供应商主数据 Customer master da ...
- 软件工程_7th weeks
内聚和耦合(学习笔记) 一.内聚 内聚是一个模块内部各成分之间相关联程度的度量.把内聚按紧密程度从低到高排列次序为: 1.偶然内聚:指一个模块内各成分为完成一组功能而组合在一起,它们相互之间即使有关系 ...