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 ...
随机推荐
- Jenkins日常运维笔记-重启数据覆盖问题、迁移、基于java代码发版(maven构建)
之前在公司机房部署了一套jenkins环境,现需要迁移至IDC机房服务器上,迁移过程中记录了一些细节:1)jenkins默认的主目录放在当前用户家目录路径下的.jenkins目录中.如jenkins使 ...
- 实践简单的项目WC
#include<iostream> #include<fstream> #include<string> #include<Windows.h> us ...
- Linux内核分析 笔记八 进程的切换和系统的一般执行过程 ——by王玥
一.进程切换的关键代码switch_to的分析 (一)进程调度与进程调度的时机分析 1.不同类型的进程有不同的调度需求 第一种分类: I/O-bound:频繁地进行I/O,花费很多的时间等待I/O操作 ...
- StringBuffer的append方法比“+”高效
在字符串的连接过程中StringBuffer的效率要比String高: string操作代码: String str = new String("welcome to "); st ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(二)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- about use Vue of methods
methods 处理事件 methods 在vue中处理一些逻辑方面的事情.vue事件监听的方式看上去有点违背分离的传统观念.而实际上vue中所有事件的处理方式和表达式都是严格绑定在当前的视图的vie ...
- vCenter机器查找功能不可用的解决
1. 公司内有多个vCenter机器, 不同的部门 组别 有不通的vCenter来进行管理 最近发现有一个管理比较多的组别的vCenter下载远程登录的情况下总是无法进行高级搜索 造成想找一个虚拟机 ...
- PP-物料清单用户处理手册
1 系统操作时间 1. 新旧物料的替换需有BOM的产生2. BOM中组件发生变化时需有BOM的更改2 系统操作权限角色编码 角色名称Z:PP_PS_007_1000_200 PP主数据维护员 3 适应 ...
- JavaScript中的Date对象在IOS中的“大坑”
在IOS5以上版本(不包含IOS5)中的Safari浏览器能正确解释出Javascript中的 new Date('2013-10-21') 的日期对象. 但是在IOS5版本里面的Safari解释ne ...
- 归并排序详解(python实现)
因为上个星期leetcode的一道题(Median of Two Sorted Arrays)所以想仔细了解一下归并排序的实现. 还是先阐述一下排序思路: 首先归并排序使用了二分法,归根到底的思想还是 ...