PAT A1103 Integer Factorization (30分) (DFS+剪枝)_pat a1103测试点34_Jin_zc的博客-程序员秘密

技术标签: dfs  算法  PAT  剪枝  数据结构  

PAT甲级:A1103 Integer Factorization (30分)

The KP factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the KP factorization of N for any positive integers N, K and P.

Input Specification:

Each input file contains one test case which gives in a line the three positive integers N (≤400), K (≤N) and P (1<P≤7). The numbers in a line are separated by a space.

Output Specification:

For each case, if the solution exists, output in the format:

N = n[1]^P + ... n[K]^P

where n[i] (i = 1, …, K) is the i-th factor. All the factors must be printed in non-increasing order.

Note: the solution may not be unique. For example, the 5-2 factorization of 169 has 9 solutions, such as 122+42+22+22+12, or 112+62+22+22+22, or more. You must output the one with the maximum sum of the factors. If there is a tie, the largest factor sequence must be chosen – sequence { a1,a2,⋯,aK } is said to be larger than { b1,b2,⋯,bK } if there exists 1≤LK such that ai=bi for i<L and aL>bL.

If there is no solution, simple output Impossible.

Sample Input 1:

169 5 2

Sample Output 1:

169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2

Sample Input 2:

169 167 3

Sample Output 2:

Impossible
  • 题意:整数因式分解,将一个整数N拆成K个数的P次方和,这里的P也是整数,如果找到符合的因数有多个则输出数字字典序较大的,例如 [10, 2, 1] 大于 [9, 9, 9],因为第一个10大于9。
  • 分析:本题考察DFS深度优先搜索,首先对于要满足的K的数的P次方和等于N,这所拆分的这几个数中最大的数要小于N,那么对于因子来说,它的P次方一样要小于N,因此在寻找这些数的时候,应该从N开P次方的数开始向下找,同时,题目要求输出满足结果中序列较大的那个,那么在DFS的时候就应该从可能的最大的数开始往下寻找。而在DFS时,存在两个分支,分别是当前数的选与不选,“死胡同”则是,当前记录的sum大于N、cntK大于K或者当前数X小于1时候,如果出现以上情况则返回。

注意:DFS时因为总是要用pow函数重复计算某个数的P次幂,导致存在一个样例超时,所以需要在深搜前把可能用到的数的P次幂先计算出来,按照下标依次存储。

