首页 > 其他分享 >1103 Integer Factorization

1103 Integer Factorization

时间:2023-05-14 14:13:00浏览次数:32  
标签:index 22 maxFacSum positive 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≤Ksuch 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<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
int n, k, p, maxFacSum = -1;
vector<int> ans, tempAns, v;
void init(){
    int tmp = 0, index = 1;
    while(tmp <= n){
        v.push_back(tmp);
        tmp = pow(index, p);
        index++;
    }
}
void dfs(int index, int tempSum, int tempK, int facSum){
    if(tempK == k){
        if(tempSum == n && facSum > maxFacSum){
            ans = tempAns;
            maxFacSum = facSum;
        }
        return;
    }
    while(index >= 1){
        if(tempSum + v[index] <= n){
            tempAns[tempK] = index;
            dfs(index, tempSum + v[index], tempK + 1, facSum + index);
        }
        if(index == 1) return;
        index--;
    }
}

int main(){
    cin>>n>>k>>p;
    init();
    tempAns.resize(k);
    dfs(v.size() - 1, 0, 0, 0);
    if(maxFacSum == -1){
        printf("Impossible");
    }else{
        cout<<n<<" = ";
        for(int i = 0; i < ans.size(); i++){
            cout<<ans[i]<<"^"<<p;
            if(i != ans.size() - 1){
                cout<<" + ";
            }
        }
    }
    return 0;
}

 

注意:

1、

while(index >= 1){
        if(tempSum + v[index] <= n){
            tempAns[tempK] = index;
            dfs(index, tempSum + v[index], tempK + 1, facSum + index);
        }
        if(index == 1) return;
        index--;
    }

dfs中要枚举所有的可能

 

2、剪枝的操作有:

1. tempK==K但是tempSum!=n的时候需要剪枝
2. 在枚举的时候,按顺序枚举,上界或者下界可进行剪枝
3. 当且仅当tempSum + v[index] <= n时,进行下一层的DFS,而不要进入下一层DFS发现不满足条件再返回,这样开销会比较大~

标签:index,22,maxFacSum,positive,Factorization,1103,Integer,include,169
From: https://www.cnblogs.com/yccy/p/17399207.html

相关文章

  • 1103.模版变量及模版过滤器
    一、模版路径总结在配置文件setting.py文件中找到TEMPLATES进行文件路径配置:1.DIRS定义一个目录列表,模板引擎按流标顺序搜索这些目录以查询模板源文件。将templates放在主项目目录下:2.APP_DIRS告诉模板引擎是否应该进入每个已安装的应用中查找模板,值为True则模板会去安装了......
  • python 报错:TypeError: only integer scalar arrays can be converted to a scalar in
    defconvolution(initial_img,kernal):img=np.zeros((initial_img.shape[0],initial_img.shape[1])).astype(np.uint8)forxinrange(1,initial_img.shape[0]-1):foryinrange(1,initial_img.shape[1]-1):temp=np.zeros([3,3......
  • TypeError: 'numpy.float64' object cannot be interpreted as an integer
    报错内容:Traceback(mostrecentcalllast):File"C:\Users\xuan\.conda\envs\pytorch1-6\lib\site-packages\scipy\sparse\_sputils.py",line225,inisintlikeoperator.index(x)TypeError:'numpy.float64'objectcannotbeinterpre......
  • Java中为什么要使用Integer呢?阐述Integer与int的区别
    (1)设计Integer封装类型的原因是:Java本身就是一个面向对象的编程语言,一切操作都是以对象作为基础,如像ArrayList,HashSet,Hashtable,HashMap等集合类中存储的元素,只支持存储Object类型,又如同泛型的设计,统统表现出了Java对于封装类型的重用,而对于int,byte,short,float,char,long,double这......
  • Java根据Integer数组(有null值)递归构造二叉树
    二叉树:publicclassTreeNode{intval;TreeNodeleft;TreeNoderight;TreeNode(){}TreeNode(intval){this.val=val;}TreeNode(intval,TreeNodeleft,TreeNoderight){this.val=val;this.l......
  • NC50454 A Simple Problem with Integers
    题目链接题目题目描述给定数列\(a[1],a[2],\dots,a[n]\),你需要依次进行q个操作,操作有两类:1lrx:给定l,r,x,对于所有\(i\in[l,r]\),将a[i]加上x(换言之,将\(a[l],a[l+1],\dots,a[r]\)分别加上x);2lr:给定l,r,求\(\sum_{i=l}^ra[i]\)的值(换言之,求\(a[l]+a[l+1]+\dots+a......
  • NC51100 A Simple Problem with Integers
    题目链接题目题目描述YouhaveNintegers,\(A_1,A_2,...,A_N\).Youneedtodealwithtwokindsofoperations.Onetypeofoperationistoaddsomegivennumbertoeachnumberinagiveninterval.Theotheristoaskforthesumofnumbersinagivenint......
  • Java基础知识点API之BigInteger的存储上限
    一:数组的长度相关内容1.数组的最大长度是int的最大值:2147483647.2.数组中每一位能表示的数字的范围:  -2147483648~21474836473.数组中最多能存储的元素个数:21亿多。4.数组中每一位能表示的数字:42亿多二:BigInteger能表示的最大数字通过了解上述的数组长度的内容,能更好的理解BigI......
  • Java编码规范-字符串与Integer的比较,BigDecimal非空参数
    Java编码规范-字符串与Integer的比较,BigDecimal非空参数packagecom.example.core.mydemo;importjava.math.BigDecimal;publicclassIntTest{publicstaticvoidmain(String[]args){Integertype=2;//if("2".equals(type)){if(typ......
  • Integer Inquiry hdoj 1047
    IntegerInquiryTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):15216    AcceptedSubmission(s):3909ProblemDescriptionOneofthefirstusersofBIT'snewsupercomputerwasChipDill......