数字因子(深度优先搜索)

1. 问题描述:

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 = n1^P + ... nK^P

where ni (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 + 42 + 22 + 22 + 12, or 112+ 62 + 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

2. 题目的大概意思是:

给定正整数N,K,P,将N表示成K个正整数(可以相同,递减排列)的P次方的和,即N = n1 ^ P + n2 ^ p ...+nk ^p,如果有多个方案那么选择底数之和n1 + n2...nk最大的方案,如果还有多种方案,那么选择底数序列的字典序最大的方案

3. 思路如下:

① 首先因为我们对于组成N的因子是不知道的,所有需要对可能的数字进行判断,需要尝试完所有的结果才可以知道形成的底数之和最大的方案是什么,根据上面的分析我们可以知道可以使用深度优先来解决,搜索所有可能的结果进行逐一的判断即可

② 由于p是不小于2的,并且在单次运行固定的,因此不妨开一个vector<int> fac,在输入p之后就预处理出所有不超过N的n ^ p,为了方便下标与元素有直接的对应,这里应该把0存进去,于是对于N = 10来说,fac[0] = 0,fac[1] = 1,fac[2] = 4,fac[3] = 9

③ 接下来需要确定dfs方法中的参数问题,因为我们要选择出若干个数字来组成N,而且可以重复选择,所以我们可以知道平行状态存在两个选择与不选择,在dfs递归的方法中我们需要知道当前处理到的是第几个位置上的数字(因子),当前处理了多少个数字,因为存在多种情况而且我们需要选择一个底数之和最大的方案,最后需要输出这些底数所以我们需要声明一个记录当前选择的数字之和的变量sum和记录当前选择的底数之和变量facsum,此外还需要开一个vector<int> ans用来存放最优的底数序列,而用一个vector<int>temp用来存放当前选中的底数组成的临时序列

void dfs(int index, int nowK, int sum, int facsum);

④ 递归方法中的设计,因为我们直到尽量让字典序大的序列优先被选中,让index从大到小递减来遍历,这样就能够总是先选中fac较大的数字了

递归的出口:什么时候递归结束呢?当sum == N而且 nowk == k的时候表示找到了一个满足条件的序列,此时为了处理多方案的情况我们需要判断底数之和facsum是否比一个全局记录的最大底数之和maxFacSum更大,如果更大则更新maxFacSum,并把temp赋值给记录最优底数序列的遍历ans,除此之前当sum > N或者nowk > k的时候都不能够产生答案直接return就好了

⑤ 需要注意的两点:

1)多方案判断最优的做法时间复杂度最优是O(1),否则容易超时,因此必须在dfs中记录当前的底数之和facsum,避免再找到一组解之后计算序列的底数之和

2)不要在找到一组解之后才判断temp序列与ans序列的字典序关系,而应该让index从大到小进行选择,这样fac[index]大的就会相对早地选中

4. 具体的代码如下:

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int n, k, p, maxFacSum = -1;
vector<int> fac, ans, temp;
int power(int x){
	int ans = 1;
	for(int i = 0; i < p; ++i){
		ans *= x;
	}
	return ans;
}

void init(){
	int i = 0, temp = 0;
	while(temp <= n){
		fac.push_back(temp);
		temp = power(++i);
	}
}

void dfs(int index, int nowK, int sum, int facSum){
	if(sum == n && nowK == k){
		if(facSum > maxFacSum){
			ans = temp;
			maxFacSum = facSum;
		}
		return;
	}
	if(sum > n || nowK > k) return;
	if(index - 1 >= 0){//fac[0]不需要选择 
		temp.push_back(index);
		dfs(index, nowK + 1, sum + fac[index], facSum + index);
		//回溯 
		temp.pop_back();
		dfs(index - 1, nowK, sum, facSum);
	}
}

int main(void){
	scanf("%d%d%d", &n, &k, &p);
	init();
	dfs(fac.size() - 1, 0, 0, 0);
	if(maxFacSum == -1) printf("Impossible\n");
	else{
		printf("%d = %d^%d", n, ans[0], p);
		for(int i = 1; i < ans.size(); ++i){
			printf(" + %d^%d", ans[i], p);
		}
	}
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值