PAT甲级:A1103 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 positive integers. You are supposed to write a program to find the K−P factorization of N for any positive integers N, K and P.
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.
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≤L≤K such that ai=bi for i<L and aL>bL.
If there is no solution, simple output Impossible
.
169 5 2
169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2
169 167 3
Impossible
注意: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;
}
管理员账号运行Powershell1,查看已经安装软件包:Get-AppxPackage -allusers | Select Name, PackageFullName2,删除对于的安装软件包:get-appxpackage CanonicalGroupLimited.Ubuntu16.04onWindows | remove-Appxpackage3,重启电脑后重新安装...
前言昨天写了JAVA整合阿里云OSS/VUE上传阿里云OSS这篇文章,上传是没有问题了,但是在删除的时候有点问题,如果在只上传单张图片的场景下,el-upload是很方便搞定的,但是如果是多张图片,上传是没问题,但是如果上传后的图片要做删除操作还是有点恶心的,本文主要针对VUE端,废话先不多说,场景引入一下!场景引入代码如下<template> <div> <el-upload :limit="4"
在页面中直接引入以下代码即可添加特效:文件名:ScarecrowClickHeart.js//鼠标点击出现爱心特效(function(window,document){varhearts=[];/***初始化动画循环事件*/window.requestAnimationFrame=(function(){...
SpringBoot-ElasticSearch5.6.8操作使用IDEA创建elasticsearch-study Maven工程添加Maven <dependencies> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId&g.
# Maintainer: Thomas Baechler #pkgname=kernel26 # Build stock -ARCH kernelpkgname=kernel26-good # Build kernel with a different name_kernelname=${pkgname#kernel26}_basekernel=2.6....
Hello大家好,我是兼容机之家的小牛!不知道大家有没有这样的感觉,电脑在使用一段时间以后,C盘的容量几乎都快见底了,记得刚装系统的时候,C盘还是有非常大的空间富余,为什么越用C盘的容量就会不断的减少呢?C盘空间不足的坏处想必我不用多说,登个扣扣都会提示“磁盘已满”。至于造成C盘空间不断减少,这里面的原因有很多,垃圾文件就会吞噬你的C盘空间!下面我就来总结四种方法,教大家如何防范C盘空间不足的情况...
文章的写作不是一蹴而就的,正式的文章往往需要经过多轮的反复修改才能得以最终定稿。在此介绍word的审阅功能,来高效地完成修改和查看他人修改意见的过程。关于word的其他论文排版技巧,可以参见之前的word论文排版和写作一文。
sql注入中Mysql常用的数据库:information_schema.tables ; 记录了数据库中所有的表名information_schema.columns; 记录了数据库中所有的列名在有错误回显的注入过程中,注入过程大致如下:利用‘id = 1 and 1=1 ’&amp;amp;&amp;amp;‘id =1 and 1=2’ 判断是否存在注入点。利用order by 语句确认...
纹理(Texture)的本质是一个2D图片(1D和3D),或者叫图形数据。只是在OpenGL中专业术语中称其为纹理。你可以这样理解纹理,你家房子装修,你想要在不同的房间贴上不同风格的墙纸,有科技感的,有雍容华贵的,还有动漫的等等,此时的墙纸就是我们所说的纹理了。......
.el-table__row:hover > td { background-color: #ffffff !important;}.el-table__row--striped:hover > td { background-color: #fafafa !important;}
之前在看别人写自定义view作绘制的时候,看到别人家的view自带发光效果,看起来也是蛮炫酷的,于是自己也抽出时间来试用一下,这里做了一个模仿太阳的各种状态样式。先上效果先上效果:实现方式:public BlurMaskFilter(float radius, Blur style) {实现是使用的Paint类的setMaskFilter()方法,传入BlurMaskFilter对象实现高斯模糊发...
Java基础-IO流对象之字符类(FileWrite与FileReader) 作者:尹正杰版权声明:原创作品,谢绝转载!否则将追究法律责任。一.常见编码简介1>ASCII 我们知道计算机是由外国人发明的,他们当时也没有考虑到全球都用到计算机,因此在设计编码的时候压...