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 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 = 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 12​2​​+4​2​​+2​2​​+2​2​​+1​2​​, or 11​2​​+6​2​​+2​2​​+2​2​​+2​2​​, 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 { a​1​​,a​2​​,⋯,a​K​​ } is said to be larger than { b​1​​,b​2​​,⋯,b​K​​ } if there exists 1≤L≤K such that a​i​​=b​i​​ for i<L and a​L​​>b​L​​.

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

题目大意:输入一个数N,以及数量K,指数P,将N用K个数的N次方的和进行表示。如果相同输出因子数最小的,如果还相同,那么输出较大(字典序较大的)的那个。

//感觉好难,是不是dfs什么的?果然大佬觉得有趣的题目都这么难。

考点是DFS+剪枝。

代码来自:https://www.liuchuo.net/archives/2451

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int n, k, p, maxFacSum = -;
vector<int> v, ans, tempAns;
void init() {
int temp = , index = ;
while (temp <= n) {
v.push_back(temp);
temp = pow(index, p);
index++;
}
}
//index表示当前可用的数最大的下标。
//底数和tempSum
//个数要求tempK
//facSum底数积
void dfs(int index, int tempSum, int tempK, int facSum) {
if (tempK == k) {
if (tempSum == n && facSum > maxFacSum) {
ans = tempAns;//新解赋值
maxFacSum = facSum;
}
return;
}
while(index >= ) {//这个index从大到小的过程,就保证了是按字典序从大到小排列的。
if (tempSum + v[index] <= n) {
tempAns[tempK] = index;
dfs(index, tempSum + v[index], tempK + , facSum + index);
}
if (index == ) return;
index--;
}
}
int main() {
scanf("%d%d%d", &n, &k, &p);
init();
tempAns.resize(k);
dfs(v.size() - , , , );
if (maxFacSum == -) {
printf("Impossible");
return ;
}
printf("%d = ", n);
for (int i = ; i < ans.size(); i++) {
if (i != ) printf(" + ");
printf("%d^%d", ans[i], p);
}
return ;
}

//真的是学习了!

1.注意剪纸,dfs里需要有这一句:if (tempSum + v[index] <= n) 这样比进入下一层要开销小很多。

下面这个代码来自:https://www.cnblogs.com/chenxiwenruo/p/6119360.html

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
/**
其实可以预先把i^p<n的i都存储起来
**/
const int maxn=;
int res[maxn];
int ans[maxn];
int factor[maxn];
int fidx=;
int maxsum=;
bool flag=false;
int n,k,p;
/**
num为当前的总和
cnt为还剩几个i^p项,即当前的k
sum为各因子的总和,因为要取和最大的
last为上一个因子的索引,因为要保证因子从大到小输出,
所以dfs后一个因子在factor中的索引不能大于上一个
**/
void dfs(int num,int cnt,int sum,int last){
if(num==&&cnt==){
if(sum>maxsum){
flag=true;
for(int i=;i<=k;i++)
ans[i]=res[i];
maxsum=sum;
}
return;
}
else if(cnt==)
return;//计数用完了,但是不符合=n的要求
for(int i=last;i>=;i--){
int left=num-factor[i];
res[cnt]=i+;
dfs(left,cnt-,sum+i,i);
}
} int main()
{
scanf("%d %d %d",&n,&k,&p);
int tmp=;
fidx=;
//预先存储i^p<=n的i
while(tmp<=n){
factor[fidx]=tmp;
fidx++;
tmp=pow(fidx+,p);
}
int cnt=;
int last=fidx-; dfs(n,k,,last);
if(flag){
printf("%d =",n);
for(int i=k;i>=;i--){
printf(" %d^%d +",ans[i],p);
}
printf(" %d^%d",ans[],p);
}
else{
printf("Impossible");
} return ;
}

//但是这个目前有点问题,今晚修改一下,看问题出在哪,顺便也是学习了!

PAT 1103 Integer Factorization[难]的更多相关文章

  1. PAT 1103 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 ...

  2. PAT甲级1103. Integer Factorization

    PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...

  3. PAT甲级——1103 Integer Factorization (DFS)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...

  4. 1103 Integer Factorization (30)

    1103 Integer Factorization (30 分)   The K−P factorization of a positive integer N is to write N as t ...

  5. 【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 ...

  6. PAT (Advanced Level) 1103. Integer Factorization (30)

    暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  7. PAT甲级1103 Integer Factorization【dfs】【剪枝】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 题意: 给定一个数n,要求从1~n中找 ...

  8. PAT甲题题解-1103. Integer Factorization (30)-(dfs)

    该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + … nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. ...

  9. 【PAT甲级】1103 Integer Factorization (30 分)

    题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...

随机推荐

  1. jQuery缓存机制(四)

    Data封装的方法的后面四个方法 和 dataAttr方法阅读. Data.prototype = { key: function( owner ) {}, set: function( owner, ...

  2. Excel中用countif和countifs统计符合条件的个数 good

    countif单条件统计个数   1 就以下表为例,统计总分大于(包含等于)400的人数. 2 在J2单元格输入公式=COUNTIF(I2:I22,">=400") 3 回车 ...

  3. [转]Linux sendmail 详解

    Internet上最基本的服务,现在应该大部分人都有自己的邮箱吧,用的人多,但理解的人估计没多少,我自己以前也是常常用,但对其原理并不操心.今天就来操心下,进行个小总结 一.邮件服务的基本流程     ...

  4. nginx upstream 实现负载均衡

    如果Nginx没有仅仅只能代理一台服务器的话,那它也不可能像今天这么火,Nginx可以配置代理多台服务器,当一台服务器宕机之后,仍能保持系统可用.具体配置过程如下: 1. 在http节点下,添加ups ...

  5. 【咸鱼教程】BitmapLabel位图字体使用

    引擎版本3.2.6 教程目录一 为什么要使用位图字体二 如何使用位图字体2.1 TextureMerger制作位图字体2.2 exml中使用位图字体三 Demo源码 一  为什么要使用位图字体egre ...

  6. VS NuGet加载本地程序包

    NuGet是VS中非常实用的一个工具,我们可以通过它在线安装想要的程序包,只要右键点击解决方案中的项目的引用,在弹出的菜单中选择“管理NuGet程序包”,然后就可以通过在线搜索找到想要添加的程序包,下 ...

  7. Azure Redis 缓存的 ASP.NET 会话状态提供程序

    Azure Redis Cache 提供了一个会话状态提供程序,你可以使用其在缓存中(而不是内存中或在 SQL Server 数据库中)存储会话状态.要使用缓存会话状态提供程序,先首先配置缓存,然后使 ...

  8. 大话FLASH和EEPROM

    最近在看代码的时候,遇到了一个使用FLASH模拟EEPROM的情况,看到这个我当时是一脸蒙蔽啊,对于一个有时候连FLASH和EEPROM都分不清的人来说,怎么可能读懂用FLASH来模拟EEPROM呢? ...

  9. tomcat启动后,页面浏览时报错 Unable to compile class for JSP的解决方案【原创】

    问题描述: tomcat启动后,console正常,console中语句为: 信息: Server startup in 7291 ms   但浏览器访问首页面http://localhost:808 ...

  10. inline-blcok 之间的空白间隙

    前言: inline-blcok 布局时,通常情况下, inline-blocks 之间有空白,尽管通常我们是不想要的,毕竟不像padding或者margin一样好控制,如图: <div cla ...