1103. Integer Factorization (30)
时间限制
1200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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.
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 = n1^P + ... nK^P
where ni (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".
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
C++:
/*@Date : 2018-02-22 19:04:55@Author : 酸饺子 (changzheng300@foxmail.com)@Link : https://github.com/SourDumplings@Version : $Id$
*//*
https://www.patest.cn/contests/pat-a-practise/1103*/#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>using namespace std;static const int MAXN = 401;
static vector<int> powerData(401);
static vector<int> thisFactors, resFactors;
static int thisFactorSum = 0, maxFactorSum = 0;
static int lastFactor; // 后一个因子不能大于前一个因子void DFS(int N, int K, int P)
{if (K == 0){if (N == 0){if (thisFactorSum > maxFactorSum){maxFactorSum = thisFactorSum;resFactors = thisFactors;}else if (thisFactorSum == maxFactorSum && thisFactors > resFactors)resFactors = thisFactors;}}else if (N != 0){int tempLastFactor = lastFactor;for (int i = 1; i <= lastFactor; ++i){if (powerData[i] > N) break;thisFactorSum += i;thisFactors.push_back(i);lastFactor = i;DFS(N - powerData[i], K - 1, P);lastFactor = tempLastFactor;thisFactorSum -= i;thisFactors.pop_back();}}return;
}int my_power(int x, int e)
{int res = x;for (int i = 2; i != e + 1; ++i)res *= x;return res;
}int main(int argc, char const *argv[])
{int N, K, P;scanf("%d %d %d", &N, &K, &P);for (int i = 1; i <= N; ++i)powerData[i] = my_power(i, P);lastFactor = N;DFS(N, K, P);if (!resFactors.empty()){int output = 0;printf("%d =", N);for (int f : resFactors){if (output++) printf(" +");printf(" %d^%d", f, P);}putchar('\n');}elseprintf("Impossible\n");return 0;
}
1103. Integer Factorization (30)相关推荐
- PAT甲级1103 Integer Factorization (30 分):[C++题解]背包问题,DP解法
文章目录 题目分析 题目链接 题目分析 分析 把N(样例中N=169)看成背包的体积:把k(样例中k=5)看成背包能承的重量.把这道题转化为二维完全背包问题.由于数据范围给出的次幂P∈[2,7],那么 ...
- 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 ...
- 1103 Integer Factorization (30 分)【难度: 中 / 爆搜】
https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 爆搜的做法,动态规划也可以做,有时间也一个动 ...
- 1103 Integer Factorization (30分)
第一个深度优先搜索的题目. 很显然这个就是分情况讨论,每次选和不选某值,最终将满足结果的序列筛选出来,我自己写的一个dfs遍历,我比较喜欢对每一个元素进行遍历,用循环控制,这样的好处是避免递归层数太多 ...
- 1103 Integer Factorization 需再做
本题是典型的DFS+剪枝 我对DFS有了更深的认识:整个过程就是一片森林(根节点不唯一)的生长,到了界限就得到结果并返回或者得不到结果也返回,DFS的参数存放的是所有需要积累的变量. 提示: 1. 最 ...
- PAT1103 Integer Factorization (30)(DFS:回溯)
题意: 给三个正整数N.K.P,将N表示成K个正整数(可以相同,递减排列)的P次方和,如果有多种方案,选择底数n1+-+nk最大的方案,如果还有多种方案,选择底数序列的字典序最大的方案 思路: 这题看 ...
- PAT A1103 Integer Factorization整数分解 (经典DFS优化)
题目 分析 题意 给定正整数N.K.P,将N表示成K个正整数(可以相同,递减排列)的P次方的和,即N=nP+-nkP.如果有多种方案,那么选择底数和n1+-+nK最大的方案,如果还有多种方案,那么选择 ...
- PAT甲级题目翻译+答案 AcWing(数学)
1059 Prime Factors (25 分) 题意 : 给一正整数,要求分解质因数 思路 : 使用is_first,来完成除了第一个质因数前都有*的效果 如果n=1,要特判 最后如果n>1 ...
- dev调试时无法进入下一步_【问题解决方案】Dev C++ 无法调试的问题与解决
浅谈ARP协议以及应用 0. 前言 本章主要简单的介绍ARP的协议格式,主机如何发送和处理ARP报文,以及免费ARP. 1. ARP协议原理 ARP,全称Address Resolution Prot ...
最新文章
- Open3d学习计划—高级篇 8(网格变形)
- 独家 | 关于Facebook数据泄露你需要知道的事
- 制作一个表格,显示班级的学生信息。
- 听说你想去大厂看学妹,带你看看字节跳动后端开发面试长啥样?
- Agile Development
- 开放下载!《iOS开发者必读资讯》
- ubuntu php 树莓派,树莓派3 安装ROS环境(ubuntu mate 16.04+kinetic)
- MySQL 引擎特性 · InnoDB Buffer Pool
- 芯片公司急聘嵌入式软件精英人才
- mysql命令实践_MySQL:常用命令行
- 30 CO配置-控制-产品成本控制-成本对象控制-期末结算-定义更新
- vue 判断一个数是否在数组中_高级前端进阶,vue如何实现$nextTick
- 计算机一级如何添加对角线,如何在表格中添加对角线
- 2020FME博客大赛——基于FME利用高德路径规划AP实现公共服务设施可达性分析——以厦门山海健康步道为例
- mc服务器物品给予,我的世界网易版给予物品指令1.12谢谢
- java画五角星_如何用Java程序写出五角星?
- 如何用计算机求素数,在线质数(素数)计算器
- 【Windows】怎么查看CUDA版本?Conda命令安装和NVIDIA官网安装包安装的CUDA有何区别?nvcc -V和nvidia-smi获得的CUDA版本有何区别?如何指定CUDA版本?
- Web全栈~06.CSS选择器
- 可口可乐“昵称瓶”营销操盘手解读社会化营销
热门文章
- GitHub与微信开启“秘密扫描”计划,来确保数据安全
- 标准差越大越集中_中国大学MOOC: 正态分布的标准差越大,其概率密度曲线越高越集中。...
- 近段时间参加的CTF竞赛部分题目复现(ISCC2020 、GKCTF、网鼎杯)
- Androidq下编译efr32mg21
- Scrum立会报告+燃尽图(Final阶段第七次)
- 计算机计算公式单组数据求乘法,(excel表格全部数据怎么乘以一个数啊)
- 【量化交易】永久投资组合,海龟交易法则阅读,回测与讨论
- Big Faceless Java PDF 阅读器
- Snipaste等截图px与浏览器内容px不一样
- 厦大计算机科学系培养方案,厦门大学计算机科学系本科生课程方案.doc