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[];
int flag=;
int ans_path[],ans_num=,ans_len; void dfs(int x,int pre,int sum,int num)
{
if(sum>n) return;
if(x>k) return;
if(sum==n)
{
if(x!=k) return; flag=;
if(num>ans_num)
{
ans_num=num;
ans_len=x;
for(int i=;i<x;i++) ans_path[i]=path[i];
} return;
}
for(int i=pre;i>=;i--)
{
path[x]=i;
dfs(x+,i,sum+int(pow(i,p)+0.5),num+i);
}
}
int main()
{
scanf("%d%d%d",&n,&k,&p);
top=(int) (pow(1.0*n,1.0/p)+0.5);
top=min(top,n/k); //加这个剪枝速度还可以提高一些 dfs(,top,,);
if(flag==) printf("Impossible\n");
else
{
printf("%d = ",n); for(int i=;i<ans_len;i++)
{
printf("%d^%d",ans_path[i],p);
if(i!=ans_len-) printf(" + ");
}
}
return ;
}
PAT (Advanced Level) 1103. Integer Factorization (30)的更多相关文章
- 【PAT甲级】1103 Integer Factorization (30 分)
题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...
- 1103 Integer Factorization (30)
1103 Integer Factorization (30 分) The K−P factorization of a positive integer N is to write N as t ...
- PAT甲题题解-1103. Integer Factorization (30)-(dfs)
该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + … nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. ...
- 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 positi ...
- 1103 Integer Factorization (30)(30 分)
The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...
- PAT (Advanced Level) 1113. Integer Set Partition (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT (Advanced Level) 1111. Online Map (30)
预处理出最短路再进行暴力dfs求答案会比较好.直接dfs效率太低. #include<cstdio> #include<cstring> #include<cmath&g ...
- PAT (Advanced Level) 1107. Social Clusters (30)
简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- PAT (Advanced Level) 1072. Gas Station (30)
枚举一下选的位置,每次算一下就可以了. #include<cstdio> #include<cstring> #include<cmath> #include< ...
随机推荐
- 创建UIView对象
创建一个UIView对象分四步: 1.创建一个view对象,并设置其大小 UIView *view = [[UIView alloc] initWithFrame:CGRectMake:(CGFloa ...
- erlang程序优化点的总结(持续更新)
转自:http://wqtn22.iteye.com/blog/1820587 转载请注明出处 注意,这里只是给出一个总结,具体性能需要根据实际环境和需要来确定 霸爷指出,新的erlang虚拟机有很多 ...
- AJAx 刷新页面
<html><head> <meta http-equiv="Content-Type" content="text/html; chars ...
- ****ural 1141. RSA Attack(RSA加密,扩展欧几里得算法)
1141. RSA Attack Time limit: 1.0 secondMemory limit: 64 MB The RSA problem is the following: given a ...
- c# 操作word demo
/// <summary> /// 新创建word /// </summary> /// <param name="fileSaveDirectory" ...
- android开发注意点
一.Android编码规范 1.java代码中不出现中文,最多注释中可以出现中文 2.局部变量命名.静态成员变量命名 只能包含字母,单词首字母出第一个外,都为大写,其他字母都为小写 3.常量命名 只能 ...
- Process 'command '/usr/lib/jvm/jdk1.8.0_25/bin/java'' finished with non-zero exit value 2
这个问题有两种可能,第一是jar包或者依赖冲突(版本冲突也算).第二是65535. 对于65535的问题,请看http://blog.csdn.net/t12x3456/article/details ...
- hdu_3966_Aragorn's Story(树链剖分裸题)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:给你一棵树,然后给定点之间的路径权值修改,最后单点查询 题解:树链剖分裸题,这里我用树状数 ...
- 再谈KMP
昨天讲解了字典树和AC自动机后感觉整个人都蒙掉了.还好就是自己今天在网上看见一篇对KMP讲解非常详细的帖子,果断收藏.(点击这里查看) 然后代码的实现也就简单分析一些了,具体的知识点大家直接自己链接过 ...
- AngularJS 的表单验证
最近开始学习angularjs,学到表单验证的时候发现有必要学习下大神的好文章: 转:http://www.oschina.net/translate/angularjs-form-validatio ...