PAT_A1103#Integer Factorization
Source:
Description:
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 thei
-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=bifor 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
Keys:
Attention:
- 不同的编译环境pow函数的精度不同,PAT跑的数据是对的,但我电脑上跑出来是错的,可以自己写一个
Code:
/*
time: 2019-07-02 18:55:08
problem: PAT_A1103#Integer Factorization
AC: 18:08 题目大意:
将整数N分解为以P为指数的K个因式的和
输入:
正整数N<=400,因式个数K,指数1<P<=7
输出:
按底数递减,
若不唯一,打印底数和最大的一组,
若不唯一,打印字典序较大的一组 基本思路:
深度优先搜索,至第K层时若存在解,则选择最优解
*/
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
int k,n,p,optValue=;
vector<int> fac,temp,ans; void DFS(int index, int numK, int sum, int sumFac)
{
if(numK==k && sum==n && sumFac>optValue)
{
optValue = sumFac;
ans = temp;
}
if(numK>=k || sum>=n || index<=)
return;
temp.push_back(index);
DFS(index,numK+,sum+fac[index],sumFac+index);
temp.pop_back();
DFS(index-,numK,sum,sumFac);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d%d%d", &n,&k,&p);
for(int i=; pow(i,p)<=n; i++){
fac.push_back(pow(i,p));
}
DFS(fac.size()-,,,);
if(ans.size() == )
printf("Impossible");
else
{
printf("%d = %d^%d", n,ans[],p);
for(int i=; i<ans.size(); i++)
printf(" + %d^%d", ans[i],p);
} return ;
}
PAT_A1103#Integer Factorization的更多相关文章
- PAT 1103 Integer Factorization[难]
1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...
- 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 ...
- 1103 Integer Factorization (30)
1103 Integer Factorization (30 分) The K−P factorization of a positive integer N is to write N as t ...
- 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 (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 ...
- 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 ...
- 1103 Integer Factorization (30)(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 ...
随机推荐
- 改进持续交付中的CI环节
改进持续交付中的CI环节 在当前 DevOps 的趋势下,持续集成(CI)和持续部署(CD)具有支柱性地位,那么能够成功搭建 CI/CD 流水线就至关重要了. 今天我就讲一讲如何做好CI部分,让我们的 ...
- POJ 1797 Heavy Transportation (最大生成树)
题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...
- 极致CMS建站系统后台GETSHELL
起因 正在学习代码审计 看到有人提交了一个注入https://www.cnvd.org.cn/flaw/show/CNVD-2019-42775 想试试看还有没有别的漏洞 受影响版本 v1.6.3 - ...
- vue父组件与子组件之间的数据传递
父组件向子组件传递数据 父组件用数据绑定:子组件用props接收 <!-- test-vue-model父组件 --> <template> <div> <m ...
- send_keys报错element not interactable
这两天要在阿里云日志中操作UI,以输入关键字搜索日志. 在send_keys时报错element not interactable. iframe 第一个问题是iframe,通过切换iframe解决: ...
- 跨域篇--JSONP原理
一篇文章让你明白 jsonp原理详解 什么是JSONP? 先说说JSONP是怎么产生的: 其实网上关于JSONP的讲解有很多,但却千篇一律,而且云里雾里,对于很多刚接触的人来讲理解起来有些困难,着用自 ...
- 嵌套循环结合修改IFS环境变量遍历文件数据中IFS的修改一致性
以下这个脚本使用了两个不同的IFS值来解析数据.第一个IFS值解析出/etc/passwd文件中的单独的行.内部for循环接着将IFS值改为冒号,云溪你从/etc/passwd的行中解析出单独的值. ...
- cordova 配置 高德地图sdk定位
获取 sha1 打开 cmd 1. cd .android 2. keytool -list -v -keystore debug.keystore 3. 输入密钥: android 4. 复制 sh ...
- ASE: CODEnn Reproduce
Background 第二次结对编程的任务是挑选一个用自然语言搜索相关代码片段的模型实现,并且可以提出自己的想法改进.这个任务很cool,前期做了不少调研.使用自然语言搜索相关代码片段现在是个很受关注 ...
- CodeForces - 337D 树形dp
题意:一颗树上有且仅有一只恶魔,恶魔会污染距离它小于等于d的点,现在已经知道被污染的m个点,问恶魔在的可能结点的数量. 容易想到,要是一个点到(距离最远的两个点)的距离都小于等于d,那么这个点就有可能 ...