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 sol& b){
    if(a.sum != b.sum)
        return a.sum > b.sum;
    else{
        for(int i = 0;i < a.seq.size();i++){
            if(a.seq[i] < b.seq[i])
                return false;
            else if(a.seq[i] > b.seq[i])
                return true;
        }
    }
}
void rec(vector<int>& pvec,vector<sol>& ret,sol seq,int n,int k,int p,int id){
    if(n < k || k == 0 && n != 0 || n > k * pvec[id])
        return;
    else if(n == 0 && k == 0){
        seq.sum = accumulate(seq.seq.begin(),seq.seq.end(),0);
        ret.push_back(seq);
    }
    else{
        int root(pvec.size() - 1);
        for(int i = id;i >= 1;i--){
            int tmp(pvec[i]);
            if(n >= tmp + k - 1){
                seq.seq.push_back(i);
                rec(pvec,ret,seq,n - tmp,k-1,p,i);
                seq.seq.pop_back();
            }
        }
    }
}
int main(){
    int n,k,p;
    scanf("%d %d %d",&n,&k,&p);
    vector<sol> ret;
    sol seq;
    vector<int> pvec;
    int num(0);
    while(true){
        int tmp(pow(float(num),p));
        if(tmp <= n){
            pvec.push_back(tmp);
        }
        else{
            break;
        }
        num++;
    }
    int id(pvec.size() - 1);
    rec(pvec,ret,seq,n,k,p,id);
    if(ret.size() == 0)
        printf("Impossible\n");
    else{
        sort(ret.begin(),ret.end());
        printf("%d = ",n);
        for(int i = 0;i < k;i++){
            if(i == 0){
                printf("%d^%d",ret[0].seq[0],p);
            }
            else{
                printf(" + %d^%d",ret[0].seq[i],p);
            }
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-10 07:52:08

1103 integer factorization的相关文章

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[难]

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分)

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

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

搜索专题——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