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 ...
随机推荐
- Hive- Hive安装
Hive安装 1.1下载Hive安装包 官网:http://hive.apache.org/downloads.html 个人建议到这里下载:http://apache.forsale.plus/ 1 ...
- python操作cad
from pyautocad import Autocad # 自動連接上cad,只要cad是開着的,就創建了一個<pyautocad.api.Autocad> 對象.這個對象連接最近打開 ...
- 分享知识-快乐自己:SpringMVC 底层执行原理解析
底层实现原理图: 观看底层代码: 1):打开 web.xml 文件 2):按住 Ctrl + 鼠标左键 进入底层查看源码 3):按住 Ctrl+o 找到对应的方法doDispatch 5): ...
- Composer基础应用1
先唠叨唠叨一些琐碎的事.本人最早从事.Net开发,后来处于好奇慢慢转到了php,因为.net从一早就使用了命名空间(反正从我使用就存在这玩意了),所以在转php时很自然的就使用了命名空间,但是在使用过 ...
- laravel 在apache或nginx的配置
laravel 下载后,如何运行起来呢,根据自己的应用,记录了几个关键点: 1.apache 配置: 打开http.conf文件,将mod_rewrite前面的#去掉(启用重写模块): 2.nginx ...
- oracle11g登录等问题
一.oracle 11g登录服务开启 成功安装Oracle 11g后,共有7个服务,这七个服务的含义分别为:1. Oracle ORCL VSS Writer Service:Oracle卷映射拷贝写 ...
- python mysqldb 教程
MySQL Python 教程 (1)下面是在Python中使用MySql数据库的教程,涵盖了Python对MySql的基础操作,主要采用了MySQLdb模块,下面的代码都是基于Ubuntu Linu ...
- 微信小程序开发之带搜索记录的搜索框
实现功能:点击搜索框,有搜索记录时以下拉菜单显示,点击下拉子菜单,将数据赋值到搜索框,点击搜索图标搜索,支持清空历史记录,可手动输入和清空查询关键字, UI: wxml: <!--查询历史记录数 ...
- Web.config中的设置 forms 中的slidingExpiration的设置
在ASP.NET 网站中,使用 Forms Authentication时,一般的设置是如下的: <authentication mode="Forms"> <f ...
- FZU - 2218 Simple String Problem(状压dp)
Simple String Problem Recently, you have found your interest in string theory. Here is an interestin ...