搜索专题——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 ans;
}
void init(){
    int i = 0,temp = 0;
    while(temp <= n){
        fac.push_back(temp);
        temp = power(++i);
    }
}
void DFS(int index,int nowK,int sum,int facSum){
    if(sum == n && nowK == k){
        if(facSum > maxFaceSum){
            ans = temp;
            maxFaceSum = facSum;
        }
        return;
    }
    if(sum > n || nowK > k){//这种情况下不会产生答案,直接返回
        return;
    }
    if(index - 1 >= 0){
        temp.push_back(index);//把底数index加入临时序列temp
        DFS(index,nowK+1,sum+fac[index],facSum+index);//选
        temp.pop_back();
        DFS(index - 1,nowK,sum,facSum);//不选
    }
}
int main(){
    scanf("%d%d%d",&n,&k,&p);
    init();
    DFS(fac.size()-1,0,0,0);
    if(maxFaceSum == -1){
        printf("Impossible\n");
    }else{
        printf("%d = %d^%d",n,ans[0],p);//输出ans的结果
        for(int i=1;i<ans.size();++i){
            printf(" + %d^%d",ans[i],p);
        }
    }
    system("pause");
    return 0;
} 

原文地址:https://www.cnblogs.com/JasonPeng1/p/12215122.html

时间: 2024-07-30 15:31:23

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

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 (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

搜索专题 BFS A1091.Acute Stroke(30)

#pragma warning(disable:4996) #include <stdio.h> #include <stdlib.h> #include <queue> using namespace std; struct node{ int x, y, z; }p; int m, n, l, t, volume = 0; int brain[1300][130][61], bbrain[1300][130][61]; //右,左,上,下,前,后 int X[] =

Leetcode之深度优先搜索(DFS)专题-DFS+记忆化 329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: nums = [ [9,9,4], [6,6,8], [2,1,1] ] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9].

Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles)

Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles) 深度优先搜索的解题详细介绍,点击 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合. 给定一个 N * N 的矩阵 M,表示班级中学生之间的朋友关系.如果M[i][j] = 1,表示已知第 i 个和 j 个学生互为朋友关系,否则为不知道.你必须输出所有学生

搜索专题小结及例题:POJ2251&amp;POJ1426&amp;POJ3087&amp;POJ2488

图的遍历也称为搜索,就是从图中某个顶点出发,沿着一些边遍历图中所有的顶点,且每个顶点仅被访问一次,遍历可采取两种不同的方式:深度优先搜索(DFS)和广度优先搜索(BFS). 1.DFS算法思想` 从顶点v出发深度遍历图G的算法 ① 访问v0顶点,置vis[v0]=1,搜索v0未被访问的邻接点w,若存在邻接点w,则dfs(w),直到到达所有邻接点都被访问过的顶点u为止,接着退回一步,看是否还有其他没有被访问的邻接点.如果有,则访问此顶点,进行前述类似的访问,如果没有,就在退回一步进行搜索,重复上述