代码:
1 #include <iostream> 2 #include <algorithm> 3 #include <vector> 4 using namespace std; 5 6 const int maxn = 10000; 7 8 //序列A中n各数选k个数使得和为x,最大平方和为maxSumSqu 9 int n, k, x, maxSumSqu = -1, A[maxn]; 10 11 vector<int> temp, ans; 12 13 void dfs(int index, int nowK, int sum, int sumSqu){ 14 if (nowK == k && sum == x){ //如果找到k个数的和为x 15 if (sumSqu > maxSumSqu){ //且找到的比当前更优 16 maxSumSqu = sumSqu; //更新最大平方和 17 ans = temp; //更新最优方案 18 } 19 20 return; 21 } 22 23 //已经处理完n个数,或者超过k个数,或者和超过x, 返回 24 if (index == n || nowK > k || sum > x) 25 return; 26 27 //选index号数 28 temp.push_back(A[index]); 29 dfs(index + 1, nowK + 1, sum + A[index], sumSqu + A[index] * A[index]); 30 temp.pop_back(); 31 32 //不选index号数 33 dfs(index + 1, nowK, sum, sumSqu); 34 } 35 36 37 int main() 38 { 39 freopen("in.txt", "r", stdin); 40 scanf("%d %d %d", &n, &k, &x); 41 for (int i = 0; i < n; i++){ 42 scanf("%d", &A[i]); 43 printf("%d ", A[i]); 44 } 45 46 dfs(0, 0, 0, 0); 47 48 printf("%d ", maxSumSqu); 49 fclose(stdin); 50 return 0; 51 }
如果每个元素可以重复被选,那么上面的程序只需改动一点点:将29行改为:
dfs(index, nowK + 1, sum + A[index], sumSqu + A[index] * A[index]);
其实这题用K层迭代也能解决。
运用这个思路求解下面这道题:
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+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:
Impossibl
解答代码为:
1 #include <stdio.h> 2 #include <vector> 3 #include <math.h> 4 5 using namespace std; 6 const int maxn = 25; 7 int fac[maxn]; //存储所有可能的项 8 int n, k, p; 9 int maxFacSum = -1; 10 vector<int> ans, temp; 11 12 //算出fac数组 13 int Fac(){ 14 int i = 0; 15 for (i = 0; pow(i, p) <= n; i++){ 16 fac[i] = pow(i, p); 17 } 18 return i - 1; 19 } 20 21 //index是fac的下标,nowK是当前已经叠加的项数,sum是当前之和,facSum是底数之和 22 void dfs(int index, int nowK, int sum, int facSum){ 23 if (nowK == k && sum == n){ 24 if (facSum > maxFacSum){ 25 maxFacSum = facSum; 26 ans = temp; 27 } 28 return; 29 } 30 // 31 if (nowK > k || sum > n) return; 32 33 if (index >= 1){ 34 35 //选index项 36 temp.push_back(index); 37 dfs(index, nowK + 1, sum + fac[index], facSum + index); 38 39 //不选index项 40 temp.pop_back(); 41 dfs(index - 1, nowK, sum, facSum); 42 } 43 44 } 45 46 int main() 47 { 48 //freopen("in.txt", "r", stdin); 49 scanf("%d %d %d", &n, &k, &p); 50 int index = Fac(); 51 dfs(index, 0, 0, 0); 52 53 //如果ans的元素为空,则说明不存在有效解 54 if (maxFacSum == -1){ 55 printf("Impossible "); 56 } 57 else{ 58 printf("%d = ", n); 59 for (int i = 0; i < k; i++){ 60 printf("%d^%d", ans[i], p); 61 if (i < k - 1){ 62 printf(" + "); 63 } 64 } 65 } 66 67 //fclose(stdin); 68 69 return 0; 70 }