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. python算法之近似熵、互近似熵算法

    理论基础 近似熵? 定义:近似熵是一个随机复杂度,反应序列相邻的m个点所连成折线段的模式的互相近似的概率与由m+1个点所连成的折线段的模式相互近似的概率之差. 作用:用来描述复杂系统的不规则性,越是不 ...

  2. juery获取元素的方法

    1 从集合中通过指定的序号获取元素 html: 复制代码 代码如下: <div> <p>0</p> <p>1</p> <p>2& ...

  3. CPU运行时间——time

    用途说明time命令常用于测量一个命令的运行时间,注意不是用来显示和修改系统时间的(这是date命令干的事情).但是今天我通过查看time命令的手册页,发现它能做的不仅仅是测量运行时间,还可以测量内存 ...

  4. js获取系统时间

    //------------------------------------获取系统日期时间 var oDate=new Date(); //alert(oDate.getFullYear());// ...

  5. 中文chrome font-size 10px,11px,12px,rem只为12px解决办法

    问题来源: html { font-size: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); } .form-signin { max-wi ...

  6. Java显式锁学习总结之三:AbstractQueuedSynchronizer的实现原理

    概述 上一篇我们讲了AQS的使用,这一篇讲AQS的内部实现原理. 我们前面介绍了,AQS使用一个int变量state表示同步状态,使用一个隐式的FIFO同步队列(隐式队列就是并没有声明这样一个队列,只 ...

  7. STM32 磁场传感器HMC5883

    一.IIC协议 默认(出厂) HMC5883LL 7 位从机地址为0x3C 的写入操作,或0x3D 的读出操作. 要改变测量模式到连续测量模式,在通电时间后传送三个字节:0x3C 0x02 0x00 ...

  8. Python 读写xlsx

    # pip install openpyxl # openpyxl只能用于处理xlsx,不能用于处理xlsfrom openpyxl import load_workbook # 打开文件ExcelF ...

  9. 纯js的N级联动列表框 —— 基于jQuery

    多个列表框联动,不算是啥大问题,但是却挺麻烦,那么怎么才能够尽量方便一点呢?网上搜了一下,没发现太好用的,于是就自己写了一个.基于jQuery,无限级联动,支持下拉列表框和列表框. 先说一下步骤和使用 ...

  10. 某PCBA企业应用易普优APS实现高级计划排程案例

    一.项目介绍 1.生产计划现状 某PCBA企业(以下简称A企业)的产品生产是典型的多品种.小批量.多变化的生产模式.其中产品种类有1000多种,主流的200多种,每个月数百个生产订单,分解到工序以后的 ...