【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, 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,?,*b**K* } if there exists 1≤LK 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次方之和,因子以非增顺序排列。多个解中取因子之和最大的,若和相同则取因子的字典序在前的。没解输出Impossible

  • 设置数组a:将index=0开始的数字的P次方存到数组a中,直到a[index]>n。
  • 全局变量:设置数组tempAns和ans,变量facSum和maxFacSum

    数组tempAns存放当前解的临时因子们,ans存放最优解的因子们。

    maxFacSum最优解的因子之和,初值设置为-1。

  • 开始dfs:
    • tempK表示当前K的个数,当tempK==K时,判断tempSum当前总和==N且(是否为最优解)facSum当前解的因子之和>maxFacSum:更新最优结果变量ans和maxFacSum。
    • index从高后前遍历数组a
      • 判断当前项a[index]是否加入解:tempSum+a[index]<=N,满足则更新tempAns,并递归调用dfs确定下一项
      • 判断是否完成一个解:index==1
      • 若不满足tempSum+a[index]<=N,则index--,因子变小再判断。

代码(参考柳神)

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

using namespace std;

int K,N,P;
vector<int> a,tempAns,ans;
int maxFacSum=-1;

/*初始化:计算a数组*/
void init() {
    int temp=0;
    for(int i=1; temp<=N; i++) {//注意;i从1开始
        a.push_back(temp);
        temp=pow(i,P);
    }
}
/*dfs*/
void dfs(int index,int tempK,int tempSum,int facSum) {
    if(tempK==K) {
        if(tempSum==N&&facSum>maxFacSum) {
            ans=tempAns;
            maxFacSum=facSum;
        }
        return;
    }

    while(index>=1) {
        if(tempSum+a[index]<=N) {
            tempAns[tempK]=index;//使用数组访问必须对vector进行resize
            dfs(index,tempK+1,tempSum+a[index],facSum+index);
        }
        if(index==1) return;
        index--;
    }

}
int main() {
    scanf("%d %d %d",&N,&K,&P);

    init();
    tempAns.resize(K);
    dfs(a.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;
}

单词

  • factorization:因式分解

    factor:因子

  • If there is a tie , ..:平手(相等)

原文地址:https://www.cnblogs.com/musecho/p/12236349.html

时间: 2024-10-08 08:00:46

【PAT甲级】1103 Integer Factorization (30分)的相关文章

1103&#160;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 a

1103. Integer Factorization (30)【搜索+剪枝】——PAT (Advanced Level) Practise

题目信息 1103. Integer Factorization (30) 时间限制1200 ms 内存限制65536 kB 代码长度限制16000 B 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 factoriz

PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to count the total number of 1's in the decimal form of the integers from 1 to N. For example, given N being 12, there are five 1's in 1, 10, 11, and 12. Inp

PAT甲题题解-1103. Integer Factorization (30)-(dfs)

该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + - nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. dfs枚举,为了防止超时,这里要预先将从1开始的i^p的值存储在factor数组中,直到i^p>n.然后dfs深度优先搜索,相当于把问题一步步分解,即若第一个因子是n1,则接下来我们要判断N-n1^p.k-1是否可行.同时存储当前因子的总和sum,要取sum最大的:还有上一次相加的因子的索引las

PAT (Advanced Level) 1103. Integer Factorization (30)

暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n,k,p,top; int path[1000]; int flag=0; int

PAT 1103 Integer Factorization[难]

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 an

pat 1132 Cut Integer(20 分)

1132 Cut Integer(20 分) Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long integers A and B. For example, after cutting Z = 167334, we have A = 167 and B = 334. It is interesting to see that Z can be devid

【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全部出手的利润.输出最大利润. trick: 测试点2可能包含M不为整数的数据.(尽管题面说明M是正整数,可是根据从前PAT甲级题目的经验,有可能不是整数.....) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using names

PAT 1103 Integer Factorization

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 in