PAT甲级 A1103
题目详情
1103 Integer Factorization (30分)
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 = 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<L and a
L
>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,直至其所以的子因式耗尽且相加恰好等于。采用pow(i,P)的方法进行剪枝以及判断边界条件,每次从上一次搜索点开始继续向大搜索,此时仍然有一个测试点无法通过
代码如下:
#include <cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<cmath>
#include<iomanip>
#include<map>
#include<queue>
#include<string>
using namespace std;
int N, K, P;
vector<int> answer;
vector<int> temps;
int sum=0;
bool isbigger() {
for (int i = K-1; i >=0; i--) {
if (temps[i] < answer[i]) {
return false;
}
else if (temps[i] > answer[i]) {
return true;
}
}
return false;
}
void DFS(int va,int sums,int left,int last) {
if (va < 0) {
return;
}
if (left == 0 && va != 0) {
return;
}
else if (left != 0 && va == 0) {
return;
}
else if (va == 0 && left == 0) {
if (sums > sum) {
answer = temps;
sum = sums;
}
else if (sums == sum) {
if (isbigger()) {
sum = sums;
answer = temps;
}
}
}
for (int i = last; pow(i,P)<=N; i++) {
int newsum = sums + i;
temps.push_back(i);
DFS(va - pow(i,P), newsum, left - 1,i);
temps.pop_back();
}
}
int main() {
cin >> N >> K >> P;
DFS(N, 0, K,1);
if (answer.size() == 0) {
cout << "Impossible" << '\n';
return 0;
}
cout <<N<< " = ";
for (int i = answer.size() - 1; i >= 0; i--) {
if (i == answer.size() - 1) {
cout << answer[i] << "^" << P;
}
else {
cout <<" + "<<answer[i] << "^" << P;
}
}
}
此时进行优化,优化方法即为在中间增加先计算所有可能用到的pow的值为一个数组,在调用时访问取代计算,能够节省时间,此时能通过所有的目标点
以下为AC代码
#include <cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<cmath>
#include<iomanip>
#include<map>
#include<queue>
#include<string>
using namespace std;
int N, K, P;
vector<int> answer;
vector<int> temps;
int sum = 0;
long long int powsave[500];
bool isbigger() {
for (int i = K - 1; i >= 0; i--) {
if (temps[i] < answer[i]) {
return false;
}
else if (temps[i] > answer[i]) {
return true;
}
}
return false;
}
void DFS(int va, int sums, int left, int last) {
if (left == 0 && va != 0) {
return;
}
else if (left != 0 && va == 0) {
return;
}
else if (va == 0 && left == 0) {
if (sums > sum) {
answer = temps;
sum = sums;
return;
}
else if (sums == sum) {
if (isbigger()) {
answer = temps;
sum = sums;
return;
}
}
}
if (va < 0) {
return;
}
for (int i =last; i >= 1; i--) {
int newsum = sums + i;
temps.push_back(i);
DFS(va - powsave[i], newsum, left - 1, i);
temps.pop_back();
}
}
int main() {
cin >> N >> K >> P;
for (int i = 1; pow(i,P)<=N; i++) {
powsave[i] = pow(i, P);
}
int temps = pow(N, (1 / (P * 1.0)));
DFS(N, 0, K, temps);
if (answer.size() == 0) {
cout << "Impossible" << '\n';
return 0;
}
cout << N << " = ";
for (int i = 0; i <answer.size(); i++) {
if (i == 0) {
cout << answer[i] << "^" << P;
}
else {
cout << " + " << answer[i] << "^" << P;
}
}
}