【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 ...
随机推荐
- 2019牛客多校第八场A All-one Matrices 悬线法,单调栈待补
All-one Matrices 题意 对于一个n,m的01矩阵,问有多少个极大矩阵. 分析 对于悬线法来说,其过程就是枚举极大矩阵的过程,那如何计数呢?对于一个点来说,若其左右边界包含了上一个点的悬 ...
- [SHOI2001] 小狗散步 - 二分图匹配
考虑到每次与主人相遇之前最多只去一个景点,很容易转化为匹配问题 由于数据很小,我们不妨枚举每个相遇点间隙和每个景点,判断是否来得及,如果来得及就连边 沙雕题搞了二十来分钟,我是憨憨 #include ...
- loj6277 数列分块入门题1
裸题分块. #include <bits/stdc++.h> using namespace std; ],b[],n,m,t1,t2,t3,t4,sq; int main(){ ios: ...
- How to write a paper in a weekend - by Prof. Pete Carr
Key points: don't procrastinate; review the notes and renew the literature search; determine who you ...
- MySQL主键设计盘点
目录 主键定义 主键设计和应用原则 主键生成策略 自增ID UUID 自建的id生成器 Twitter的snowflake算法 @ 最近在项目中用了UUID的方式生成主键,一开始只是想把这种UUID的 ...
- idea 配置 tomcat 教程
最近在搞一个项目需要用到idea 配置tomcat,翻了翻网上的帖子发现稂莠不齐,最后决定还是自己写个吧!(其实我挺蠢的走了好多的弯路,哎~) 1.首先准备一个需要大家tomcat的工程,然后使用id ...
- 测试准确率计算方法说明 pre.eq(target).float().sum().item()
测试准确率计算方法说明 pre.eq(target).float().sum().item() 待办 pred = logits.argmax(dim=1) correct += pred.eq(ta ...
- Win系统如何修改远程桌面端口3389
Win系统如何修改远程桌面端口3389: https://jingyan.baidu.com/article/ce43664928d5293773afd39b.html window10远程桌面连接: ...
- TD - 单选框 - RadioButton
基本方法 Html - 默认选中 //checked="true" - 默认选中 <input dojoType="bootstrap.form.RadioButt ...
- protobuf-net简单使用
第一个测试的proto文件 syntax = "proto3"; package ProtoMsg; message Foo { ; int32 id = ; repeated b ...