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 ...
随机推荐
- Spring-Boot中如何使用多线程处理任务
看到这个标题,相信不少人会感到疑惑,回忆你们自己的场景会发现,在Spring的项目中很少有使用多线程处理任务的,没错,大多数时候我们都是使用Spring MVC开发的web项目,默认的Controll ...
- python包的安装
Microsoft Windows [版本 10.0.17134.228] (c) 2018 Microsoft Corporation.保留所有权利. C:\Users\Administrator& ...
- VBS脚本完美实现开机延时启动
目录 概述 vbs内容示例: vbs示例语句分析 自定义vbs脚本 一些问题和解决方法 概述 系统开机时,顺带自动启动了不少驱动程序,使得电脑开机后鼠标要呆滞许久.为了加快windows的开机速度 ...
- 【luoguP3868】猜数字
description 现有两组数字,每组k个,第一组中的数字分别为:a1,a2,...,ak表示,第二组中的数字分别用b1,b2,...,bk表示.其中第二组中的数字是两两互素的.求最小的非负整数n ...
- thinkphp 使用php代码
Php代码可以和标签在模板文件中混合使用,可以在模板文件里面书写任意的PHP语句代码 ,包括下面两种方式: 直线电机选购 第一种:使用php标签 例如: <php>echo 'Hello, ...
- kubernetes istio之gateway
[root@master istio-]# kubectl apply -f samples/httpbin/httpbin.yaml service/httpbin created deployme ...
- tornado接收ajax的post请求报错WARNING:tornado.access:405 OPTIONS /add
后端报错信息 WARNING:tornado.access:405 OPTIONS /add (::1) 1.00m 前端报错信息 2xhr.js?ec6c:172 OPTIONS http://lo ...
- springboot跨域问题解决
package com.qif.xdqdm.config; import org.springframework.context.annotation.Bean; import org.springf ...
- js 实现 map 工具类
/* * MAP对象,实现MAP功能 * * 接口: * size() 获取MAP元素个数 * isEmpty() 判断MAP是否为空 * clear() 删除MAP所有元素 * put(key, v ...
- dart 异步
使用异步有两种方法 then 或者 async/await. async/await 方法更易于理解,