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<P<=7). The numbers in a line are separated by a space.
Output Specification:
For each case, if the solution exists, output in the format:
N = n1^P + ... nK^P
where ni (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 122 + 42 + 22 + 22 + 12, or 112 + 62 + 22 + 22 + 22, 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 { a1, a2, ... aK } is said to be larger than { b1, b2, ... bK } if there exists 1<=L<=K such that ai=bi for i<L and aL>bL
If there is no solution, simple output "Impossible".
Sample Input 1:169 5 2Sample Output 1:
169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2Sample Input 2:
169 167 3Sample Output 2:
Impossible
#include <iostream> #include<stdio.h> #include<algorithm> #include<string> #include<vector> #include<queue> #include<numeric>//include accumulate using namespace std; int aceil[8] = { 0, 0, 20, 7, 4, 3, 2, 2 };//表示上限,比如当p=4时,ceil[4]=4,表示4^4<400而5^4>400 //这个4就是ans中每个数字的上限,ans[i]必须都<=aceil[p],因为ceil已经是关键字了,所以我这里用了aceil int n, k, p;//放在全局变量中,这样DFS中就可以少一些参数传递 vector<int>v_tmp;//存放可能的答案的序列 vector<vector<int>>ans;//存放所有的答案 int kpow(int x, int y){ int ret = x; while (--y){ ret *= x; } return ret; } void DFS(int start, int curSum, int count){//分别表示开始的数字,现在的sum和计数 for (int i = start; i <= aceil[p]; ++i){ int sum_tmp = curSum + kpow(i, p);//*point,这里用pow测试点5会超时,需要用自己写的kpow if (sum_tmp > n)return;//剪枝,和大了要剪掉 if (count == k - 1 && sum_tmp < n)continue;//剪枝,到达个数了和小了也剪掉 if(count == k - 1 && sum_tmp == n){ v_tmp.push_back(i); ans.push_back(v_tmp); v_tmp.pop_back(); return; } v_tmp.push_back(i);//典型的DFS模式:挖坑,跳入,再填坑(自创) DFS(i, sum_tmp, count + 1); v_tmp.pop_back(); } return; } /*如果不用accumulate或者不熟悉,就可以自己写一个,效果一样的 int vSum(vector<int>Vx){ int ret = 0; for (int i = 0; i < Vx.size(); ++i){ ret += Vx[i]; } return ret; } */ bool cmp(vector<int>V1, vector<int>V2){ if (accumulate(V1.begin(), V1.end(), 0) != accumulate(V2.begin(), V2.end(), 0))//accumulate第3个参数是init,初值设置为0就OK return accumulate(V1.begin(), V1.end(), 0) > accumulate(V2.begin(), V2.end(), 0); else{ return V1 > V2;//vector自带的<运算符,可以按元素依次比较,符合我们的要求 } } void print(vector<int> Vx){//对于vector的格式化输出,再定制更改一下 cout << n << " = "; for (int i = 0; i < Vx.size(); ++i){ if (i) cout << " + " << Vx[i] << "^" << p; else cout << Vx[i]<< "^" << p; } } int main(){ freopen("F://Temp/input.txt", "r", stdin); cin >> n >> k >> p; /*题目中说p > 1,不过如果p可以等于1的话,应该是如下处理 if (p == 1){//当p为1时,就可以直接做处理 v_tmp.push_back(n - k + 1); for (int i = 0; i < k - 1; i++){ v_tmp.push_back(1); } print(v_tmp); cout << endl; return 0; } */ DFS(1, 0, 0);//初始值 if (ans.size() == 0){//如果没有答案,直接输出返回 cout << "Impossible" << endl; return 0; } for (int i = 0; i < ans.size(); ++i){ reverse(ans[i].begin(), ans[i].end());//因为答案是按递增的,为了比较和输出方便,倒置一下 } sort(ans.begin(), ans.end(), cmp);//对多个答案按照题意进行排序,把最佳的放在最前面 print(ans[0]); cout << endl; return 0; }
#include <iostream> #include <cstdio> #include <vector> using namespace std; int num[21]; int maxSum = 0; vector<int> res; vector<int> vec; void dfs(int start, int k, int n, int sum){ if (k == 0){ if (n == 0){ if (sum >= maxSum){ res = vec;//只用记录最后一组答案就好,因为从数学的角度看,这是最佳的 maxSum = sum; } } } else{ if (n > 0){ for (int i = start; i < 21; ++i){ if(n - num[i] < 0)//剪枝,没有这个判断测试点5过不了 break; vec.push_back(i); dfs(i, k - 1, n - num[i], sum + i); vec.pop_back(); } } } } int main(void){ int n, k, p; scanf("%d%d%d", &n, &k, &p); for (int i = 1; i < 21; ++i){//这样就直接不用pow了 num[i] = 1; for (int j = 0; j < p; ++j){ num[i] *= i; } } dfs(1, k, n, 0); if (res.empty()) printf("Impossible\n"); else{ printf("%d = ", n); printf("%d^%d", res[k - 1], p); for (int i = k - 2; i >= 0; --i){ printf(" + "); printf("%d^%d", res[i], p); } printf("\n"); } return 0; }代码更简洁,也更好了。并且: