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< ...
随机推荐
- 简单学习JavaScript面向对象编程
JavaScript是一种弱类型语言.有一种原型机制. 1.创建一个空对象:var bill = {}; 给这个对象添加属性和方法: bill.name = "Bill E Goat&quo ...
- SQL Server服务开闭
SQL Server(MSSQLSERVER)是必须要开启的,这个是数据库引擎服务,就像汽车的发动机一样. SQL Server代理(MSSQLSERVER)是代理服务,比如你有一些自动运行的,定时作 ...
- JavaScript获取浏览器信息的方法
Window有navigator对象让我们得知浏览器的全部信息.我们可以利用一系列的API函数得知浏览器的信息. JavaScript代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 1 ...
- (转)用JMX监测JVM的运行参数
翻译自http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html 用JMX管理你的JVMJMX是Java管理扩展 ...
- android 联网
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="htt ...
- svn hooks的使用demo
我是理论家: svn server端提供了Hooks Script.所谓钩子实际上是一种时间触发机制,是指当系统执行到某个特殊的事件时,触发我们预定义的动作,可以让我们在某些特定状态发生的时候做我们想 ...
- VBS脚本合集(自制脚本)
一.视频搜索器选择: Selectitem() '######################################### Function Selectitem() 'Set Vedio ...
- 在选定的数据源上未找到名为“TitleSub”的字段或属
在.NET开发过程中时常会遇到“在选定的数据源上未找到名为“TitleSub”的字段或属性”的错误”,导致这类错误的原因有很多,在我的项目中,详细情况是这样:1.有两个控件:DropDownList类 ...
- Oracle小技术集锦
- jQuery第一章
一.jQuery的优势 1.轻量级:压缩之后大小只有30KB左右. 2.强大的选择器:jQuery允许CSS1到CSS3几乎所有的选择器以及自身独创的选择器. 3.出色的DOM操作的封装:jQuery ...