PAT 1103 Integer Factorization

Posted mr-stn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT 1103 Integer Factorization相关的知识,希望对你有一定的参考价值。

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 (≤), K (≤) and P (1). 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 1, or 1, 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 { , } is said to be larger than { , } if there exists 1 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

 1 #include<iostream>
 2 #include<cmath>
 3 #include<vector>
 4 using namespace std;
 5 vector<int> temp, path;
 6 int n, p, k, maxx=-1, t;
 7 bool flag=true;
 8 /*
 9   dfs的回溯中弹出和压栈的位置很重要
10 */
11 void dfs(int a, int b, int c){
12     if(b==0 && c==0){
13         int tempMax=0;
14         for(int i=0; i<temp.size(); i++) tempMax+=temp[i];
15         if(maxx<tempMax){
16             maxx = tempMax;
17             path = temp;
18         }else if(maxx==tempMax && path<temp) path=temp;
19         temp.pop_back();
20         return;
21     }else if(c<=0 || b<=0){
22         temp.pop_back();
23         return;
24     }
25     for(int i=a; i>=1; i--){
26         if(b-c*pow(i, p)>0) break;  //解决超时的关键点
27         if(b-pow(i, p)<0) {
28             while(b-pow(i, p)<0 && i>=1) --i;
29             temp.push_back(i);
30             dfs(i, b-pow(i, p), c-1);
31         }else{
32             temp.push_back(i);
33             dfs(i, b-pow(i, p), c-1);
34         }
35     }
36     temp.pop_back();//弹出的位置在for循环之外,很重要;
37 }
38 int main(){
39   scanf("%d%d%d", &n, &k, &p);
40   t =pow(n, 1.0/p) > n/k+1 ? pow(n, 1.0/p) : (n/k+1);
41   dfs(t, n, k);
42   if(path.size()==0) cout<<"Impossible"<<endl;
43   else {
44     printf("%d = ", n);
45     for(int i=0; i<path.size(); i++) {
46       if(i!=0) printf(" + ");
47       printf("%d^%d", path[i], p);
48     }
49   }
50   return 0;
51 }

 



以上是关于PAT 1103 Integer Factorization的主要内容,如果未能解决你的问题,请参考以下文章

PAT 1103 Integer Factorization

PAT甲级——A1103 Integer Factorization

1103. Integer Factorization (30)搜索+剪枝——PAT (Advanced Level) Practise

1103. Integer Factorization (30)搜索+剪枝——PAT (Advanced Level) Practise

1103. Integer Factorization (30)搜索+剪枝——PAT (Advanced Level) Practise

PAT A1103 Integer Factorization