PAT甲级——1103 Integer Factorization (DFS)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720
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 (≤), 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 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 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
题目大意:将一个正整数N分解成K个正整数的P次方和,在多个结果里面找出因子之和最大的,若因子之和相同,字典序大的为答案。
思路:主要是DFS的思想,建立一个数组F,用来储存 1~m的P次方,m^P为≤N的最大正整数。find()里面传入四个变量,n为当前find()里面的for循环次数;cnt初始值为K,cnt=0作为递归的边界;tmpSum储存因子之和;sum是总和,sum=N才是符合条件的备选答案~
下一层的递归里的n总是小于等于上一层递归里的n,所以保证了字典序,不需要画蛇添足地写compera函数来筛选答案了(一开始就是因为这个操作导致测试点2答案错误),若无必要,勿增操作。
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int N, K, P, m, fSum = -;
vector <int> ans, F, tmpA;
void find(int n,int cnt, int tmpSum, int sum); int main()
{
scanf("%d%d%d", &N, &K, &P);
int i = ;
F.push_back();
while () {
int x = pow(i, P);
if (x > N)
break;
else {
F.push_back(x);
i++;
}
}
m = F.size() - ;
find(m, K, , );
if (ans.empty()) {
printf("Impossible\n");
return ;
}
printf("%d =", N);
for (int i = ; i < K; i++) {
printf(" %d^%d", ans[i], P);
if (i < K - ) {
printf(" +");
}
}
printf("\n");
return ;
}
void find(int n, int cnt, int tmpSum, int sum) {
if(n==) return;
if (cnt == ) {
if (fSum < tmpSum) {
if (sum == N) {
ans = tmpA;
fSum = tmpSum;
}
}
return;
}
for (int i = n; i > ; i--) {
if (sum <= N) {
tmpA.push_back(i);
find(i, cnt - , tmpSum + i, sum + F[i]);
tmpA.pop_back();
}
}
}
PAT甲级——1103 Integer Factorization (DFS)的更多相关文章
- 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】【剪枝】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 题意: 给定一个数n,要求从1~n中找 ...
- 【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 ...
- 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 1103 Integer Factorization[难]
1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...
- 1103 Integer Factorization (30)
1103 Integer Factorization (30 分) The K−P factorization of a positive integer N is to write N as t ...
- PAT甲题题解-1103. Integer Factorization (30)-(dfs)
该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + … nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. ...
- 【PAT甲级】1103 Integer Factorization (30 分)
题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...
- PAT 1103 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 ...
随机推荐
- PYTHON 爬虫笔记七:Selenium库基础用法
知识点一:Selenium库详解及其基本使用 什么是Selenium selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium ...
- CSS样式命名整理
CSS样式命名整理 页面结构 容器: container/wrap 整体宽度:wrapper 页头:header 内容:content 页面主体:main 页尾:footer 导航:nav 侧栏:si ...
- TF-IFD算法及python实现关键字提取
TF-IDF算法: TF:词频(Term Frequency),即在分词后,某一个词在文档中出现的频率. IDF:逆文档频率(Inverse Document Frequency).在词频的基础上给每 ...
- linux命令学习笔记(47):iostat命令
Linux系统中的 iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视. 它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况. ...
- POJ2155 Matrix(二维树状数组||区间修改单点查询)
Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row an ...
- 【Python】File IO
1.numpy.genfromtxt() numpy.genfromtxt() CSV文件很容易被numpy类库的genfromtxt方法解析 2.
- BZOJ2877:[NOI2012]魔幻棋盘
浅谈树状数组与主席树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://lydsy.com/JudgeOnline/problem. ...
- POJ1797(dijkstra求最短最长边)
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 26442 Accepted: ...
- 好文章!转载嵌入式LINUX
整理了嵌入式linux学习路线供参考,希望对您有所参考价值! 一.linux入门 目前嵌入式主要开发环境有 Linux.Wince等:Linux因其开源.开发操作便利而被广泛采用.而Linux操作系统 ...
- Cocos2d-x v3.x and Xcode 6.x with ARM 64 Architecture
转自:http://becomingindiedev.blogspot.com.es/2014/12/cocos2d-x-v3x-and-xcode-6x-with-arm-64.html Hi! W ...