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

解题思路

搜索加剪枝

AC代码

#include <cstdio>
#include <cstring>
#include <cmath>
#include <functional>
#include <algorithm>
using namespace std;
int rmx = -1, rs[405], trs[405], pw[22];
int n, k, p, has = 0;

void find(int t, int lit, int loc, int sum){
    if (t == 0 && loc > k){
        if (sum > rmx){
            rmx = sum;
            memcpy(rs, trs, sizeof(rs));
        }
        has = 1;
    }
    if (loc > k) return;
    for (int i = lit; i >= 1; --i){
        if (pw[i] <= t - (k - loc) && pw[i] * (k - loc + 1) >= t){ //剪枝
            trs[loc] = i;
            find(t - pw[i], i, loc + 1, sum + i);
        }
    }
}
int main()
{
    scanf("%d%d%d", &n, &k, &p);
    for (int i = 0; i < 21; ++i){
        pw[i] = (int)(pow(i, p) + 0.1);
    }
    find(n, 20, 1, 0);
    if (has){
        printf("%d = %d^%d", n, rs[1], p);
        for (int i = 2; i <= k; ++i){
            printf(" + %d^%d", rs[i], p);
        }
        printf("\n");
    }else {
        printf("Impossible\n");
    }
    return 0;
}
时间: 2024-08-27 10:00:52

1103. Integer Factorization (30)【搜索+剪枝】——PAT (Advanced Level) Practise的相关文章

1091. Acute Stroke (30)【搜索】——PAT (Advanced Level) Practise

题目信息 1091. Acute Stroke (30) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice,

1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise

题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with follo

【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

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c

1086. Tree Traversals Again (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1086. Tree Traversals Again (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to

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

1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less tha