题意:

输入三个正整数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 分)的更多相关文章

  1. PAT甲级1103. Integer Factorization

    PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...

  2. PAT甲级——1103 Integer Factorization (DFS)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...

  3. PAT甲级1103 Integer Factorization【dfs】【剪枝】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 题意: 给定一个数n,要求从1~n中找 ...

  4. 1103 Integer Factorization (30)

    1103 Integer Factorization (30 分)   The K−P factorization of a positive integer N is to write N as t ...

  5. PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)

    1080 Graduate Admission (30 分)   It is said that in 2011, there are about 100 graduate schools ready ...

  6. 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 ...

  7. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

    1049 Counting Ones (30 分)   The task is simple: given any positive integer N, you are supposed to co ...

  8. PAT 甲级 1026 Table Tennis (30 分)(坑点很多,逻辑较复杂,做了1天)

    1026 Table Tennis (30 分)   A table tennis club has N tables available to the public. The tables are ...

  9. PAT 甲级 1022 Digital Library (30 分)(字符串读入getline,istringstream,测试点2时间坑点)

    1022 Digital Library (30 分)   A Digital Library contains millions of books, stored according to thei ...

随机推荐

  1. mybatis一级缓存和二级缓存(一)

    一级缓存: 就是Session级别的缓存.一个Session做了一个查询操作,它会把这个操作的结果放在一级缓存中. 如果短时间内这个session(一定要同一个session)又做了同一个操作,那么h ...

  2. 最近公共祖先 Lowest Common Ancestors

    基于深度的LCA算法:  对于两个结点u.v,它们的深度分别为depth(u).depth(v),对于其公共祖先w,深度为depth(w),u需要向上回溯depth(u)-depth(w)步,v需要d ...

  3. Selenium3+python自动化009- 截图

    一.断言 assert cur_title=='百度一下,你就知道1','title断言失败' 'title断言失败':自定义报错 二.截图 # 调用截屏方法(路径+截图名称)driver.get_s ...

  4. (转)预估大数据量下UV的方法

    在实际应用中,我们经常碰到这种情况,即要统计某个对象或者事件独立出现的次数.对于较小的数据量,这很容易解决,我们可以首先在内存中对序列进行排序,然后扫描有序序列统计独立元素数目.其中排序时间复杂度为O ...

  5. promise的连缀写法

    promise的连缀写法 以上写法相当于写了两个实例 promise.all() 1. promise.all() all这个方法是 promise 构造函数的成员不是实例对象成员,这个方法接受一个参 ...

  6. git 报错和解决

    1.报错 fatal: refusing to merge unrelated histories 解决 两个不相干的库进行合并,需要进行强制合并 git pull origin master --a ...

  7. 巨杉内核笔记 | 会话(Session)

    SequoiaDB 巨杉数据库是一款金融级分布式关系型数据库,坚持从零开始打造分布式开源数据库引擎.“内核笔记系列”旨在分享交流 SequoiaDB 巨杉数据库引擎的设计思路和代码解析,帮助社区用户深 ...

  8. selenium 模拟滑动解锁

    来源:Selenium模拟JQuery滑动解锁   (selenium +Python ) 本文:selenium+Java package cn.gloryroad; import org.open ...

  9. SD卡报错“error -110 whilst initialising SD card”

    目前开发遇到了某些SD卡和TI的SOC芯片的驱动不协调的地方,具体表现为: uboot 阶段初始化mmc dev 1 没有任何串口信息输出,无法读写mmc Kernel阶段报错”SD卡初始化失败 er ...

  10. samba搭建共享目录

    centos 中使用docker 运行samba docker pull dperson/samba 运行一下命令 docker run -it -p 139:139 -p 445:445 --nam ...