【PAT甲级】1103 Integer Factorization (30 分)
题意:
输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible"。
//找到最大可能的整数pos后从大到小爆搜,sample 1给的输出好像不是最大的序列。。。。。
AAAAAccepted code:
1 #define HAVE_STRUCT_TIMESPEC
2 #include<bits/stdc++.h>
3 using namespace std;
4 int a[407];
5 int n,k,p;
6 int mxsum;
7 vector<int>ans,anss;
8 void solve(int now,int temp,int sum,int num){
9 if(num==k&&temp==n&&sum>mxsum){
10 anss=ans;
11 mxsum=sum;
12 }
13 if(num==k)
14 return ;
15 while(now>=1){
16 if(temp+a[now]<=n){
17 ans[num]=now;
18 solve(now,temp+a[now],sum+now,1+num);
19 }
20 if(now==1)
21 return ;
22 --now;
23 }
24 }
25 int main(){
26 ios::sync_with_stdio(false);
27 cin.tie(NULL);
28 cout.tie(NULL);
29 cin>>n>>k>>p;
30 ans.resize(k);
31 int pos=0;
32 for(int i=1;i<=n+1;++i){
33 a[i]=pow(i,p);
34 if(a[i]+k-1>n){
35 pos=i;
36 break;
37 }
38 }
39 solve(pos-1,0,0,0);
40 if(!mxsum){
41 cout<<"Impossible";
42 return 0;
43 }
44 cout<<n<<" =";
45 for(int i=0;i<anss.size();++i){
46 cout<<" "<<anss[i]<<"^"<<p;
47 if(i<anss.size()-1)
48 cout<<" +";
49 }
50 return 0;
51 }
【PAT甲级】1103 Integer Factorization (30 分)的更多相关文章
- PAT甲级1103. Integer Factorization
PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...
- 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中找 ...
- 1103 Integer Factorization (30)
1103 Integer Factorization (30 分) The K−P factorization of a positive integer N is to write N as t ...
- PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)
1080 Graduate Admission (30 分) It is said that in 2011, there are about 100 graduate schools ready ...
- PAT 甲级 1072 Gas Station (30 分)(dijstra)
1072 Gas Station (30 分) A gas station has to be built at such a location that the minimum distance ...
- PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ...
- PAT 甲级 1026 Table Tennis (30 分)(坑点很多,逻辑较复杂,做了1天)
1026 Table Tennis (30 分) A table tennis club has N tables available to the public. The tables are ...
- PAT 甲级 1022 Digital Library (30 分)(字符串读入getline,istringstream,测试点2时间坑点)
1022 Digital Library (30 分) A Digital Library contains millions of books, stored according to thei ...
随机推荐
- 记录 Docker 的学习过程 (dockerfile自动制作镜像)
自动制作镜像 通过编写dockerfile来自动创建镜像 #vi Dockerfile #编辑dockerfile文件,一定要以这个名字命名 #cat Dockerfile #导入哪个基础镜像FROM ...
- AtCoder Beginner Contest 154 题解
人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...
- PP: Think globally, act locally: A deep neural network approach to high-dimensional time series forecasting
Problem: high-dimensional time series forecasting ?? what is "high-dimensional" time serie ...
- WordPress 背后的故事竟然是这样
原文链接:The Story Behind the New WordPress.com 译者:杰微刊-Leo Xu 一年半以前,我们对使用 WordPress 构建网站所需要采用的技术和开发流程进行大 ...
- RN开发-Linux开发环境搭建(Ubuntu 12.04)
1.首先安装JDK 2.安装Android开发环境 3.安装node.js 3.1 官网下载 : node-v6.9.1-linux-x64 3.2 添加环境变量 sudo vi /etc/profi ...
- svg 放大缩小后的比例
function getCoords(evt) { // 计算出当前屏幕与svg的比例 var canvas = document.getElementById("canvase&qu ...
- 面试官所问的--Token认证
写这一篇文章的来源是因为某一天的我被面试官提问:让你设计一个登录页面,你会如何设计? 我当时的脑子只有??? 不就是提交账号.密码给后台就搞定了呢? 不可能那么简单,我弱弱的想,难道要对密码加密?? ...
- Django_发邮件
1. 设置项
- path is not a working copy directory
svn: 'G:\chengXu\2017_Year\JFW\Easy7\WebRoot' is not a working copy directory 解决方法: (1)原因:eclipse把sr ...
- 显示目录文件命令 - ls
1) 命令名称:ls 2) 英文原意:list 3) 命令所在路径:/bin/ls 4) 执行权限:所有用户 5) 功能描述:显示目录文件 6) 语法: ls 选项[-ald][文件或目录] -a 显 ...