暴力搜索。

#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)的更多相关文章

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

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

  2. 1103 Integer Factorization (30)

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

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

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

  4. 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 ...

  5. 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 ...

  6. PAT (Advanced Level) 1113. Integer Set Partition (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  7. PAT (Advanced Level) 1111. Online Map (30)

    预处理出最短路再进行暴力dfs求答案会比较好.直接dfs效率太低. #include<cstdio> #include<cstring> #include<cmath&g ...

  8. PAT (Advanced Level) 1107. Social Clusters (30)

    简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  9. PAT (Advanced Level) 1072. Gas Station (30)

    枚举一下选的位置,每次算一下就可以了. #include<cstdio> #include<cstring> #include<cmath> #include< ...

随机推荐

  1. ecos 问题答疑(转)

    1.为什么我购买的是源码版,但是我的base/ego.php(或者base/ego/目录下文件)却是加密的?  答:ego 源码商业授权文件仅用于和商派软件签订源码协议的商业用户按照甲乙的源码保护约定 ...

  2. Matlab中矩阵的平方和矩阵中每个元素的平方介绍

    该文章讲述了Matlab中矩阵的平方和矩阵中每个元素的平方介绍.   设t = [2 4 2 4] 则>> t.^2 ans = 4 164 16 而>> t^2 ans = ...

  3. NYOJ-1036 非洲小孩(贪心)

    非洲小孩 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 家住非洲的小孩,都很黑.为什么呢?第一,他们地处热带,太阳辐射严重.第二,他们不经常洗澡.(常年缺水,怎么洗 ...

  4. MemoryStream 转 pdf

    在项目开发中用到将MemoryStream 转pdf,在转化过程中需要建了一个.dom格式的模板,先保存为.doc文件,然后再转换为.pdf. 有一个插件感觉好不错,给大家推荐一下. dll下载链接  ...

  5. FreeMarker 实例

    1.jar包:freemarker-2.3.19.jar,将jar拷贝到lib目录下: 2.新建Web项目:TestFreeMarker 在web目录下新建ftl文件夹: 在ftl下新建模版文件ftl ...

  6. RecyclerView.Adapter

    RecyclerView无需多说,是用于替代ListView的新控件.它的适配器在于灵活. 现在有一个需求:需要RecyclerView的item支持点击事件,并且下拉到最后时,显示ProgressB ...

  7. Load$$ execution region symbols

    6.3.3 Load$$ execution region symbols The linker generates Load$$ symbols for every execution region ...

  8. C语言介绍

    以下东东转自百度百科 C语言是一种计算机程序设计语言,它既具有高级语言的特点,又具有汇编语言的特点.它由美国贝尔实验室的Dennis M. Ritchie于1972年推出,1978年后,C语言已先后被 ...

  9. lldp中与snmp相关内容agentx

    struct lldpd { int    g_snmp; struct event  *g_snmp_timeout; void   *g_snmp_fds; char   *g_snmp_agen ...

  10. C#在Json反序列化中处理键的特殊字符

    假设有如下Json 数据: 1.{ 2."id" : 1, 3."@value" : "this a @", 4."$p" ...