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. Arm-kernel 内存收集【转】

    转自:http://blog.csdn.net/linyt/article/details/6627664 Linux kernel的内存管理子系统非常复杂,为了深入了解内存管理系统,我打算分多篇文章 ...

  2. Mysql 中 char 、varchar 、text的区别

    首先它们的存储方式和数据的检索方式都不一样.数据的检索效率是:char > varchar > text 空间占用方面,就要具体情况具体分析了. char:存储定长数据很方便,CHAR字段 ...

  3. angular项目中使用ngSemantic

    npm install ng-semantic --save npm install jquery --save 下载 Official Semantic UI bundle ( .zip ) fro ...

  4. C语言花括号

    由于C语言本身就是函数式语言,说白了,C程序就是由函数构成的! 所以花括号肯定是用在函数之中,包括函数中的各种流程控制语句中. 实际上,C程序中花括号{}的作用:就是把多个单条语句用花括号{}括起来组 ...

  5. PC端网站跳转手机端网站

    <SCRIPT LANGUAGE="JavaScript"> function mobile_device_detect(url) { var thisOS=navig ...

  6. [ JS 进阶 ] Repaint 、Reflow 的基本认识和优化

    你是不是经常听师兄或一些前端前辈说不能用CSS通配符 *,CSS选择器层叠不能超过三层,CSS尽量使用类选择器,书写HTML少使用table,结构要尽量简单-DOM树要小....等这些忠告,以前我就大 ...

  7. 【hdoj_1715】大菲波数(大数+100000000进制)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1715 本题采用大数加法即可解决.采用100000000进制速度更快. C++代码如下: #include& ...

  8. c++ primer 10 关联容器

    关联容器和顺序容器的本质差别在于:关联容器通过键(key)存储和读取元素,顺序容器则通过元素在容器中的位置顺序存储和访问元素 关联容器类型 map 关联数组:元素通过键来存储和读取 set 大小可变的 ...

  9. iis应用池内存溢出卡死优化

    1.修改回收阀值memoryLimit 在ASP.NET Web服务器上,ASP.NET所能够用到的内存,通常不会等同于所有的内存数量.在machine.config(C:/WINDOWS/Micro ...

  10. 使用STL sort对字符串按字典序排序

    使用string数组 #include<iostream> #include<string> #include<algorithm> using namespace ...