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 Kpositive 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 (≤), K (≤) and P (1). 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 1, or 1, 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 { , } is said to be larger than { , } if there exists 1 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
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int n, k, p, maxFacSum = -;//maxFacSum用来记录最大底数之和
vector<int>fac, ans, temp;//最大底数不超过n的数,底数最优数序列,临时存放
void DFS(int index, int nowK, int sum, int facSum)
{
if (sum == n && nowK == k)//统计因素个数
{
if (facSum > maxFacSum)//更优的组合
{
ans = temp;
maxFacSum = facSum;
}
return;
}
if (sum > n || nowK > k)return;//超出限制
if (index - >= )//给出数组小角标的限制
{
temp.push_back(index);//记录数据
DFS(index, nowK + , sum + fac[index], facSum + index);//选
temp.pop_back();//弹出数据
DFS(index - , nowK, sum, facSum);//不选
}
}
int main()
{
cin >> n >> k >> p;
for (int i = ; pow(i, p) <= n; ++i)
fac.push_back(pow(i, p));//初始化底数不超过n的因素
DFS(fac.size() - , , , );//为了得到最大的因素数组,从最后一位开始向前搜索
if (maxFacSum == -)
cout << "Impossible" << endl;//没有找到满足的序列
else
{
cout << n << " = ";
for (int i = ; i < ans.size(); i++)
cout << ans[i] << "^" << p << (i == ans.size() - ? "" : " + ");
}
return ;
}
PAT甲级——A1103 Integer Factorization的更多相关文章
- PAT甲级1103. Integer Factorization
PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...
- PAT甲级——1103 Integer Factorization (DFS)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...
- PAT甲级1103 Integer Factorization【dfs】【剪枝】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 题意: 给定一个数n,要求从1~n中找 ...
- PAT A1103 Integer Factorization (30 分)——dfs,递归
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
线性dfs,注意每次深搜完状态的维护~ #include<bits/stdc++.h> using namespace std; ; vector<int> v,tmp,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 K positi ...
- 【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 甲级 1113 Integer Set Partition
https://pintia.cn/problem-sets/994805342720868352/problems/994805357258326016 Given a set of N (> ...
- PAT甲级——A1113 Integer Set Partition
Given a set of N (>) positive integers, you are supposed to partition them into two disjoint sets ...
随机推荐
- HTTP入门简介
一.概念:Hyper Text Transfer Protocol 超文本传输协议 传输协议:定义了客户端和服务器端通信时,发送数据的格式 特点: 1.基于TCP/IP的高级协议 2.默认端口号:80 ...
- Nginx安装及分流多个web服务
Ngnix安装及常用配置 一.安装Nginx 1.检查依赖 yum install gcc-c++ yum install -y pcre pcre-devel yum install -y zlib ...
- Leetcode274.H-IndexH指数
原题的中文翻译不是很好,所以给出英文版. Given an array of citations (each citation is a non-negative integer) of a rese ...
- Django项目基础配置和基本使用
博文配置内容包括: django项目的创建 django项目下应用的创建及配置 数据库的配置 templates模板的配置 命令行创建项目: 在需要的目录下创建Django项目输入命令:django- ...
- centos7 RPM命令安装操作
RPM 安装操作 命令: rpm -i 需要安装的包文件名 举例如下: rpm -i example.rpm 安装 example.rpm 包: rpm -iv example.rpm 安装 exam ...
- CF850E Random Elections
题意:一共有n个人,要在三个人中选prefer,一开始他们心中都会想好他们的排名(共6种),之后给出的判断不会矛盾.规则如下:一共有三轮,分别是a->b,b->c,c->a,每个人选 ...
- 「题解」:$Simple$
问题 A: $Simple$ 时间限制: 1 Sec 内存限制: 256 MB 题面 题面谢绝公开. 题解 不算数学的数学题?? 直接枚举会重.$60%$两种算法:1.无脑$vis$数组记录.2.$ ...
- 最大流任务调度+离散化——hdu2883
思想就是把时间段离散化,然后用个点来表示一段时间 #include<iostream> #include<cstdio> #include<cstring> #in ...
- kubernetes监控和性能分析工具:heapster+influxdb+grafana
1.部署heapster 下载 heapster 相关 yaml 文件 [root@master dashboard]# wget https://raw.githubusercontent.com/ ...
- STM32硬件错误HardFault_Handler的处理方法
https://blog.csdn.net/electrocrazy/article/details/78173558