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< ...
随机推荐
- redis数据类型:sorted sets类型及操作
sorted sets类型及操作: sorted set是set的一个升级版本,它是在set的基础上增加了一个顺序 属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会 自动重新按新的值 ...
- linux双线ip设置(不需额外增加路由表)
linux 双线ip设置(不需额外增加路由表,只需修改下面就ok了)修改 vi /etc/iproute2/rt_tables (增加电信和网通两个路由表) 增加252 ...
- 使用timer定时器,防止事件重入
首先简单介绍一下timer,这里所说的timer是指的System.Timers.timer,顾名思义,就是可以在指定的间隔是引发事件.官方介绍在这里,摘抄如下: 1 2 Timer 组件是基于服务器 ...
- Hibernate中自带ID的generator的含义
increment:代理主键,适合于所有数据库,由hibernate维护主键自增,和底层数据库无关,但是不适合于2个或以上hibernate进程. identity:代理主键,适合于Mysql或ms ...
- svn地址如何更改
1.先进入平时放的更新文件的位置 2.然后右键 选中TortoiseSVN中的Relocate修改里面的完了之后输入账号和密码就好了当然你要记得密码和账号
- HDU - 2586 How far away ?(LCA模板题)
HDU - 2586 How far away ? Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- android开发注意点
一.Android编码规范 1.java代码中不出现中文,最多注释中可以出现中文 2.局部变量命名.静态成员变量命名 只能包含字母,单词首字母出第一个外,都为大写,其他字母都为小写 3.常量命名 只能 ...
- 命令窗口修改编码,CMD编码修改方法
cmd中的编码方式为ANSI,若中文不是此编码方式则会出现乱码.作为程序员,会经常使用命令窗口查看执行日志,但是有时编码格式不对,大部分都是UTF8,在网上搜索了不少方法,很多没什么用,在这里教一个具 ...
- 搭建Mantis 缺陷管理系统
什么是Mantis MantisBT is a free popular web-based bugtracking system (feature list). It is written in t ...
- MFC中MessageBeep与sndPlaySound播放声音函数使用
MessageBeep(0x00000000L); //用来播放系统默认音频文件,如0x00000000L为系统提示音,具体音频对应规则,请参照MSDN. sndPlaySound函数用来播放指 ...