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 ...
随机推荐
- Choreographer解析
Choreographer_舞蹈编导 为什么叫舞蹈编导,因为舞蹈是由节奏的,节奏是每个点位动作的快慢控制,跳舞时节奏很重要,编舞者控制节奏.视图刷新也是如此,不是说你想刷就能刷,一切要按照底层信号要求 ...
- Prisma GraphQL 服务器 生产者 "https://www.prisma.io"
Prisma 一个 GraphQL 服务器 生产者 "https://www.prisma.io" , 关注一下
- Revit二次开发之获得项目族预览图
using Autodesk.Revit.UI; using Autodesk.Revit.DB; using Autodesk.Revit.Attributes; using System.Wind ...
- lnmp.org 安装环境的,root权限都没法删除网站文件夹,问题解决-转
rm -rf删除网站目录时出现rm: cannot remove `.user.ini': Operation not permitted rm -rf删除网站目录时出现rm: cannot remo ...
- Three teachers who make differences in my life
人生路漫漫,从小到大,经历过九年义务教育的我们也受到过很多老师的熏陶,而已经进入大学的我也会回想起那些给我带来别样意义的老师们. 亲爱的老李-老李是我初中的班主任,也是我的历史老师.依稀记得上他的课会 ...
- js编码解码 punyCode
;(function(w) { var PunycodeModule = function () { function IdnMapping() { this.utf16 = { decode: fu ...
- 学习笔记: yield迭代器
yield 与 IEnumerable<T> 结对出现, 可实现按需获取 , 迭代器模式 static void Main(string[] args) { ...
- Vue 增删改查 demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- UIWebView的常用方法
//webview导航栏类型enum UIWebViewNavigationType : Int { case LinkClicked case FormSubmitted case BackForw ...
- PyCharm使用秘籍
PyCharm的基本使用 在PyCharm下为你的Python项目配置Python解释器 Project:当前项目名>Project Interpreter>add Local 在PyCh ...