PAT甲级刷题记录——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​ +2​2 +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≤L≤K such that ai​ =bi​ for i<L and aL >b​L​ .
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

思路

这题其实是个很简单的递归的题目(感觉放到递归里更符合这题的本质……),但是呢,多种标尺的选择,又感觉像是图里面的DFS,然后导致半天都做不出来(太笨了)……

最后看了晴神的代码才AC……在这里提醒一下自己:碰到诸如此类能反复选一个数的题目,dfs的分岔路就是选和不选两种方案(我一开始想的是用for循环选择每一个数,然后每个数都能进一个dfs,然后写的代码很冗长,而且只有简单的数才能出结果,稍微大一点的直接就爆了(应该是因为分岔路太多的缘故,如果像样例一样169的话,最大能到132,也就是每次要for循环13个dfs,时间复杂度是O(N13),害怕)……)。

这题给我的感觉和《算法笔记》递归章节里的:【问题 C: 神奇的口袋】,不过那题不能重复选同一个,但是思路和这题大体一致:选当前index号的元素(如果选了,那么包含当前index号位,往前的元素之和应该是V),或者不选当前index号的元素(如果不选,那么不包含当前index号位,前面的元素之和应该是V)。

代码

#include<cstdio>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
vector<int> path, tempPath;//路径存放的是底数
vector<int> fac;//fac[i]代表着底数为i,指数是P的值
int N, K, P;
int maxDishuSum = -1;
int power(int x){//x^P
    int sum = 1;
    for(int i=0;i<P;i++){
        sum *= x;
    }
    return sum;
}
void dfs(int index, int DishuSum, int sum, int nowK){
    if(sum==N&&nowK==K){
        if(DishuSum>maxDishuSum){
            maxDishuSum = DishuSum;
            path = tempPath;
        }
        return;
    }
    if(sum>N||nowK>K) return;
    if(index-1>=0){//如果还存在前一位的话,就有“选”和“不选”当前index位两种选择
        //也就是最前只到fac[1],如果index=0,则不存在前一位,就不会发生任何操作
        tempPath.push_back(index);
        dfs(index, DishuSum+index, sum+fac[index], nowK+1);//继续选index号的数
        tempPath.pop_back();
        dfs(index-1, DishuSum, sum, nowK);//不选index号的数,故往前搜索
    }
}
int main(){
    scanf("%d%d%d", &N, &K, &P);
    for(int i=0;power(i)<=N;i++){
        fac.push_back(power(i));//fac[i] = i^p
    }
    dfs(fac.size()-1, 0, 0, 0);
    if(maxDishuSum==-1) printf("Impossible");
    else{
        printf("%d = ", N);
        for(int i=0;i<path.size();i++){
            if(i==0) printf("%d^%d", path[i], P);
            else printf(" + %d^%d", path[i], P);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值