1103 Integer Factorization (30point(s)) - C語言 PAT 甲級

1103 Integer Factorization (30point(s))

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+2​2+12, or 112​​+6​2+2​2+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 a​L​​>b​L.

If there is no solution, simple output Impossible.

Sample Input:

169 5 2

Sample Output:

169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2

Sample Input:

169 167 3

Sample Output:

Impossible

題目大意:

輸入整數 N,K,P,求 K 個數的 P 次方相加等於 N,若有多個序列,輸出字典序最大的

設計思路:
  • 建立不大於 N 的 i ^ K 數組,節約運算時間
  • DFS 算法求解,dfs(index 當前 i 值,nowk 當前 k 個數,nowindexsum 當前所有 i 值的和,nowpowsum 當前所有 i ^ K 的和)
    • 因爲序列可以有重複的值,所以有兩個遞歸入口,dfs(index) 和 dfs(index - 1)
    • nowk 和 nowpowsum 判斷終止遞歸
    • nowindexsum 判斷字典序列大小
    • 因爲需要字典序最大,所以 dfs 從最大值開始
編譯器:C (gcc)
#include <stdio.h>
#include <math.h>

int n, k, p;
int pownum[410], cnt;
int result[410], rcnt, temp[410], tcnt;
int indexsum = 0;

void dfs(int index, int nowk, int nowindexsum, int nowpowsum)
{
        if (nowk >= k || nowpowsum >= n) {
                if (nowk == k && nowpowsum == n && nowindexsum > indexsum) {
                        int i;
                        for (i = 0; i < tcnt; i++)
                                result[i] = temp[i];
                        rcnt = tcnt;

                        indexsum = nowindexsum;
                }
                return ;
        }
        if (index > 0) {
                temp[tcnt] = index;
                tcnt++;
                dfs(index, nowk + 1, nowindexsum + index, nowpowsum + pownum[index]);
                tcnt--;
                dfs(index - 1, nowk, nowindexsum, nowpowsum);
        }
}

void makepownum()
{
        int i, t;
        for (i = 1; i <= 400 && (t = pow(i, p)) <= n; i++)
                pownum[i] = t;
        cnt = i;
}

int main(void)
{
        scanf("%d%d%d", &n, &k, &p);
        makepownum();
        dfs(cnt - 1, 0, 0, 0);
        if (rcnt) {
                printf("%d = %d^%d", n, result[0], p);
                int i;
                for (i = 1; i < rcnt; i++)
                        printf(" + %d^%d", result[i], p);
        } else {
                printf("Impossible");
        }
        return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章