#include <bits/stdc++.h>
using namespace std;
int n, k, p, maxFacSum = -1;
vector<int> temp, ans, fac;
void dfs(int x, int sum, int facSum, int cntK) {
    
    if (x < 1 || sum > n || cntK > k) return;
    if (sum == n && cntK == k) {
    
        if (facSum > maxFacSum) {
    
            ans = temp;
            maxFacSum = facSum;
        }
        return;
    }
    temp.push_back(x);
    dfs(x, sum + fac[x], facSum + x, cntK + 1);
    temp.pop_back();
    dfs(x - 1, sum, facSum, cntK);
}
int main() {
    
    scanf("%d%d%d", &n, &k, &p);
    int i = 0, temp = 0;
    while (temp <= n) {
    
        fac.push_back(temp);
        temp = pow(++i, p);
    }
    dfs((int)fac.size() - 1, 0, 0, 0);
    if (ans.size() == 0) printf("Impossible\n");
    else {
    
        printf("%d = ", n);
        for (int i = 0; i < ans.size(); i++) {
    
            if (i != 0) printf(" + ");
            printf("%d^%d", ans[i], p);
        }
    }
    return 0;
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/charjindev/article/details/104564000

智能推荐

win10 linux(wsl子系统)删除方法_如何删除linux wsl子系统_heyjude0919的博客-程序员秘密

管理员账号运行Powershell1,查看已经安装软件包:Get-AppxPackage -allusers | Select Name, PackageFullName2,删除对于的安装软件包:get-appxpackage CanonicalGroupLimited.Ubuntu16.04onWindows | remove-Appxpackage3,重启电脑后重新安装...

el-upload删除已选图片存在问题_el-upload handleremove_程序员劝退师-TAO的博客-程序员秘密

前言昨天写了JAVA整合阿里云OSS/VUE上传阿里云OSS这篇文章,上传是没有问题了,但是在删除的时候有点问题,如果在只上传单张图片的场景下,el-upload是很方便搞定的,但是如果是多张图片,上传是没问题,但是如果上传后的图片要做删除操作还是有点恶心的,本文主要针对VUE端,废话先不多说,场景引入一下!场景引入代码如下&lt;template&gt; &lt;div&gt; &lt;el-upload :limit="4"

JS 页面点击桃心特效_js桃心_隐身的稻草人的博客-程序员秘密

在页面中直接引入以下代码即可添加特效:文件名:ScarecrowClickHeart.js//鼠标点击出现爱心特效(function(window,document){varhearts=[];/***初始化动画循环事件*/window.requestAnimationFrame=(function(){...

SpringBoot-ElasticSearch5.6.8操作_spring boot 5.6.8 es_胡安民的博客-程序员秘密

SpringBoot-ElasticSearch5.6.8操作使用IDEA创建elasticsearch-study Maven工程添加Maven &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;org.elasticsearch&lt;/groupId&gt; &lt;artifactId&gt;elasticsearch&lt;/artifactId&g.

arch linux编译,archlinux下编译新内核_柠锘的博客-程序员秘密

# Maintainer: Thomas Baechler #pkgname=kernel26 # Build stock -ARCH kernelpkgname=kernel26-good # Build kernel with a different name_kernelname=${pkgname#kernel26}_basekernel=2.6....

win7c盘空间越来越小_C盘容量越来越小?这是因为你不知道这些方法,解决你C盘空间问题..._weixin_39728909的博客-程序员秘密

Hello大家好,我是兼容机之家的小牛!不知道大家有没有这样的感觉,电脑在使用一段时间以后,C盘的容量几乎都快见底了,记得刚装系统的时候,C盘还是有非常大的空间富余,为什么越用C盘的容量就会不断的减少呢?C盘空间不足的坏处想必我不用多说,登个扣扣都会提示“磁盘已满”。至于造成C盘空间不断减少,这里面的原因有很多,垃圾文件就会吞噬你的C盘空间!下面我就来总结四种方法,教大家如何防范C盘空间不足的情况...

随便推点

word论文排版和写作06:审阅和修改文章_Jeremy_权的博客-程序员秘密

文章的写作不是一蹴而就的,正式的文章往往需要经过多轮的反复修改才能得以最终定稿。在此介绍word的审阅功能,来高效地完成修改和查看他人修改意见的过程。关于word的其他论文排版技巧,可以参见之前的word论文排版和写作一文。

手动mysql注入+post请求注入(学习) 2018/11/13_Gypsies_97的博客-程序员秘密

sql注入中Mysql常用的数据库:information_schema.tables ; 记录了数据库中所有的表名information_schema.columns; 记录了数据库中所有的列名在有错误回显的注入过程中,注入过程大致如下:利用‘id = 1 and 1=1 ’&amp;amp;amp;&amp;amp;amp;‘id =1 and 1=2’ 判断是否存在注入点。利用order by 语句确认...

OpenGL(十四)——Qt OpenGL纹理_opengl 纹理_冯一川的博客-程序员秘密

纹理(Texture)的本质是一个2D图片(1D和3D),或者叫图形数据。只是在OpenGL中专业术语中称其为纹理。你可以这样理解纹理,你家房子装修,你想要在不同的房间贴上不同风格的墙纸,有科技感的,有雍容华贵的,还有动漫的等等,此时的墙纸就是我们所说的纹理了。......

element-ui 表格 取消鼠标悬停高亮效果_饿了么ui不要鼠标悬浮高亮效果_「已注销」的博客-程序员秘密

.el-table__row:hover &gt; td { background-color: #ffffff !important;}.el-table__row--striped:hover &gt; td { background-color: #fafafa !important;}

android canvas光晕绘制_Android 绘制发光效果_weixin_39979215的博客-程序员秘密

之前在看别人写自定义view作绘制的时候,看到别人家的view自带发光效果,看起来也是蛮炫酷的,于是自己也抽出时间来试用一下,这里做了一个模仿太阳的各种状态样式。先上效果先上效果:实现方式:public BlurMaskFilter(float radius, Blur style) {实现是使用的Paint类的setMaskFilter()方法,传入BlurMaskFilter对象实现高斯模糊发...

Java基础-IO流对象之字符类(FileWrite与FileReader)_weixin_33881041的博客-程序员秘密

           Java基础-IO流对象之字符类(FileWrite与FileReader)                                      作者:尹正杰版权声明:原创作品,谢绝转载!否则将追究法律责任。一.常见编码简介1&gt;ASCII  我们知道计算机是由外国人发明的,他们当时也没有考虑到全球都用到计算机,因此在设计编码的时候压...

推荐文章

热门文章

相关标签