本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720

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 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 a​i​​=b​i​​for i<L and a​L​​>b​L​​.

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)的更多相关文章

  1. PAT甲级1103. Integer Factorization

    PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...

  2. PAT甲级1103 Integer Factorization【dfs】【剪枝】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 题意: 给定一个数n,要求从1~n中找 ...

  3. 【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 ...

  4. 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 ...

  5. PAT 1103 Integer Factorization[难]

    1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...

  6. 1103 Integer Factorization (30)

    1103 Integer Factorization (30 分)   The K−P factorization of a positive integer N is to write N as t ...

  7. PAT甲题题解-1103. Integer Factorization (30)-(dfs)

    该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + … nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. ...

  8. 【PAT甲级】1103 Integer Factorization (30 分)

    题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...

  9. 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 ...

随机推荐

  1. Spark- ERROR Shell: Failed to locate the winutils binary in the hadoop binary path java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.

    运行 mport org.apache.log4j.{Level, Logger} import org.apache.spark.rdd.RDD import org.apache.spark.{S ...

  2. z+f profiler 9012

    角度分辨率/角度精度 0.0088°/0.02°RMS

  3. POJ 2151 Check the difficulty of problems:概率dp【至少】

    题目链接:http://poj.org/problem?id=2151 题意: 一次ACM比赛,有t支队伍,比赛共m道题. 第i支队伍做出第j道题的概率为p[i][j]. 问你所有队伍都至少做出一道, ...

  4. python基础-正则1

    什么是正则表达式? 正则表达式是一种小型的\高度专业化的变成语言,主要用于字符串处理 正则表达式是一种通用语言,在python中通过re模块实现,import re 工具:在线正则表达式测试 http ...

  5. L97

    We are young. So let's set the world on fire. We can burn brighter than the sun.我们是青年,让我们点亮世界,释放生命,胜 ...

  6. 洛谷P3252 [JLOI2012]树

    题目描述 在这个问题中,给定一个值S和一棵树.在树的每个节点有一个正整数,问有多少条路径的节点总和达到S.路径中节点的深度必须是升序的.假设节点1是根节点,根的深度是0,它的儿子节点的深度为1.路径不 ...

  7. Godot-3D教程-02.3D性能和局限性

    介绍 Introduction Godot遵循表现与性能平衡信条.在这个表现的世界中,它们总是有许多约定俗成的东西,主要是在用执行速度换取可用性与扩展性方面.下面是一些实际的例子: 高效地渲染对象是比 ...

  8. bzoj 4278 Tasowanie —— 后缀数组

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4278 每次取两个后缀中字典序较小的那个的首字符: 注意超出去的部分是 inf 而不是 0,因 ...

  9. asp.net mvc Model验证总结及常用正则表达式【转载】

    关于Model验证官方资料: http://msdn.microsoft.com/zh-cn/library/system.componentmodel.dataannotations.aspx AS ...

  10. 【240】◀▶IEW-Unit05

    Unit 5 Education: Study Abroad 表格技巧讲解 1. Model1对应表格分析 This table shows the numbers of international ...