首页 > 其他分享 >1103 Integer Factorization

1103 Integer Factorization

时间:2022-11-19 02:22:44浏览次数:50  
标签:22 int positive sum Factorization 1103 Integer include 169

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≤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<math.h>
#include<algorithm>
#include<vector>                  //用vector方便更新 
using namespace std;


int seq[500];
int num;
int p;
int k;
int i=0;
vector<int> temp;
vector<int>result;
int maxsum=-1;
void find(int index,int nowk,int sum,int seqsum)
{

    if(sum==num&&nowk==k)
    {
        if(seqsum>maxsum)
        {
            maxsum=seqsum;
            result=temp;
        }
        return; 
     } 
    
    if(sum>num||nowk>k||index<1) 
    return;
    temp.push_back(index);
    find(index,nowk+1,sum+seq[index],seqsum+index);
    temp.pop_back();
    find(index-1,nowk,sum,seqsum);
    
    
}
int main()
{
    
    scanf("%d",&num);
    scanf("%d",&k);
    scanf("%d",&p);
    
    while(pow(i,p)<=num)
    {
        seq[i]=pow(i,p);
        i++;
    }
    find(i-1,0,0,0);
    int tempk=0;
    if(maxsum==-1)
    {
        printf("Impossible");
    }
    else
    {
        while(result.size()!=tempk)
    {
        if(tempk==0)
        {
            printf("%d = %d^%d",num,result[tempk],p);
            tempk++;
        }
        else
        {
            printf(" + %d^%d",result[tempk],p);
            tempk++;    
        }
    
    }
    
    }

 } 

 

 

标签:22,int,positive,sum,Factorization,1103,Integer,include,169
From: https://www.cnblogs.com/zzzlight/p/16905362.html

相关文章

  • roman-to-integer 罗马数字转整数
    问题描述:罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。字符数值I1V5X10L50C10......
  • 【JAVA】详解在JAVA中int与Integer的区别以及背后的原因。
    区别首先我们要明确,这两点之间有什么区别?主要有以下几点:数据类型不同:int是基础数据类型,而Integer是包装数据类型;默认值不同:int的默认值是0,而Integer的默认值是nul......
  • GL-Planning a trip 20221103 same
    Planningatrip20221103Needtogetawayfromitall?Planyourdreamvacationwithyourclassmates,Whowouldyouliketogoonholidaywith?这节课有人吗?I......
  • GL-Planning a trip 20221103
    Planningatrip这节课有人吗?Idon'thaveajobatpresentIhavebeenworkingfor5yearsmyhobbiesareplayvideogamesandlong-diatancerunning.Youwina......
  • 【TS】1103- 30个小知识让你更清楚TypeScript
    TypeScript是Microsoft开发的JavaScript的开源超集,用于在不破坏现有程序的情况下添加附加功能。由于其独特的优势,例如,静态类型和许多速记符号,TypeScript现在被前端和......
  • [A202211110354]
    [A202211110354](2022,南开大学)设\(x_n=\displaystyle\sum_{k=0}^n{\frac{1}{k!}}\),\(n=1,2,\cdots\),求极限\[\lim_{n\rightarrow\infty}\left(\frac{\lnx_n}{\sqr......
  • [A202211110303]
    [A202211110303](2022,同济大学)已知\(\{a_n\}\)是一个实数列,\(0<|\lambda|<1\).证明:\(\displaystyle\lim_{n\rightarrow\infty}a_n=a\)的充要条件是\[\lim_{n\rig......
  • Integer对象比较
    今天有个需求需要写个目录这不是简简单单把之前的代码复制一份 修修变量名就大功告成了 但是出现了一个bug 改了两个小时 以为Integer是对象  所以两个Int......
  • java.sql.SQLException: java.lang.ClassCastException:java.math.BigInteger cannot
    错误原因:由于在IDEA中导入的驱动包的版本和数据库的版本不匹配解决方案:​​数据库官网中下载与数据库配套的Jar包,重新导入就可以了​官网:​​https://dev.mysql.com/downloa......
  • HDU 4658 Integer Partition
    ProblemDescriptionGivenn,k,calculatethenumberofdifferent(unordered)partitionsofnsuchthatnopartisrepeatedkormoretimes.  ......