题意:

输入三个正整数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. Python之路Day11

    函数名的第一类对象及使用 当作值,赋值给变量 def func(): print(1) print(func) #查看函数的内存地址 a=func print(a) a() 可以当作容器中的元素 de ...

  2. [国家集训队] Crash的数字表格 - 莫比乌斯反演,整除分块

    考虑到\(lcm(i,j)=\frac{ij}{gcd(i,j)}\) \(\sum_{i=1}^n\sum_{j=1}^m\frac{ij}{gcd(i,j)}\) \(\sum_{d=1}^{n} ...

  3. js及jsp.java查错的几种方式

    一.js 1.console.log("你想输出的内容"); 2.alert("你想输出的内容"); 3.debugger;(记得打开F12) 4.快速找到js ...

  4. 数字输出精度 - C语言

    1. 浮点型/双精度浮点型 double a=1234.5678; printf("%8.2lf\n",a);// 小数点前的数表示总位数,小数点也算一位 printf(" ...

  5. View -->Controller传值的几种方法

    1.参数名称必须和表单的name是一致的. //参数的名称需要和表单的字段名称一致,这样系统便会直接赋值. public ActionResult GetUserInfo(string usernam ...

  6. 关于强类型、ViewData

    对于ASP.NET MVC中页面强类型的个人理解   进入ASP.NET MVC学习 发现很多和winfrom不同的东西,但是利用的C#语言还是没有变化,更多的是利用了新的语言,html jquery ...

  7. Radar Installation(利用数据有序化进行贪心选择)

    English appre: an infinite straight line:一条无限长的直线 on the coasting:在海岸线上 Cartesian coordinate system, ...

  8. el-popover 点击input框出现table表,可点击选中,可拼音检索完回车选中

    <template> <card> <el-popover placement="right" width="400" trigg ...

  9. SuperSocket与SuperSocket.ClientEngine实现Protobuf协议

    参考资料说明 SuperSocket文档 http://docs.supersocket.net/ Protobuf语言参考 https://developers.google.com/protoco ...

  10. [C语言学习笔记四]变量与系统的交互

    使用 const 创建常量和使用 volatie 优化变量 C语言中使用 const 定义常量. 例如: const INT a = 10; 此处如果添加a = 20;,编辑器则会报错,因为此处 a ...