PAT advanced-1103-Integer Factorization (30分)【深搜DFS】

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 (≤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 = 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 122+4​2+22+22+12 or 112+6​2+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

题目要求:
找出总和最大的K个数,使这K个数的P次方之和为N,若有两组数总和相等,则找出不等的第一个元素,元素大的即为为最终解。

思路:
1. 基本算法思想为DFS,算法参数为当前数字num,当前P次方之和nown,当前数字的个数nowk,当前数字之和sum,同时定义一个全局变量maxsum。
2. 在主函数中计算出DFS算法的初始num,即为符合i^P<N的最大i值。
3.注意剪枝,否则容易超时。

/*
DFS深度优先搜索,按照算法笔记上的模板即可,写出应熟记模板
*/
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int n, k, p;//k个数的p次方之和为n
int maxsum = 0;//数的最大和,当前和,当前p次方之和,当前第几个数
vector<int> temp, ans;  //temp储存计算出的当前解,ans储存最终解
//num为当前数,nown为当前P次方之和,nowk为当前个数,sum为当前数字之和
void DFS(int num, int nown, int nowk,int sum)
{
	if (nowk == k && nown == n)  //若找到一组解
	{
		if (sum > maxsum)  //若当前和大于最大和,则更新最终解
		{
			ans = temp;
			maxsum = sum;
		}
		else if (sum == maxsum)  //若当前和与最大和相等,则依次比较
		{
			int i = 0;   //i为数组下标
			while (i < k && temp[i] == ans[i])  //找出当前解与最终解第一个不同的元素下标
				i++;
			if (i<k && temp[i]>ans[i])  
				ans = temp;
		}
		return;
	}
	if (num == 0 || nowk > k || nown > n) return;  //剪枝!!!

	//由于num数可以被加上多次,因此加上num后,不应该直接处理num-1,应该继续加num,直至退出循环
	//加上当前num
	temp.push_back(num);
	DFS(num , nown + pow(num, p), nowk + 1, sum + num);
	temp.pop_back();  //pop_back()函数不需要参数
	//不加num
	DFS(num - 1, nown, nowk, sum);
}
int main()
{
	cin >> n >> k >> p;
	int i = 0;
	while (pow(i, p) < n)  //计算深搜的第一个数,最终解中的数均小于该数
		i++;
	//cout << "第一个数:" << i << endl;
	DFS(i, 0, 0, 0);
	
	if (ans.size() == k) {  //若有解,则输出
		cout << n << " =";
		for (int j = 0; j < k; j++)
		{
			if (j > 0) cout << " +";
			cout << " " << ans[j] << "^" << p;
		}
	}
	else cout << "Impossible";  //若无解,则输出impossible
	
	return 0;
}

注:算法笔记上的算法讲解是我见过最容易懂的,强烈推荐。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值