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 }

原文地址:https://www.cnblogs.com/mr-stn/p/9584952.html

时间: 2024-07-30 16:20:04

PAT 1103 Integer Factorization的相关文章

PAT 1103 Integer Factorization[难]

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 an

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

题目信息 1103. Integer Factorization (30) 时间限制1200 ms 内存限制65536 kB 代码长度限制16000 B 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 factoriz

【PAT甲级】1103 Integer Factorization (30分)

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 an

1103&#160;Integer Factorization (30)

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 a

PAT甲题题解-1103. Integer Factorization (30)-(dfs)

该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + - nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. dfs枚举,为了防止超时,这里要预先将从1开始的i^p的值存储在factor数组中,直到i^p>n.然后dfs深度优先搜索,相当于把问题一步步分解,即若第一个因子是n1,则接下来我们要判断N-n1^p.k-1是否可行.同时存储当前因子的总和sum,要取sum最大的:还有上一次相加的因子的索引las

PAT (Advanced Level) 1103. Integer Factorization (30)

暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n,k,p,top; int path[1000]; int flag=0; int

1103 integer factorization

dfs+prune AC代码: #include <vector> #include <cstdio> #include <algorithm> #include <cmath> #include <numeric> using namespace std; class sol{ public: vector<int> seq; int sum; }; bool operator<(const sol& a,const

PAT 1113 Integer Set Partition

Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint sets A1 and A2 of n1?? and n?2 numbers, respectively. Let S1? and S?2 denote the sums of all the numbers in A?1?? and A?2?? , respectively. You are supposed

PAT A1113 Integer Set Partition (25 分)——排序题

Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint sets A?1?? and A?2?? of n?1?? and n?2?? numbers, respectively. Let S?1?? and S?2?? denote the sums of all the numbers in A?1?? and A?2??, respectively. You