PAT-A1103.Integer Factorization

TheK−Pfactorization of a positive integerNis to writeNas the sum of theP-th power ofKpositive integers. You are supposed to write a program to find theK−Pfactorization ofNfor any positive integersN,KandP.

Input Specification:

Each input file contains one test case which gives in a line the three positive integersN(≤400),K(≤N) andP(1

Output Specification:

For each case, if the solution exists, output in the format:

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

wheren[i](i= 1, ...,K) is thei-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 as12​^2​​+4​^2​​+2^​2​​+2​^2​​+1​^2​​, or11​^2​​+6​^2​​+2​^2​​+2​^2​​+2^​2​​, 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 {a​1​​,a​2​​,⋯,a​K​​} is said to belargerthan {b​1​​,b​2​​,⋯,b​K​​} if there exists1≤L≤Ksuch thata​i​​=b​i​​forib​L​​.

If there is no solution, simple outputImpossible.

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

注意点

  • 可以事先将每个数的p次幂保存下来,使得数组下标与数的平方相对应。
  • 利用dfs逐个选择数的时候可以从后向前选择,这样保证了字典序的最大值,无需再可以判断字典序。
  • 熟练计算x的p次幂。

代码

#include  
#include 
#include 

using namespace std;

const int N = 410;

int n, k, p;
int maxSumF = -1; 
int a[N];

vector fac, temp, ans;

int power(int x)
{
    int ans = 1;
    for (int i = 0; i < p; i ++)
        ans *= x;
    return ans;
}

int init()
{
    int i = 0, temp = 0;
    while (temp <= n)
    {
        fac.push_back(temp);
        temp = power(++ i);
    }
}


void dfs(int index, int nowK, int sum, int sumF)
{
    if ( nowK == k )
    {
        if (sum == n && sumF > maxSumF)        
        {
            maxSumF = sumF;
            ans = temp;
        }
        return;
    }
    
    if (sum > n || nowK > k) return;
    
    //选 
    if (index - 1 >= 0)
    {
        temp.push_back(index);
        dfs(index, nowK  + 1, sum + fac[index], sumF + index);
        temp.pop_back();
    
        //不选 
        dfs(index - 1, nowK, sum, sumF);    
    }

}



int main()
{
    scanf("%d %d %d", &n, &k, &p);
    init();        //初始化fac 
    dfs(fac.size() - 1, 0, 0, 0);
    
    if (ans.size() == k)
    {
        printf("%d = ", n);
        for (int i = 0; i < k; i ++)
        {
            printf("%d^%d", ans[i], p);
            if ( i < k - 1)
                printf(" + ");
         } 
    }
    else
    {
        printf("Impossible\n");
    }
        
    return 0;
}
参考资料《算法笔记》.

你可能感兴趣的:(算法-数据结构,c++)