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 ...
随机推荐
- poj The Settlers of Catan( 求图中的最长路 小数据量 暴力dfs搜索(递归回溯))
The Settlers of Catan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1123 Accepted: ...
- BZOJ 3400 [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队:dp【和为f的倍数】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1375 题意: 给你n个数,你可以从中选任意多个,但不能不选.问你所选数字之和为f的倍数 ...
- html中css的三种样式
在html中定义CSS样式的方法有三种,新建CSS文件 使用link 关联 这种是最常用的外部引用样式,第二种讲样式写在 head 头部里面 这种是页面样式 ,第三中样式直接写在行内 style里面 ...
- python基础-元组
操作元组 获取元组中的值 tup1 = ('高数','计算机',2008,2016) tup2 = (1,2,3,4,5,6,7) #和list的一样,同样取下标1,2,3,4的值 print(&qu ...
- DBSCAN 聚类分析
DBSCANCLUSTER DBSCAN(Density-basedspatial clustering ofapplications with noise)Martin.Ester, Hans-Pe ...
- [acm]HDOJ 3082 Simplify The Circuit
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3082 字符串处理+并联电阻公式 //11481261 2014-08-18 16:52:47 Acc ...
- AndyQsmart ACM学习历程——ZOJ3870 Team Formation(位运算)
Description For an upcoming programming contest, Edward, the headmaster of Marjar University, is for ...
- 洛谷 U6931 灯光
题目背景 明天就是校园活动了,小明作为场地的负责人,将一切都布置好了.但是在活动的前几天,校园里的灯却都坏掉了,无奈之下,只好再去买一批灯.但是很遗憾的是,厂家看马上要过年了,就没有在进货了,现在只剩 ...
- Grunt:GruntFile.js
ylbtech-Grunt:GruntFile.js 1.返回顶部 1. module.exports = function (grunt) { grunt.initConfig({ useminPr ...
- thinkpad取消fn键功能
转自:https://bbs.thinkpad.com/thread-1834235-1-1.html 1就是一直觉得fn建自动开启很烦人,于是百度后得到 我们可以 控制面板 -- 键盘--think ...