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 = n1^P + ... nK^P

where ni (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<=L<=K 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
 #include<stdio.h>
#include<string>
#include<iostream>
#include<string.h>
#include<sstream>
#include<vector>
#include<map>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<set>
using namespace std; int k,p;
int MAX = -;
vector<int> re;
void DFS(vector<int>& vv,int n)
{
if(vv.size() == k )
{
if(n == )
{
int sum = ;
for(int i = ;i < k;++i)
sum += vv[i];
if(sum >= MAX) // 需要等号,可使得 sequence { a1, a2, ... aK } is said to be larger than { b1, b2, ... bK } i
{
MAX = sum;
re = vv;
}
}
vv.pop_back();
return;
}
int low = vv.size() == ? : vv[vv.size() -];//剪枝 使得只有增序情况
int m = sqrt(double(n));
for(int i = low ; i <= m;++i)
{
int tmp = pow(double(i),p);
if(n >= tmp)
{
vv.push_back(i);
DFS(vv,n-tmp);
}else break;
}
if(!vv.empty())
vv.pop_back();
} int main()
{
int n;
scanf("%d%d%d",&n,&k,&p);
vector<int> vv;
DFS(vv, n);
if(re.empty())
{
printf("Impossible\n");
}
else
{
printf("%d = %d^%d",n,re[re.size()-],p);
for(int i = re.size() - ;i >= ;--i)
{
printf(" + %d^%d",re[i],p);
}
printf("\n");
}
return ;
}

1103. Integer Factorization (30)的更多相关文章

  1. 1103 Integer Factorization (30)

    1103 Integer Factorization (30 分)   The K−P factorization of a positive integer N is to write N as t ...

  2. 1103 Integer Factorization (30)(30 分)

    The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

  3. 【PAT甲级】1103 Integer Factorization (30 分)

    题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...

  4. PAT (Advanced Level) 1103. Integer Factorization (30)

    暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  5. PAT甲题题解-1103. Integer Factorization (30)-(dfs)

    该题还不错~. 题意:给定N.K.P,使得可以分解成N = n1^P + … nk^P的形式,如果可以,输出sum(ni)最大的划分,如果sum一样,输出序列较大的那个.否则输出Impossible. ...

  6. PAT甲级——1103 Integer Factorization (DFS)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...

  7. PAT 1103 Integer Factorization[难]

    1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...

  8. PAT甲级1103. Integer Factorization

    PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...

  9. 【PAT】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 positi ...

随机推荐

  1. 安卓Design包之CoordinatorLayout配合AppBarLayout,ToolBar,TabLaout的使用

    转载: CoordinatorLayout配合AppBarLayout,Toolbar和TabLayout的使用 控件的简单介绍: AppBarLayout:它是继承LinerLayout实现的一个V ...

  2. jQuery阻止事件冒泡的例子

    下面给给各位朋友稍加整理了一jquery中阻止事件冒泡的一些例子,我们知道JQuery 提供了两种方式来阻止事件冒泡,但我们简单的利用它来做一些应用可能不深入或不理解,下面整理了更详细的方法,有兴趣的 ...

  3. Storm累计求和中使用各种分组Grouping

    Shuffle Grouping: 随机分组, 随机派发stream里面的tuple, 保证bolt中的每个任务接收到的tuple数目相同.(它能实现较好的负载均衡) Fields Grouping: ...

  4. IDG合伙人李丰:O2O中的C2C蕴藏巨大商机

    [ 亿欧导读 ] IDG合伙人李丰表示,每个新趋势出现,都是在解决上一轮行业革新时没有解决好的市场需求.而O2O中的C2C将会出现巨大商机的原因在于移动互联网的出现创造了新的交互方式,可以更快速的匹配 ...

  5. AspNetPager学习使用2

    接上回: <webdiyer:aspnetpager id=" FirstPageText="首页"LastPageText="尾页" Next ...

  6. javascript进阶-原型prototype

    一.javascript原型认识 很多编程语言都有类的概念,我们可以拿原型和类进行比较,看看它们之间的区别以及相同点在哪里. 1.类:类是一个具体事物的抽象所以类是一个抽象的东西,在面向对象中类可以用 ...

  7. css+js 控制幻灯片效果

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. Part 3 talking about constraint in sql

    What is Foreign key and how to create a Foreign key constraint? Note:Foreign Keys are used to enforc ...

  9. vs怎么创建MVC及理解其含义

    怎么创建MVC项目 一·1.点击 文件à新建à项目à模板àVisua C#(选择 .NET Framework 4.0或以上版本) à选择 MVC 3 Web应用程序 或者MVC 4 Web应用程序à ...

  10. (转)手把手教你如何架设VPN

    简介 让远程用户连接Exchange Server的传统解决方案是使用Outlook Web Access.然而,为何不使用虚拟专用网(Virtual Private Network,VPN)让你的远 ...