PAT甲级1103 Integer Factorization【dfs】【剪枝】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224
题意:
给定一个数n,要求从1~n中找出k个数,使得这些数的p次方之和等于n
思路:
因为n为400,所以dfs加剪枝【本来还在想dp来着】
他要求输出的方案是数字之和最大的,如果之和相等要输出字典序较大的。
所以还需维护一个数字之和。
一个剪枝的方法是从大到小进行dfs,后面被选的数一定比前面被选的要小,这里不限制的话显然会出现重复。
如果从大到小进行dfs的话,tmpsum=anssum的时候,后面的方案一定不如前面的方案,所以这里不取等号
#include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int n, k, p;
vector<int>ans, tmp;
int anssum = , tmpsum = ; int ppow[]; void init()
{
for(int i = ; i <= n; i++){
ppow[i] = pow(i, p);
}
} void dfs(int i, int num, int sum)
{
if(num == ){
if(sum == && tmpsum > anssum){
anssum = tmpsum;
ans = tmp;
}
return;
}
if(sum < )return; for(int j = i; j >= ; j--){
tmp.push_back(j);
tmpsum += j;
dfs(j, num - , sum - ppow[j]);
tmp.pop_back();
tmpsum -= j;
}
} int main()
{
scanf("%d%d%d", &n, &k, &p);
init();
dfs(n, k, n);
//printf("%d\n", ans.size());
if(ans.size() != k){
printf("Impossible\n");
}
else{ printf("%d = %d^%d", n, ans[], p);
for(int i = ; i < k; i++){
printf(" + %d^%d", ans[i], p);
}
printf("\n");
}
return ;
}
PAT甲级1103 Integer Factorization【dfs】【剪枝】的更多相关文章
- PAT甲级——1103 Integer Factorization (DFS)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...
- PAT甲级1103. Integer Factorization
PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...
- 【PAT】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 ...
- PAT甲级——A1103 Integer Factorization
The K−P factorization of a positive integer N is to write N as the sum of the P-th power of Kpositiv ...
- PAT 1103 Integer Factorization[难]
1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...
- 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. ...
- 【PAT甲级】1103 Integer Factorization (30 分)
题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...
- PAT 1103 Integer Factorization
The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...
随机推荐
- cmd 命令添加防火墙端口
windows dos 命令添加防火墙端口. 示例 123 端口: netsh firewall add portopening protocol = UDP port = name = NTPSER ...
- 使用HBuilder+MUI+Flask后端服务器框架+Mongodb数据库开发手机APP
1.开发准备HBuilder: 1.下载安装HBuilder2.创建APP项目 3.代码示例: <!doctype html> <html lang="en"&g ...
- android:shape 设置圆形
组件高度和宽度设置为相同的值即可<?xml version="1.0" encoding="utf-8"?><shape xmlns:andr ...
- linux服务器安装Mysql后,只能看到information_schema/test这两个库,无法修改密码
参考链接:https://www.cnblogs.com/ThinkVenus/p/7670722.html 问题背景:登录mysql失败,密码错误,由此想到需要更改密码 然而,进入数据库后,只能看到 ...
- Easyui datalist 使用记录
仅简单记录下,资料相对比较少 官方给了一个很简单的例子,没啥用处,文档:http://www.jeasyui.com/documentation/datalist.php 学习要点: 1.追加行 $( ...
- 帆软报表(finereport)动态列查询
新建普通报表,新建一个数据集ds1,sql语句:select * from 销量 设计模板:选择复选按钮组控件 设置 控件名称 paraed , 控件值设置为公式:["列名",& ...
- @Transactional 无效原因
在controller 上面使用 @Transactional 注解时候发现数据没有回滚,在执行完update 更新语句,事务直接就commit 了, 此时方法尚未执行结束,数据库数据已经更新了. ...
- Bag of Tricks for Image Classification with Convolutional Neural Networks论文笔记
一.高效的训练 1.Large-batch training 使用大的batch size可能会减小训练过程(收敛的慢?我之前训练的时候挺喜欢用较大的batch size),即在相同的迭代次数 ...
- python实现目录大小计算(含子目录)
path=r"F:\\PYTHON\\day8\\ftp_server\\"#这里我直接写了固定地址,你可以自己输入 file_size = 0 def split_doc(pat ...
- 开启Golang编程第一章
Go is an open source programming language that makes it easy to build simple,reliable, and effcient ...