IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> 【PAT甲级 - C++题解】1103 Integer Factorization -> 正文阅读

[C++知识库]【PAT甲级 - C++题解】1103 Integer Factorization

?个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1103 Integer Factorization (pintia.cn)
🔑中文翻译:整数分解
📣专栏定位:为想考甲级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 (≤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 122+42+22+22+12, or 112+62+22+22+22, 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 { a1,a2,?,aK } is said to be larger than { b1,b2,?,bK } if there exists 1≤LK such that ai=bi for i<L and aL>bL.

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

题意

正整数 NK?P 分解,是将 N 写为 K 个正整数的 P 次幂的和。

请你编写一个程序,给定 N,K,P 的情况下,找到 NK?P 分解。

思路

状态表示: f[i][j][k] 表示所有只考虑前 i 个物品,且总 p 次方和恰好是 j ,且数的个数恰好是 k 的所有选法的集合。

这个集合意思是存的答案中用到的所有数的总和,比如 n = 169, k = 5, p = 2 的最佳拆分答案为 6^2 + 6^2 + 6^2 + 6^2 + 5^2 ,所以集合 f[5][169][5] = 6 + 6 + 6 + 6 + 5 = 29

故我们需要先求出最佳的集合数,然后再反过来拆分集合得到最终的答案。

代码

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

const int N = 410;

int n, k, p;
int f[21][N][N];

//计算a的b次方
int power(int a, int b)
{
    int res = 1;
    for (int i = 0; i < b; i++)    res *= a;
    return res;
}

int main()
{
    cin >> n >> k >> p;

    //初始化
    memset(f, -0x3f, sizeof f);
    f[0][0][0] = 0;

    //开始dp
    int m;
    for (m = 1;; m++)   //从1开始向后枚举
    {
        int v = power(m, p);   //计算m的p次方
        if (v > n) break;  //如果当前枚举的数已经超过n,则dp结束

        //更新数据
        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= k; j++)
            {
                //不选当前数
                f[m][i][j] = f[m - 1][i][j];

                //选当前数
                if (i >= v && j) f[m][i][j] = max(f[m][i][j], f[m][i - v][j - 1] + m);
            }
    }

    m--;

    //从得到的集合中开始拆分出答案
    if (f[m][n][k] < 0)    puts("Impossible");
    else
    {
        printf("%d = ", n);
        bool is_first = true;
        while (m)
        {
            int v = power(m, p);
            //判断答案集合中是否有m
            while (n >= v && k && f[m][n - v][k - 1] + m == f[m][n][k])
            {
                if (is_first)    is_first = false;
                else    printf(" + ");

                printf("%d^%d", m, p);
                n -= v, k--;
            }
            m--;
        }
    }

    return 0;
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-11-05 00:06:35  更:2022-11-05 00:11:32 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 3:30:57-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码