【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 ...
随机推荐
- ubantu安装apache
1.命令安装: sudo apt install apache2 2.检查是否启动了Apache服务 systemctl status apache2 3.开启.关闭和重启服务器 /etc/init. ...
- arm汇编笔记
ARM汇编(非虫笔记) 1.ARM汇编的目的: 分析elf文件的需要. 2.原生程序生成过程. (1)预处理,编译器处理c代码中的预处理指令. gcc -E hello.c -o hello.i (2 ...
- (转)linux 之 grep命令
转自:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html 简介 grep (global search regular e ...
- SimpleDateFormat中YYYYmmDDhhMMss大小写问题-获取不到正确时间以及常见的格式串
1.问题解决: SimpleDateFormat sf = new SimpleDateFormat("YYYYmmDDhhMMss");String transTime = &q ...
- C# Timer 控件的用法
一.主要的属性 在 Windows 窗体应用程序中,定时器控件(Timer)与其他的控件略有不同,它并不直接显示在窗体上,而是与其他控件连用. Enabled 属性: 用于设置该Timer控件是否可用 ...
- js中的window.location.search的用法与作用。
用该属性获取页面 URL 地址: window.location 对象所包含的属性 属性 描述 hash 从井号 (#) 开始的 URL(锚) host 主机名和当前 URL 的端口号 hostnam ...
- day30 NFS服务器概述
02. NFS存储服务概念介绍 NFS是Network File System的缩写,中文意思是网络文件共享系统, 它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录 存储服 ...
- python接口自动化之fiddler使用(二)
1.快捷设置,自定义会话框,查看get和post请求 (1)鼠标放在#后面,右键 (2)选择Customize columns (3)选择Miscellaneous (4)选择RequestMetho ...
- go语言 RSA数字签名和验证签名
package main import ( "crypto" "crypto/rand" "crypto/rsa" "crypto ...
- Jquery 如何设置多个attr()属性
Jquery 如何设置多个attr()属性? 文章来源:刘俊涛的博客 欢迎关注公众号.留言.评论,一起学习. _________________________________________ ...