[PAT]1103 Integer Factorization (30分)

1103 Integer Factorization (30分)

一、题目

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,or112+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 l <= 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

二、题目翻译

将一个正整数N写成K个整数的P次幂之和被称为“K-P因式分解”,现在要求你写一个程序来求任意正整数N的"K-P因式分解"。
输入格式:
每一个输入文件包含一个测试用例,包含三个正整数N(<=400),K(<=N)和P(1<P<=7)。每一个正整数用空格隔开。
输出格式:
对于每个测试用例,如果结果存在,输出格式如下:

N = n[1]^P + ... n[K]^P

其中n[i]表示第i个因子,因子按照降序排序。
注意:结果可能不唯一。例如,169的5-2因式分解就有9个解,其中两个解是:122+42+22+22+12和112+62+22+22+22。要求输出因子(不包括指数)之和最大的解。如果存在两个解的因子之和相等,那么输出字典顺序最大的解。(字典顺序:序列{a1,a2,…,ak}和{b1,b2,…,bk},如果存在1<=L<=K当i<L时ai=bi,并且aL>bL那么就说序列a的字典顺序大于序列b)。

三、思路

使用深度搜索DFS。

四、代码

#include <iostream>
#include <vector>
#include <cmath>

int N, K, P;
int flag = 0;	// 标记是否找到解
// 得到num的P次幂
int getPow(int num) {
    return std::pow(num, P);
}
// 因为可能有多个解,所以需要对解进行比较,返回因子和最大的解(当因子和相等时返回字典序最大的解)
std::vector<int> &getMaxVec(std::vector<int> &a, std::vector<int> &b) {
    int sumA = 0, sumB = 0;
    for (int i = 0; i < a.size(); i++) {
        sumA += a[i];
        sumB += b[i];
    }
    if (sumA > sumB) {
        return a;
    } else if (sumA < sumB) {
        return b;
    } else {
        for (int i = 0; i < a.size(); i++) {
            if (a[i] > b[i]) {
                return a;
            } else if (a[i] < b[i]) {
                return b;
            } else {
                std::runtime_error("Error in getMax:");
            }
        }
    }
}

// 深度搜索,cnt 搜索的深度(第几个因子,从0开始),index 因子的起始值,sum 到该节点所有节点的和,ans 解, tempAns 临时解
void DFS(int cnt, int index, int sum, std::vector<int> &ans, std::vector<int> &tempAns) {
    if (cnt >= K) {
        return ;
    }
    for (int i = index; i <= N; i++) {
        tempAns[cnt] = i;
        int indexPow = getPow(i);
        sum += indexPow;
        if (cnt == K - 1) {
            if (sum == N) {
                ans = getMaxVec(ans, tempAns);
                flag = 1;
                return;
            } else if (sum > N) {
                return;
            } else {
                sum -= indexPow;
                continue;
            }
        } else {
            if (sum + (K - cnt - 1) * indexPow > N) {
                return;
            } else {
                DFS(cnt + 1, i, sum, ans, tempAns);
                sum -= indexPow;
                continue;
            }
        }
    }
}

void printAns(std::vector<int> &ans) {
    std::cout << N << " = ";
    for (int i = ans.size() - 1; i > 0; i--) {
        std::cout << ans[i] << "^" << P << " + ";
    }
    std::cout << ans[0] << "^" << P << std::endl;
}


int main() {
    std::cin >> N >> K >> P;
    std::vector<int> ans(K);
    std::vector<int> tempAns(K);
    for (int i = 0; i < K; i++) {
        ans[i] = 0;
        tempAns[i] = 0;
    }
    DFS(0, 1, 0, ans, tempAns);

    if (flag) {
        printAns(ans);
    } else {
        std::cout << "Impossible" << std::endl;
    }
    return 0;
}

posted on 2020-03-22 23:14  刘好念  阅读(3)  评论(0编辑  收藏  举报  来源