美文网首页
2018-06-05

2018-06-05

作者: 动感新势力fan | 来源:发表于2018-06-05 23:44 被阅读11次
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    #include <iostream>
    #include <vector>
    #include <cmath>
    using namespace std;
    int n, k, p, maxFacSum = -1;
    vector<int> v, ans, tempAns;
    void init() {
        int temp = 0, index = 1;
        while(temp <= n) {
            v.push_back(temp);
            temp = pow(index, p);
            index++;
        }
    }
    void dfs(int index, int tempSum, int tempK, int facSum) {
        if(tempSum == n && tempK == k) {
            if(facSum > maxFacSum) {
                ans = tempAns;
                maxFacSum = facSum;
            }
            return ;
        }
        if(tempSum > n || tempK > k) return ;
        if(index >= 1) {
            tempAns.push_back(index);
            dfs(index, tempSum + v[index], tempK + 1, facSum + index);
            tempAns.pop_back();
            dfs(index - 1, tempSum, tempK, facSum);
        }
    }
    int main() {
        scanf("%d%d%d", &n, &k, &p);
        init();
        dfs(v.size() - 1, 0, 0, 0);
        if(maxFacSum == -1) {
            printf("Impossible");
            return 0;
        }
        printf("%d = ", n);
        for(int i = 0; i < ans.size(); i++) {
            if(i != 0) printf(" + ");
            printf("%d^%d", ans[i], p);
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:2018-06-05

          本文链接:https://www.haomeiwen.com/subject/fwrvsftx.html