PAT Advanced 1103 Integer Factorization (30) [depth-first search DFS]

topic

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 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 12^2 + 4^2 + 2^2 + 2^2 + 1^2, or 11^2 + 6^2 + 2^2 + 2^2 + 2^2, 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 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

Topic analysis

Known that N, the number of required power of k and p = N
, and taking the maximum base number sequence if the plurality of sequences; if there is still a plurality of sequences, whichever is greater sequence (two sequences, the first item i are equal, i + 1-item larger)
output requirement: sequence numbers in ascending order non-

Problem-solving ideas

  1. Since the p constant may be pretreated result all power p <= n numbers stored in the container (if the power of a number p greater than n, then this number can not be selected)
  2. dfs depth of the search, from small to large numbers of digital traversal is selected numerals may be repeated again selected, so the current digital two branches: The current number is selected and recorded; current number is not selected the option to skip the next digit
  3. dfs exit condition:
    3.1 (valid sequence) has been selected is greater than the sum of the numbers n
    3.2 (valid sequence) number of digits has been selected larger than K
    3.3 (valid sequence) has been selected equal to the sum of the numbers n and is selected the number of digits equal to k, updating the optimal solution

Code

#include <iostream>
#include <vector>
using namespace std;
int n,k,p,maxSum;
vector<int> fac,ans,temp;
int power(int x) {
	// 方法一:p个x相乘
	int ans =1;
	for(int i=0; i<p; i++)
		ans*=x;
	return ans;
	//方法二:使用cmath中power函数
}
void init() {
	for(int i=0; power(i)<=n; i++)
		fac.push_back(power(i));
}
// index 当前访问的索引
// numK 当前已经选择的数字个数
// sum 当前已经选择的数字之和
// facnum 当前已经选中的数字底数之和
void dfs(int index, int numK,int sum,int facSum) {
	if(sum==n&&numK==k) {
		// 符合条件,更新最优解
		if(facSum>maxSum) {
			ans=temp; //保存序列
			maxSum=facSum; //更新底数和最大值
		}
		return;
	}
	if(sum>n||numK>k)return;
	if(index>=1) { //不选0
		temp.push_back(index);
		dfs(index, numK+1, sum+fac[index], facSum+index); //选择当前数字
		temp.pop_back();
		dfs(index-1, numK, sum, facSum); //不选择当前数字
	}
}
int main(int argc,char *argv[]) {
	scanf("%d %d %d",&n,&k,&p);
	init(); //预处理数字的固定幂次结果 
	dfs(fac.size()-1,0,0,0);
	if(ans.empty()) printf("Impossible");
	else {
		printf("%d = %d^%d",n, ans[0], p);
		for(int i=1; i<k; i++)
			printf(" + %d^%d", ans[i], p);
	}
	return 0;
}



Guess you like

Origin www.cnblogs.com/houzm/p/12555952.html