PAT甲级1103. Integer Factorization
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的更多相关文章
- 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】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甲级——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 ...
- PAT 1103 Integer Factorization[难]
1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...
- 1103 Integer Factorization (30)
1103 Integer Factorization (30 分) The K−P factorization of a positive integer N is to write N as t ...
- 【PAT甲级】1103 Integer Factorization (30 分)
题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...
- 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 ...
- PAT (Advanced Level) 1103. Integer Factorization (30)
暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
随机推荐
- Android 开发笔记(二)菜单设计
菜单设计一 // 创建菜单 public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 0, 0, "关于"); men ...
- css的背景图片background
1.使用背景图片的标签定设置宽高,没有设置的话,也需要用内容来撑开标签. 2.如果对同一个标签分开设置背景图片和颜色,背景颜色一定要写在背景图片后面,不然会被覆盖 <!DOCTYPE html& ...
- python RSA加密解密及模拟登录cnblog
1.公开密钥加密 又称非对称加密,需要一对密钥,一个是私人密钥,另一个则是公开密钥.公钥加密的只能私钥解密,用于加密客户上传数据.私钥加密的数据,公钥可以解密,主要用于数字签名.详细介绍可参见维基百科 ...
- tcgetattr函数与tcsetattr函数控制终端
6.4.4 使用tcgetattr函数与tcsetattr函数控制终端 为了便于通过程序来获得和修改终端参数,Linux还提供了tcgetattr函数和tcsetattr函数.tcgetattr用于 ...
- Linux时间结构体和获得时间函数
关于Linux下时间编程的问题: 1. Linux下与时间有关的结构体 struct timeval { int tv_sec; int tv_usec; }; 其中tv_sec是由凌晨开始算起的秒数 ...
- CentOS下NTP安装配置
安装yum install ntp 配置文件 /etc/ntp.confrestrict default kod nomodifynotrap nopeer noqueryrestrict -6 ...
- free之后将指针置为NULL
free一个指针,只是将指针指向的内存空间释放掉了,并没有将指针置为NULL,指针仍指向被释放掉的内存的地址,在判断指针是否为NULL的时候,通常是通过if(pt == NULL) ,这时,导致指针成 ...
- STL中stack/queue/map以及Boost unordered_map 的使用方法
一.stackstack 模板类的定义在<stack>头文件中.stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要的,在不指定容器类型时,默认的容器类型 ...
- 摄像机distortion vector、project matrix、camera matrix
关于标定后图像如何校正:http://wiki.ros.org/image_pipeline/CameraInfo ros distortion vector 参数顺序:http://docs.ros ...
- Java学习(匿名对象、内部类、包、import、代码块)
一.匿名对象 概念:匿名对象是指在创建对象时,只有创建的语句,却没有把对象地址值赋给某个变量. 特点: (1)匿名对象直接使用,没有变量名.当做形参使用. new Person().name=&quo ...