1103 Integer Factorization (30)

1103 Integer Factorization (30 分)

The KP 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 KP 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≤LK 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

#include<bits/stdc++.h>
using namespace std;

const int maxn=410;

int factor[maxn];

int n,k,p;

vector<int> ans,temp;

int cnt=0;

bool flag=false;

int maxSum = -1;

void init(){
    int i=0,temp=0;
    while(temp<=n){
        factor[i]=(int)pow(i*1.0,p);
        temp=factor[i];
        cnt=i;
        i++;
    }
}

void DFS(int index,int nowk,int sumW,int sumC){
    if(nowk>k||sumC>n)
            return;

    if(nowk==k&&sumC==n){

        flag=true;

        if(sumW>maxSum){
            maxSum=sumW;
            ans=temp;
        }
    }

    if(index-1>=0){
        temp.push_back(index);
        DFS(index,nowk+1,sumW+index,sumC+factor[index]);
        temp.pop_back();
        DFS(index-1,nowk,sumW,sumC);

    }

}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);

    cin>>n>>k>>p;

    init();

    DFS(cnt,0,0,0);

    if(!flag){
        cout<<"Impossible\n";
        return 0;
    }

    cout<<n<<" =";

    for(int i=0;i<ans.size();i++){
        if(i>0)
            cout<<" +";

        cout<<" "<<ans[i]<<"^"<<p;
    }

    cout<<endl;

    return 0;

}

原文地址:https://www.cnblogs.com/moranzju/p/11332157.html

时间: 2024-08-29 14:20:35

1103 Integer Factorization (30)的相关文章

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

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

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

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 in

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

搜索专题——DFS A1103 Integer Factorization(30)

#include <bits/stdc++.h> #include<math.h> #include <string> using namespace std; //maxFacSum 记录最大底数之和 int n,k,p,maxFaceSum = -1; vector<int> fac,ans,temp; int power(int x){ int ans = 1; for(int i = 0;i<p;++i){ ans *= x; } return

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10