PAT A1103 Integer Factorization (30 分)——dfs,递归
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,⋯,bK } if there exists 1≤L≤K 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
#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写出来,发现真的写不来,有好多情况,看了大佬的思路,原来这就是递归,很明显的有个递归边界,递归式也很方便,果然对递归的理解还是不够深,知道思路,却没想到用递归这个武器。
最新文章
- C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 远程同步服务器大量基础数据到客户端
- mongodb 维护
- Piggy-Bank
- css3 钟表
- 关于HTML中浮动与清除的思考
- sql 选取每个分组中的第一条数据
- Ajax的“dataType”乱用的陷阱
- 李洪强iOS开发之使用CycleScrollView实现轮播图
- 用Canvas,画中国国旗(Canvas基本知识点)
- Python学习--字典
- python之tuple
- [BZOJ1601] [Usaco2008 Oct] 灌水 (kruskal)
- Extjs6.2 项目学习系列(一)
- java基础1之基本数据类型
- linux-网站收藏
- css 可继承属性 display:inline-block 历史
- socket 发送图片
- function方法控制是否隐藏部分内容
- 有关JOIN ON的心得体会
- 【liunx】nslookup命令
热门文章
- elasticsearch6.7 05. Document APIs(6)UPDATE API
- 【nginx】详细配置说明
- js .map方法
- Python3.4:splinter or traceback
- javascript: checked 不可全选
- [工作总结]jQuery在工作开发中常用代码片段集锦(1-10)
- 微软 WPC 2014 合作伙伴keynote
- 测试思想-集成测试&#160;关于接口测试&#160;Part&#160;2
- weblogic系列漏洞整理 -- 1. weblogic安装
- 08-OpenLDAP主机控制策略