PAT甲级1103. Integer Factorization

题意:

正整数N的K-P分解是将N写入K个正整数的P次幂的和。你应该写一个程序来找到任何正整数N,K和P的N的K-P分解。

输入规格:

每个输入文件包含一个测试用例,它给出一行三个正整数N(<= 400),

K(<= N)和P(1 <P <= 7)。一行中的数字用空格分隔。

输出规格:

对于每种情况,如果解决方案存在,输出格式如下:

N = n1 ^ P + ... nK ^ P

其中ni(i = 1,... K)是第i个因子。所有因素必须以不增加的顺序打印。

注意:解决方案可能不是唯一的。例如,

因子分解169有9种解决方案,如122 + 42 + 22 + 22 + 12,或112 + 62 + 22 + 22 + 22或更多。您必须输出具有因子的最大和的一个。如果有关系,则必须选择最大因子序列 - 序列{a1,a2,... aK}被认为大于{b1,b2,...

bK}如果存在1 <= L <= K,使得对于i <L和aL> bL,ai = bi

如果没有解决方案,简单输出“不可能”。

思路:

dfs + 剪枝。慎用stl。把vector换成数组就a了。。

ac代码:

C++

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map>
#include<unordered_set> using namespace std; int n, k, p;
int res[401];
int temp[401];
int maxsum = 0;
int mypow[21];
int last; void caculate(int n,int sum, int len)
{
if (n < 0 || len > k) return;
if (k == len && n == 0)
{
if (sum > maxsum)
{
maxsum = sum;
for (int i = 0; i < k; i++)
res[i] = temp[i];
}
return;
} //long long num;
for (int i = last; i >= 1; i--)
{
if (mypow[i] > n) continue;
if ((k - len) * mypow[i] < n) break;
temp[len] = i;
last = i;
caculate(n - mypow[i], sum + i,len + 1);
}
} //bool cmp(vector<int>& a, vector<int>& b)
//{
// for (int i = 0; i < k; i++)
// {
// if (a[i] != b[i]) return a[i] > b[i];
// }
//} int main()
{
scanf("%d %d %d", &n, &k, &p);
for (int i = 1; i <= 20; i++)
{
mypow[i] = i;
for (int j = 1; j < p; j++)
{
mypow[i] *= i;
}
} last = 20;
caculate(n, 0, 0); //printf("%d\n", res.size());
if (res[0] == 0) printf("Impossible\n");
else
{
printf("%d = ", n);
for (int i = 0; i < k - 1; i++)
printf("%d^%d + ", res[i],p);
printf("%d^%d\n", res[k - 1], p);
}
return 0;
}

PAT甲级1103. Integer Factorization的更多相关文章

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

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

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

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

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

  4. 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 Kpositiv ...

  5. PAT 1103 Integer Factorization[难]

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

  6. 1103 Integer Factorization (30)

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

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

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

  8. 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 ...

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

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

随机推荐

  1. 【nginx+tomcat集群】Nginx1.12.2+Tomcat7集群+负载均衡+Session共享

    今天想着将项目优化一下,就想的实现集群分布,在本机测试:利用nginx+tomcat实现 通过上一篇博客(http://www.cnblogs.com/qlqwjy/p/8535235.html),N ...

  2. 贪心算法_01背包问题_Java实现

    原文地址:http://blog.csdn.net/ljmingcom304/article/details/50310789 本文出自:[梁敬明的博客] 1.贪心算法 什么是贪心算法?是指在对问题进 ...

  3. Ubuntu连接多台Ubuntu server的问题

    如果您用的是虚拟机上安装的几个Ubuntu server进行IP配置 要注意以下几点: <1>虚拟机上安装完成Ubuntu server 默认的网络连接方式是NAT ,应该改成桥接网卡 ( ...

  4. CSS背景横向平铺BUG,解决方法

    给定DIV一个背景图片横向平铺,缩小浏览器,拉动横向滚动条,此时触发此BUG:背景图片平铺不完整 解决办法: 1.把背景图片写在BODY上,此办法局限于没有使用iframe的情况下,所以少用 2.设定 ...

  5. effective c++读书笔记(一)

    很早之前就听过这本书,找工作之前读一读.看了几页,个人感觉实在是生涩难懂,非常不符合中国人的思维方式.之前也有博主做过笔记,我来补充一些自己的理解. 我看有人记了笔记,还不错:http://www.3 ...

  6. [MySQL]You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

    执行update语句,出现问题: 问题描述: You are using safe update mode and you tried to update a table without a WHER ...

  7. 远程连接 mysql 数据库连接不上的解决方案

    今天用Navicat访问虚拟机上的mysql,无法访问报cannot connect(10038). 首先看是否可以telnet,本机cmd,telnet 10.10.10.10 3306,结果是连接 ...

  8. python datetime 时区(timezone) dateutil

    记录下python中的时区问题, 代码如下:  包括datetime.datetime对象使用不同的时区,以及在不同时区间转换. from datetime import datetime from ...

  9. 删除WP提示:自动升级WordPress失败

    wordpress后台总有烦人的升级失败的提示,查了半天找不到怎么去掉:“自动升级WordPress失败--请再试一次”这个提示的方法,特意分享出来 方法/步骤   1 打开wordpress根目录找 ...

  10. 2019.2.25 模拟赛T1【集训队作业2018】小Z的礼物

    T1: [集训队作业2018]小Z的礼物 我们发现我们要求的是覆盖所有集合里的元素的期望时间. 设\(t_{i,j}\)表示第一次覆盖第i行第j列的格子的时间,我们要求的是\(max\{ALL\}\) ...