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 12​2​​+4​2​​+2​2​​+2​2​​+1​2​​, or 11​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 be larger than { b​1​​,b​2​​,⋯,b​K​​ } if there exists 1≤L≤K such that a​i​​=b​i​​ for i<L and a​L​​>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

 #include <stdio.h>
#include <algorithm>
#include <set>
#include <string.h>
#include <vector>
#include <math.h>
#include <queue>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
const int maxn = ;
int n,p,k,maxk=-;
int vis[maxn]={};
vector<int> res,tmp;
void dfs(int index,int ksum,int cntk,int nsum){
//if(index<1 || nsum>n || cntk>k) return;
if(nsum==n && cntk == k){
if(ksum>maxk){
res=tmp;
maxk=ksum;
}
return;
}
tmp.push_back(index);
if(nsum+vis[index]<=n && cntk+<=k)dfs(index,ksum+index,cntk+,nsum+vis[index]);
tmp.pop_back();
if(index->)dfs(index-,ksum,cntk,nsum);
}
int main(){
scanf("%d %d %d",&n,&k,&p);
int i;
for(i=;i<=n;i++){
int res = pow(i,p);
if(res>n)break;
vis[i]=res;
}
i--;
dfs(i,,,);
if(maxk==-)printf("Impossible");
else{
printf("%d = ",n);
sort(res.begin(),res.end(),cmp);
for(int j=;j<res.size();j++){
printf("%d^%d",res[j],p);
if(j<res.size()-)printf(" + ");
}
}
}

注意点:看到题目想到了要从大到小一个个遍历然后去比较条件,想用while和for写出来,发现真的写不来,有好多情况,看了大佬的思路,原来这就是递归,很明显的有个递归边界,递归式也很方便,果然对递归的理解还是不够深,知道思路,却没想到用递归这个武器。

PAT A1103 Integer Factorization (30 分)——dfs,递归的更多相关文章

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

    该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + … nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. ...

  2. 【PAT甲级】1103 Integer Factorization (30 分)

    题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...

  3. PAT A1103 Integer Factorization

    线性dfs,注意每次深搜完状态的维护~ #include<bits/stdc++.h> using namespace std; ; vector<int> v,tmp,pat ...

  4. 1103 Integer Factorization (30)

    1103 Integer Factorization (30 分)   The K−P factorization of a positive integer N is to write N as t ...

  5. PAT 1103 Integer Factorization[难]

    1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...

  6. PAT 甲级 1147 Heaps (30 分) (层序遍历,如何建树,后序输出,还有更简单的方法~)

    1147 Heaps (30 分)   In computer science, a heap is a specialized tree-based data structure that sati ...

  7. [PAT] 1147 Heaps(30 分)

    1147 Heaps(30 分) In computer science, a heap is a specialized tree-based data structure that satisfi ...

  8. PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

    1057 Stack (30 分)   Stack is one of the most fundamental data structures, which is based on the prin ...

  9. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

随机推荐

  1. Java框架之Struts2(四)

    一.ComboGrid 扩展自$.fn.combo.defaults和$.fn.datagrid.defaults.使用$.fn.combogrid.defaults重写默认值对象.数据表格下拉框结合 ...

  2. 简单说一下UWP中的JumpList

    在Windows10的10856这个版本中,微软为桌面版提供了一组新的应用交互方式,磁贴和Toast通知的个性化都有了一定的改善.针对磁贴方面,微软为我们提供了一组新的API来扩充我们对应用的交互方式 ...

  3. tomcat8 manager页面限制IP访问

    tomcat8 manager页面限制IP访问 配置tomcat8/webapps/manager/META-INF/context.xml <Context antiResourceLocki ...

  4. JavaScript--水平幻灯片

    // 列表布局,图片左浮动,外侧容器设置overflow:hidden; // 组合使用构造函数模式(对象不共享的数据)和原型模式(对象共享的数据) <!DOCTYPE html> < ...

  5. TFS 安装遇到的问题

    居然是是微信桌面客户端占用了8080端口,也是醉了... 1 VS链接 源码管理器 发现提示 Http 404, 发现原来是自己吧tfs 给删除了 2 重新安装tfs,过程中提示 8080 端口被占用 ...

  6. KVM虚拟化研究-1

    使用qemu-img创建镜像 例子: [root@HOST31 rybtest]# qemu-img create -f raw /rybtest/test1.raw 1G 使用qemu-img查看镜 ...

  7. ocLazyLoad按顺序加载

    $ocLazyLoad.load({ serie:true, files: [oneFile,twoFile] }) 使用serie:true 这是 传送门

  8. 3.网络编程-tcp的服务器简单实现

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/1/13 22:03 # @Author : ChenAdong # @ema ...

  9. sql server系统表和视图相关的语句

    一.系统表 数据字典的详细信息请查SQL SERVER BOL,这里仅列出一部分. 1.1.sysservers 1.查看所有本地服务器及链接服务器 select * from master..sys ...

  10. [20171225]没有备份数据文件的恢复.txt

    [20171225]没有备份数据文件的恢复.txt --//别人问的问题,增加了数据文件没有备份,如何恢复,实际上很简单,因为当前控制文件有记录建立时间只要从建立数据文件开始的--//归档日志都存在恢 ...