The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K−P factorization of N for any positive integers N, K and P.
Input Specification:
Each input file contains one test case which gives in a line the three positive integers N (≤400), K (≤N) and P (1
Output Specification:
For each case, if the solution exists, output in the format:
N = n[1]^P + … n[K]^P
where n[i] (i = 1, …, K) is the i-th factor. All the factors must be printed in non-increasing order.
Note: the solution may not be unique. For example, the 5-2 factorization of 169 has 9 solutions, such as 12
2
+4
2
+2
2
+2
2
+1
2
, or 11
2
+6
2
+2
2
+2
2
+2
2
, or more. You must output the one with the maximum sum of the factors. If there is a tie, the largest factor sequence must be chosen – sequence { a
1
,a
2
,⋯,a
K
} is said to be larger than { b
1
,b
2
,⋯,b
K
} if there exists 1≤L≤K such that a
i
=b
i
for i
>b
L
.
If there is no solution, simple output Impossible.
Sample Input 1:
169 5 2
Sample Output 1:
169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2
Sample Input 2:
169 167 3
Sample Output 2:
Impossible
网上清一色 dfs,可能这就是大佬吧,也不是说dfs多难,而是自己不会往那方面想,当然想到,和想到能做出来,和想到并且熟练的做出来是不一样的。
我用的是模拟,22分,有2个地方错误,但是我找不到,22分如果考试中就可以这个分数,但是不知道自己哪里错了就很难受,然后上网找测试点2,5,也没找到什么实质性的错误,然后就学习了一下算法笔记上面的做法,dfs
第一次 22分 模拟
我看p > 2,所以我认为最大幂为sqrt(n),然后一点点模拟,减法,判断,我觉得合情合理,唯一不足就是可能时间比较长,而且也没什么算法,也没什么剪枝,其实大体上我感觉和dfs差不多,
#include
#include
#include
#include
#include
#include
using namespace std;
vector<int> ans;
int power(int a,int b){
int t = 1;
for(int i=1;i<=b;i++){
t*=a;
}
return t;
}
int main(){
int n,k,p,maxn = 0;
cin>>n>>k>>p;
int sqt = (int)sqrt(n);
for(int i=sqt;i>=1;i--){
vector<int> temp;
int tn = n,tmaxn=0;
for(int t= i;t>=1;t--){
while(power(t,p) <= tn){
tmaxn+=t;
tn -= power(t,p);
temp.push_back(t);
}
}
if(tn == 0 && tmaxn > maxn && temp.size() == k ){
maxn = tmaxn;
ans = temp;
}
}
if(ans.size() == 0) cout<<"Impossible"<<endl;
else{
printf("%d = ",n);
for(int i=0;i<ans.size();i++){
if(i > 0) printf(" + ");
printf("%d^%d",ans[i],p);
}
}
return 0;
}
第二种 算法笔记 dfs
(我自己的感悟,可能比较trash)
先离线记录power(i,p),最大的不超过n,
最好自己写一个power函数,
dfs学会剪枝,
//要这么写
if(nowk == k && sum == n ){
if(facsum > maxn){
maxn = facsum;
ans = temp;
}
return ;
}
而不是
if(nowk == k && sum == n && facsum > maxn){
maxn = facsum;
ans = temp;
return ;
}
小细节,上面的做法剪掉更多的枝,想想你就明白了
这题我觉的很好,即考到了对题目的挖掘,理解,也考到了dfs的写法,
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int n,k,p;
vector<int> fac,ans,temp;
int power(int b){
int t = 1;
for(int i=1;i<=p;i++) t*=b;
return t;
}
void init(){
int i = 0,temp = 0;
while(temp <= n){
fac.push_back(temp);
temp = power(++i);
}
}
int maxn = -1;
void dfs(int index,int nowk,int sum,int facsum){
if(nowk == k && sum == n){
if(facsum > maxn){
maxn = facsum;
ans = temp;
}
return ;
}
if(nowk > k || sum > n) return;
if(index >= 1){
temp.push_back(index);
dfs(index,nowk+1,sum+fac[index],facsum+index);
temp.pop_back();
dfs(index-1,nowk,sum,facsum);
}
}
int main(){
cin>>n>>k>>p;
init();
dfs(fac.size()-1,0,0,0);
if(maxn == -1) cout<<"Impossible"<<endl;
else{
printf("%d = ",n);
for(int i=0;i<ans.size();i++){
if(i > 0) printf(" + ");
printf("%d^%d",ans[i],p);
}
}
return 0;
}
dfs也是以后需要加强的地方
其实做了那么多题目,已经没有想刚开始那样恐惧甲级的题目了,摸清套路,不会的东西,先想,再参考别人优秀的思路和算法,然后自己记住,理解是最好的,然后就成为自己的了,不要着急,重要的是效率和坚持,嗯嗯,继续加油吧