a dfs Questions

Code:

. 1 #include <the iostream>
 2 #include <algorithm>
 . 3 #include <Vector>
 . 4  the using  namespace STD;
 . 5  
. 6  const  int MAXN = 10000 ;
 . 7  
. 8  // sequence selected from the group A, the number n and the number of each of k such that x, The maximum sum of squares maxSumSqu 
. 9  int n-, K, X, maxSumSqu = - . 1 , A [MAXN];
 10  
. 11 Vector < int > TEMP, ANS;
 12 is  
13 is  void DFS ( int index, int nowK, int SUM, int sumSqu) {
14      IF (SUM nowK && k == == X) {             // if the number is found k and X 
15          IF (sumSqu> maxSumSqu) {         // and found better than the current 
16              maxSumSqu = sumSqu;             // Update The maximum sum of squares 
. 17              ANS = TEMP;                     // update the optimal solution 
18 is          }
 . 19  
20 is          return ;
 21 is      }
 22 is  
23 is      // been processed number n, the number of or more than k, or with more than x, returns 
24      IF (index = n-nowK || => K || SUM> X)
 25          return ;
 26 is  
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\n", maxSumSqu);
49     fclose(stdin);
50     return 0;
51 }

Each element can be repeated if selected, then the above procedure only changes a little: the 29 lines read:

 dfs(index, nowK + 1, sum + A[index], sumSqu + A[index] * A[index]);

 In fact, this problem with the K layer iteration can be solved.

Use this idea to solve this question below:

1103 Integer Factorization (30 分)
 

The KP 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 KP 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<P7). 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 1LK 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

Solution code:

. 1 #include <stdio.h>
 2 #include <Vector>
 . 3 #include <math.h>
 . 4  
. 5  the using  namespace STD;
 . 6  const  int MAXN = 25 ;
 . 7  int FAC [MAXN];                 // to store all possible entries 
8  int n-, K, P;
 . 9  int maxFacSum = - . 1 ;
 10 Vector < int > ANS, TEMP;
 . 11  
12 is  // calculated fac array 
13 is  int fac () {
 14      int I = 0;
 15      for (I = 0 ; POW (I, P) <= n-; I ++ ) {
 16          FAC [I] = POW (I, P);
 . 17      }
 18 is      return I - . 1 ;
 . 19  }
 20 is  
21 is  // index is fac subscript, nowK are currently already several superimposed, sum of the current and, facSum sum is the base 
22 is  void DFS ( int index, int nowK, int sUM, int facSum) {
 23 is      IF (K == && nowK == 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 is      }
 43 is  
44 is  }
 45  
46 is  int main ()
 47  {
 48      // The freopen ( "in.txt", "R & lt", stdin); 
49      Scanf ( " % D % D% D " , & n-, & K, & P);
 50      int index = Fac ();
 51 is      DFS (index, 0 , 0 , 0 );
 52 is      
53 is      // if ans the element is empty, then there is no valid solution 
54 is      IF (maxFacSum == - . 1 ) {
 55         printf("Impossible\n");
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 }

 

Guess you like

Origin www.cnblogs.com/hi3254014978/p/11462913.html
dfs