暴力搜索。

#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. k个区间相交的段落数 Educational Codeforces Round 4 D

    http://codeforces.com/contest/612/problem/D 题目大意:给你n个区间,这n个区间会有相交的部分,如果一个区间相交的部分>=k,那么就把这个区间记录下来. ...

  2. 十三、oracle 数据字典和动态性能视图

    一.概念数据字典是oracle数据库中最重要的组成部分,它提供了数据库的一些系统信息.动态性能视图记载了例程启动后的相关信息. 二.数据字典1).数据字典记录了数据库的系统信息,它是只读表和视图的集合 ...

  3. 终于下决心在cnblogs上安家了,^_^

    以前在这个地方学到了很多东西,希望在这里安家以后,自己也可以有很多成长. mark一下,~~

  4. oracle日期的处理

    字符长转换为日期,有两种写法 SELECT date'2016-06-03' FROM dualselect to_date('2016-06-03','yyyy-mm-dd') from dual ...

  5. Bourn Again Shell编程

    shell既是命令解释程序,又是一种高级程序设计语言.shell是解释型语言. bash脚本的建立和运行: 注释行以#开头 #!后面的参数告诉系统执行本文件的程序 执行脚本文件有两种方法: 1.   ...

  6. 安卓Activity组件

     Activity生命周期 熟悉javaEE的朋友们都了解servlet技术,我们想要实现一个自己的servlet,需要继承相应的基类,重写它的方法,这些方法会在合适的时间被servlet容器调用.其 ...

  7. iOS模拟器沙盒使用推荐

    iOS沙盒路径的查看和使用 1.模拟器沙盒目录文件都在个人用户名文件夹下的一个隐藏文件夹里,中文叫资源库,他的目录其实是Library.因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制 ...

  8. cPanel 安装方法

    . 如何安装cPanel/WHM? 答:安装过程较长,建议使用screen或nohupscreen: yum -y install screen perl screen -S cpanel cd /h ...

  9. 利用JFreeChart绘制股票K线图完整解决方案

    http://blog.sina.com.cn/s/blog_4ad042e50100q7d9.html 利用JFreeChart绘制股票K线图完整解决方案 (2011-04-30 13:27:17) ...

  10. OpenCV中Mat的列向量归一化

    OpenCV中Mat的列向量归一化 http://blog.csdn.net/shaoxiaohu1/article/details/8287528 OpenCV中Mat的列向量归一化 标签: Ope ...