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 ...
随机推荐
- 【Explain】mysql之explain详解(分析索引的最佳使用)
在日常工作中,我们会有时会开慢查询去记录一些执行时间比较久的SQL语句,找出这些SQL语句并不意味着完事了,些时我们常常用到explain 这个命令来查看一个这些SQL语句的执行计划,查看该SQL语句 ...
- Deep Learning基础--机器翻译BLEU与Perplexity详解
前言 近年来,在自然语言研究领域中,评测问题越来越受到广泛的重视,可以说,评测是整个自然语言领域最核心和关键的部分.而机器翻译评价对于机器翻译的研究和发展具有重要意义:机器翻译系统的开发者可以通过评测 ...
- Vue.js写一个SPA登录页面的过程
技术栈 vue.js 主框架 vuex 状态管理 vue-router 路由管理 一般过程 在一般的登录过程中,一种前端方案是: 检查状态:进入页面时或者路由变化时检查是否有登录状态(保存在cooki ...
- angular项目中使用ngSemantic
npm install ng-semantic --save npm install jquery --save 下载 Official Semantic UI bundle ( .zip ) fro ...
- git版本控制系统常见操作总结
简介 Git是强大的版本控制系统,主要功能是针对代码.配置文件等文本进行版本控制.备份等,同时个人认为还是分发代码的一个不错的方式. 常见用法 #创建远程git仓库 [root@test88 ~]# ...
- SuSE Linux Supervisor的安装与使用案例
建议使用 root 管理员账户操作 1.安装工具 1.apache 2..Net Core(dotnet-sdk-2.0) 3.Supervisor(进程管理工具,目的是服务器一开机就启动服务器 ...
- LeetCode301. Remove Invalid Parentheses
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- Python--re模块的findall等用法
1)正则表达式含义 . # 点可代表一切字符 \ # 起转义作用 [...] # 指代方括号中的任意字符 \d # 指代数字0-9 \D # 指代非数字 \s # 指代一切空格,包括tab制表符.空格 ...
- js 利用事件委托解决mousedown中的click
有一个需求是这样的: 父元素div绑定一个mousedown事件,子元素a绑定一个click事件. 看解构: <div id="nav"> <a href=&qu ...
- 报错org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet"
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n ...